Skip to content

Commit

Permalink
feat: Add handling of banned and kicked reverifications
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Drla <[email protected]>
  • Loading branch information
mimotej committed Sep 24, 2023
1 parent 182c22b commit 4164fab
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export default () => {
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildModeration,
],
});
client.login(token);
Expand Down
18 changes: 16 additions & 2 deletions src/commands/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function execute(interaction: ChatInputCommandInteraction) {
content: 'Verification failed! Contact admin.',
});
}
if (user.length != 0) {
if (user.length != 0 && user[0].status !== 'removed') {
return interaction.editReply({
content:
'User already verified! Contact admin if you need to verify again.',
Expand All @@ -78,7 +78,7 @@ export async function execute(interaction: ChatInputCommandInteraction) {
content: 'Verification failed! Contact admin.',
});
}
if (user.length != 0) {
if (user.length != 0 && user[0].status !== 'removed') {
return interaction.editReply({
content: 'This thesis is already used! Please contact admin.',
});
Expand All @@ -100,6 +100,20 @@ export async function execute(interaction: ChatInputCommandInteraction) {
});
}

if (user[0]?.status === 'removed') {
try {
await prisma.users.delete({
where: {
id: user[0].id,
},
});
} catch (err) {
console.log(`Database error: ${err}`);
return interaction.editReply({
content: 'Verification failed! Contact admin.',
});
}
}
const roleProgramm = await assignRole(
interaction,
scrapedConfirmationStudy,
Expand Down
44 changes: 44 additions & 0 deletions src/listeners/actionListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Listenes for kick or ban actions and adds either a kick or ban value to user status if user is verified
*/

import { Client, GuildBan } from 'discord.js';
import { prisma } from '../model';

export default (client: Client) => {
client.on('guildBanAdd', async (guildBan: GuildBan) => {
const user = await prisma.users.findFirst({
where: {
discordId: guildBan.user.id,
},
});
if (user) {
await prisma.users.update({
where: {
id: user.id,
},
data: {
status: 'banned',
},
});
}
});

client.on('guildMemberRemove', async (member) => {
const user = await prisma.users.findFirst({
where: {
discordId: member.id,
},
});
if (user) {
await prisma.users.update({
where: {
id: user.id,
},
data: {
status: 'removed',
},
});
}
});
};
158 changes: 158 additions & 0 deletions tests/verify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ const databaseUser = {
joinDate: new Date(),
};

const databaseRemovedUser = {
id: '123',
discordId: '123',
idThesis: '2223121',
status: 'removed',
joinDate: new Date(),
};

const databaseBannedUser = {
id: '123',
discordId: '123',
idThesis: '2223121',
status: 'banned',
joinDate: new Date(),
};

describe('Tests for verify command', () => {
const interaction = mockDeep<ChatInputCommandInteraction>();
beforeEach(() => {
Expand Down Expand Up @@ -207,6 +223,148 @@ describe('Tests for verify command', () => {
});
});

it('Verified user was removed verification should succeed', async () => {
prismaMock.users.findMany.mockResolvedValue([databaseRemovedUser]);
interaction.user.id = '123';
interaction.options.getString
.calledWith('linktoconfirmationmuni')
.mockReturnValue(
'https://is.muni.cz/confirmation-of-studies/cccxxd3?lang=en'
);
interaction.options.getString
.calledWith('bachelorthesislink')
.mockReturnValue('https://dspace.vutbr.cz/handle/11012/2223121');
jest.spyOn(utils, 'scrapeThesis').mockReturnValue(
Promise.resolve(nameUser)
);
jest.spyOn(utils, 'scrapeConfirmationStudies').mockReturnValue(
Promise.resolve(dict)
);
if (interaction.guild) {
interaction.guild.roles.cache.find.mockReturnValue(
'test' as unknown as Role
);
}
await verify.execute(interaction);
expect(interaction.editReply).toHaveBeenCalledWith({
content: 'You have been successfully verified with role undefined.',
});
});

it('Verified user was removed new user uses same thesis verification should succeed', async () => {
prismaMock.users.findMany.mockResolvedValue([databaseRemovedUser]);
interaction.user.id = '1234';
interaction.options.getString
.calledWith('linktoconfirmationmuni')
.mockReturnValue(
'https://is.muni.cz/confirmation-of-studies/cccxxd3?lang=en'
);
interaction.options.getString
.calledWith('bachelorthesislink')
.mockReturnValue('https://dspace.vutbr.cz/handle/11012/2223121');
jest.spyOn(utils, 'scrapeThesis').mockReturnValue(
Promise.resolve(nameUser)
);
jest.spyOn(utils, 'scrapeConfirmationStudies').mockReturnValue(
Promise.resolve(dict)
);
if (interaction.guild) {
interaction.guild.roles.cache.find.mockReturnValue(
'test' as unknown as Role
);
}
await verify.execute(interaction);
expect(interaction.editReply).toHaveBeenCalledWith({
content: 'You have been successfully verified with role undefined.',
});
});

it('Verified user was banned verification should fail', async () => {
prismaMock.users.findMany.mockResolvedValue([databaseBannedUser]);
interaction.user.id = '123';
interaction.options.getString
.calledWith('linktoconfirmationmuni')
.mockReturnValue(
'https://is.muni.cz/confirmation-of-studies/cccxxd3?lang=en'
);
interaction.options.getString
.calledWith('bachelorthesislink')
.mockReturnValue('https://dspace.vutbr.cz/handle/11012/2223121');
jest.spyOn(utils, 'scrapeThesis').mockReturnValue(
Promise.resolve(nameUser)
);
jest.spyOn(utils, 'scrapeConfirmationStudies').mockReturnValue(
Promise.resolve(dict)
);
if (interaction.guild) {
interaction.guild.roles.cache.find.mockReturnValue(
'test' as unknown as Role
);
}
await verify.execute(interaction);
expect(interaction.editReply).toHaveBeenCalledWith({
content:
'User already verified! Contact admin if you need to verify again.',
});
});
it('Verified user was banned verification should fail', async () => {
prismaMock.users.findMany.mockResolvedValue([databaseBannedUser]);
interaction.user.id = '123';
interaction.options.getString
.calledWith('linktoconfirmationmuni')
.mockReturnValue(
'https://is.muni.cz/confirmation-of-studies/cccxxd3?lang=en'
);
interaction.options.getString
.calledWith('bachelorthesislink')
.mockReturnValue('https://dspace.vutbr.cz/handle/11012/2223121');
jest.spyOn(utils, 'scrapeThesis').mockReturnValue(
Promise.resolve(nameUser)
);
jest.spyOn(utils, 'scrapeConfirmationStudies').mockReturnValue(
Promise.resolve(dict)
);
if (interaction.guild) {
interaction.guild.roles.cache.find.mockReturnValue(
'test' as unknown as Role
);
}
await verify.execute(interaction);
expect(interaction.editReply).toHaveBeenCalledWith({
content:
'User already verified! Contact admin if you need to verify again.',
});
});

it('Verified user was banned verification should fail', async () => {
prismaMock.users.findMany.mockResolvedValue([databaseRemovedUser]);
interaction.user.id = '1234';
interaction.options.getString
.calledWith('linktoconfirmationmuni')
.mockReturnValue(
'https://is.muni.cz/confirmation-of-studies/cccxxd3?lang=en'
);
interaction.options.getString
.calledWith('bachelorthesislink')
.mockReturnValue('https://dspace.vutbr.cz/handle/11012/2223121');
jest.spyOn(utils, 'scrapeThesis').mockReturnValue(
Promise.resolve(nameUser)
);
jest.spyOn(utils, 'scrapeConfirmationStudies').mockReturnValue(
Promise.resolve(dict)
);
if (interaction.guild) {
interaction.guild.roles.cache.find.mockReturnValue(
'test' as unknown as Role
);
}
await verify.execute(interaction);
expect(interaction.editReply).toHaveBeenCalledWith({
content:
'User already verified! Contact admin if you need to verify again.',
});
});

it('User is using thesis which is assigned to different user', async () => {
interaction.user.id = '123';
prismaMock.users.findMany
Expand Down

0 comments on commit 4164fab

Please sign in to comment.