feat(arabot): add manual verification command

This commit is contained in:
smyalygames 2023-02-20 13:10:55 +00:00
parent 9b402a435b
commit 80f2107d32

View File

@ -19,13 +19,14 @@
import { Args, Command, RegisterBehavior } from '@sapphire/framework'; import { Args, Command, RegisterBehavior } from '@sapphire/framework';
import type { import type {
GuildMember,
Message, Message,
User, User,
Guild, Guild,
Snowflake,
} from 'discord.js'; } from 'discord.js';
import IDs from '#utils/ids'; import IDs from '#utils/ids';
import { Snowflake } from 'discord.js'; import { finishVerifyMessages, giveVerificationRoles } from '#utils/verification';
import { manualVerification } from '#utils/database/verification';
export class VerifyCommand extends Command { export class VerifyCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) { public constructor(context: Command.Context, options: Command.Options) {
@ -35,7 +36,6 @@ export class VerifyCommand extends Command {
aliases: ['ver'], aliases: ['ver'],
description: 'Gives roles to the user', description: 'Gives roles to the user',
preconditions: [['ModCoordinatorOnly', 'VerifierCoordinatorOnly', 'VerifierOnly']], preconditions: [['ModCoordinatorOnly', 'VerifierCoordinatorOnly', 'VerifierOnly']],
enabled: false,
}); });
} }
@ -59,15 +59,15 @@ export class VerifyCommand extends Command {
// Command run // Command run
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
// TODO add database updates
// Get the arguments // Get the arguments
const user = interaction.options.getUser('user', true); const user = interaction.options.getUser('user', true);
const roles = interaction.options.getString('roles', true); const roles = interaction.options.getString('roles', true);
const mod = interaction.member; const verifier = interaction.member;
const { guild } = interaction; const { guild } = interaction;
const messageId = interaction.id;
// Checks if all the variables are of the right type // Checks if all the variables are of the right type
if (mod === null || guild === null) { if (verifier === null || guild === null) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching moderator or guild!', content: 'Error fetching moderator or guild!',
ephemeral: true, ephemeral: true,
@ -76,23 +76,36 @@ export class VerifyCommand extends Command {
return; return;
} }
await this.verify(user, roles, guild); const verify = await this.verify(user, verifier.user.id, roles, messageId, guild);
await interaction.reply({
content: verify.message,
fetchReply: true,
});
} }
public async messageRun(message: Message, args: Args) { public async messageRun(message: Message, args: Args) {
// Get arguments // Get arguments
let user: GuildMember; let user: User;
try { try {
user = await args.pick('member'); user = await args.pick('user');
} catch { } catch {
await message.react('❌'); await message.react('❌');
await message.reply('User was not provided!'); await message.reply('User was not provided!');
return; return;
} }
const mod = message.member; const roles = args.finished ? null : await args.rest('string');
if (mod === null) { if (roles === null) {
await message.react('❌');
await message.reply('Roles were not provided!');
return;
}
const verifier = message.member;
if (verifier === null) {
await message.react('❌'); await message.react('❌');
await message.reply('Verifier not found! Try again or contact a developer!'); await message.reply('Verifier not found! Try again or contact a developer!');
return; return;
@ -105,50 +118,86 @@ export class VerifyCommand extends Command {
await message.reply('Guild not found! Try again or contact a developer!'); await message.reply('Guild not found! Try again or contact a developer!');
return; return;
} }
const verify = await this.verify(user, verifier.user.id, roles, message.id, guild);
await message.reply(verify.message);
await message.react(verify.success ? '✅' : '❌');
} }
private async verify(user: User, rolesString: string, guild: Guild) { private async verify(
user: User,
verifierId: Snowflake,
rolesString: string,
messageId: Snowflake,
guild: Guild,
) {
const info = { const info = {
message: '', message: '',
success: false, success: false,
}; };
const validRoles = ['v', 'a', 't', 'x', 'nv', 'veg', 'conv']; const roles = {
vegan: false,
activist: false,
araVegan: false,
trusted: false,
vegCurious: false,
convinced: false,
};
const member = guild.members.cache.get(user.id); let member = guild.members.cache.get(user.id);
// Checks if guildMember is null // Checks if member is null
if (member === undefined) { if (member === undefined) {
info.message = 'Failed to fetch member'; member = await guild.members.fetch(user.id)
.catch(() => undefined);
if (member === undefined) {
info.message = 'Failed to fetch member';
return info;
}
}
if (member.roles.cache.hasAny(...IDs.roles.restrictions.restricted)) {
info.message = 'Can\'t verify a restricted user!';
return info; return info;
} }
const roles = rolesString.split(' '); let verifier = guild.members.cache.get(verifierId);
const giveRoles: Snowflake[] = []; // Checks if verifier is null
if (verifier === undefined) {
verifier = await guild.members.fetch(user.id)
.catch(() => undefined);
if (verifier === undefined) {
info.message = 'Failed to fetch verifier';
return info;
}
}
roles.forEach((role) => { const roleArgs = rolesString.split(' ');
roleArgs.forEach((role) => {
switch (role.toLowerCase()) { switch (role.toLowerCase()) {
case 'v': case 'v':
giveRoles.push(IDs.roles.vegan.vegan); roles.vegan = true;
giveRoles.push(IDs.roles.vegan.nvAccess);
break; break;
case 'a': case 'a':
giveRoles.push(IDs.roles.vegan.activist); roles.activist = true;
break; break;
case 'x': case 'x':
giveRoles.push(IDs.roles.vegan.araVegan); roles.araVegan = true;
break;
case 't': case 't':
giveRoles.push(IDs.roles.trusted); roles.trusted = true;
break; break;
case 'nv': case 'nv':
giveRoles.push(IDs.roles.nonvegan.nonvegan);
break; break;
case 'veg': case 'veg':
giveRoles.push(IDs.roles.nonvegan.vegCurious); roles.vegCurious = true;
break; break;
case 'conv': case 'conv':
giveRoles.push(IDs.roles.nonvegan.convinced); roles.convinced = true;
break; break;
default: default:
info.message = 'There was an invalid argument!'; info.message = 'There was an invalid argument!';
@ -160,14 +209,35 @@ export class VerifyCommand extends Command {
return info; return info;
} }
if (roles.includes('v') && (roles.includes('nv') || roles.includes('veg') || roles.includes('conv'))) { if ((roles.vegan || member.roles.cache.has(IDs.roles.vegan.vegan))
&& (roleArgs.includes('nv') || roles.vegCurious || roles.convinced)) {
info.message = 'Can\'t give non-vegan roles to a vegan'; info.message = 'Can\'t give non-vegan roles to a vegan';
return info; return info;
} }
if (roles.includes('nv') && (roles.includes('v') || roles.includes('a') || roles.includes('x'))) if (roleArgs.includes('nv')
&& (roles.vegan || roles.activist || roles.araVegan)) {
info.message = 'Can\'t give vegan roles to a non-vegan';
return info;
}
await member.roles.add(giveRoles); await giveVerificationRoles(member, roles, true);
await finishVerifyMessages(user, roles, true);
await manualVerification(messageId, member, verifier, roles);
if (member.roles.cache.has(IDs.roles.nonvegan.nonvegan)
&& (roles.vegan || roles.activist || roles.araVegan)) {
await member.roles.remove([
IDs.roles.nonvegan.nonvegan,
IDs.roles.nonvegan.vegCurious,
IDs.roles.nonvegan.convinced,
]);
}
info.success = true;
info.message = `Verified ${user}`;
return info;
} }
} }