From 873afcd4d637bfee45254366b44e34aa3b3f8d93 Mon Sep 17 00:00:00 2001 From: smyalygames Date: Fri, 3 Mar 2023 17:45:42 +0000 Subject: [PATCH] refactor(arabot): change role command structures --- src/commands/roles/bookClub.ts | 98 +++++------ src/commands/roles/debateHost.ts | 102 +++++------ src/commands/roles/gameNightHost.ts | 102 +++++------ src/commands/roles/guest.ts | 98 +++++------ src/commands/roles/staff/mentor.ts | 97 +++++------ src/commands/roles/staff/mod.ts | 103 +++++------- src/commands/roles/staff/restrictedAccess.ts | 103 +++++------- src/commands/roles/staff/stagehost.ts | 98 +++++------ src/commands/roles/staff/trialVerifier.ts | 101 +++++------ src/commands/roles/staff/verifier.ts | 105 +++++------- src/commands/roles/verification/activist.ts | 152 +++++++---------- src/commands/roles/verification/convinced.ts | 135 ++++++--------- src/commands/roles/verification/plus.ts | 113 +++++-------- src/commands/roles/verification/trusted.ts | 110 ++++++------ src/commands/roles/verification/vegan.ts | 158 +++++++----------- src/commands/roles/verification/vegcurious.ts | 141 ++++++---------- 16 files changed, 742 insertions(+), 1074 deletions(-) diff --git a/src/commands/roles/bookClub.ts b/src/commands/roles/bookClub.ts index c6d3391..05dce7e 100644 --- a/src/commands/roles/bookClub.ts +++ b/src/commands/roles/bookClub.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class BookClubCommand extends Command { @@ -50,67 +50,39 @@ export class BookClubCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || mod === null || guild === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const bookClub = guild.roles.cache.get(IDs.roles.bookClub); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || bookClub === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageBookClub(user, mod, guild); - // Checks if the user has Book Club and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.bookClub)) { - // Remove the Book Club role from the user - await guildMember.roles.remove(bookClub); - await interaction.reply({ - content: `Removed the ${bookClub.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Give Book Club role to the user - await guildMember.roles.add(bookClub); - await interaction.reply({ - content: `Gave ${user} the ${bookClub.name} role!`, - fetchReply: true, - }); - - await user.send(`You have been given the ${bookClub.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); @@ -126,31 +98,45 @@ export class BookClubCommand extends Command { return; } + const info = await this.manageBookClub(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageBookClub(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); const bookClub = guild.roles.cache.get(IDs.roles.bookClub); + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + if (bookClub === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + info.message = 'Error fetching book club role from cache!'; + return info; } // Checks if the user has Book Club and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.bookClub)) { + if (member.roles.cache.has(IDs.roles.bookClub)) { // Remove the Book Club role from the user - await user.roles.remove(bookClub); - await message.reply({ - content: `Removed the ${bookClub.name} role from ${user}`, - }); - } else { - // Give Book Club role to the user - await user.roles.add(bookClub); - await message.reply({ - content: `Gave ${user} the ${bookClub.name} role!`, - }); - await user.send(`You have been given the ${bookClub.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(bookClub); + info.message = `Removed the ${bookClub.name} role from ${user}`; + return info; } + // Add Book Club role to the user + await member.roles.add(bookClub); + info.message = `Gave ${user} the ${bookClub.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${bookClub.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/debateHost.ts b/src/commands/roles/debateHost.ts index 2bed4a7..010e7ca 100644 --- a/src/commands/roles/debateHost.ts +++ b/src/commands/roles/debateHost.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class DebateHostCommand extends Command { @@ -50,67 +50,39 @@ export class DebateHostCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || mod === null || guild === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const debate = guild.roles.cache.get(IDs.roles.debateHost); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || debate === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageDebateHost(user, mod, guild); - // Checks if the user has Debate Host and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.debateHost)) { - // Remove the Debate Host role from the user - await guildMember.roles.remove(debate); - await interaction.reply({ - content: `Removed the ${debate.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Give Debate Host role to the user - await guildMember.roles.add(debate); - await interaction.reply({ - content: `Gave ${user} the ${debate.name} role!`, - fetchReply: true, - }); - - await user.send(`You have been given the ${debate.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); @@ -126,31 +98,45 @@ export class DebateHostCommand extends Command { return; } - const debate = guild.roles.cache.get(IDs.roles.debateHost); + const info = await this.manageDebateHost(user, mod, guild); - if (debate === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageDebateHost(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); + const debateHost = guild.roles.cache.get(IDs.roles.debateHost); + + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + + if (debateHost === undefined) { + info.message = 'Error fetching debate host role from cache!'; + return info; } // Checks if the user has Debate Host and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.debateHost)) { + if (member.roles.cache.has(IDs.roles.debateHost)) { // Remove the Debate Host role from the user - await user.roles.remove(debate); - await message.reply({ - content: `Removed the ${debate.name} role from ${user}`, - }); - } else { - // Give Debate Host role to the user - await user.roles.add(debate); - await message.reply({ - content: `Gave ${user} the ${debate.name} role!`, - }); - await user.send(`You have been given the ${debate.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(debateHost); + info.message = `Removed the ${debateHost.name} role from ${user}`; + return info; } + // Add Debate Host role to the user + await member.roles.add(debateHost); + info.message = `Gave ${user} the ${debateHost.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${debateHost.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/gameNightHost.ts b/src/commands/roles/gameNightHost.ts index 2c33162..ef1a5b7 100644 --- a/src/commands/roles/gameNightHost.ts +++ b/src/commands/roles/gameNightHost.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class GameNightHostCommand extends Command { @@ -50,67 +50,39 @@ export class GameNightHostCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || mod === null || guild === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const game = guild.roles.cache.get(IDs.roles.gameNightHost); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || game === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageGameNight(user, mod, guild); - // Checks if the user has Game Night and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.gameNightHost)) { - // Remove the Game Night Host role from the user - await guildMember.roles.remove(game); - await interaction.reply({ - content: `Removed the ${game.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Give Game Night Host role to the user - await guildMember.roles.add(game); - await interaction.reply({ - content: `Gave ${user} the ${game.name} role!`, - fetchReply: true, - }); - - await user.send(`You have been given the ${game.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); @@ -126,31 +98,45 @@ export class GameNightHostCommand extends Command { return; } - const game = guild.roles.cache.get(IDs.roles.gameNightHost); + const info = await this.manageGameNight(user, mod, guild); - if (game === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageGameNight(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); + const gameNightHost = guild.roles.cache.get(IDs.roles.gameNightHost); + + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + + if (gameNightHost === undefined) { + info.message = 'Error fetching game night host role from cache!'; + return info; } // Checks if the user has Game Night and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.gameNightHost)) { + if (member.roles.cache.has(IDs.roles.gameNightHost)) { // Remove the Game Night Host role from the user - await user.roles.remove(game); - await message.reply({ - content: `Removed the ${game.name} role from ${user}`, - }); - } else { - // Give Game Night Host role to the user - await user.roles.add(game); - await message.reply({ - content: `Gave ${user} the ${game.name} role!`, - }); - await user.send(`You have been given the ${game.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(gameNightHost); + info.message = `Removed the ${gameNightHost.name} role from ${user}`; + return info; } + // Add Game Night Host role to the user + await member.roles.add(gameNightHost); + info.message = `Gave ${user} the ${gameNightHost.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${gameNightHost.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/guest.ts b/src/commands/roles/guest.ts index 367c2f9..ac3b7f3 100644 --- a/src/commands/roles/guest.ts +++ b/src/commands/roles/guest.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class GuestCommand extends Command { @@ -50,67 +50,39 @@ export class GuestCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || mod === null || guild === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const guest = guild.roles.cache.get(IDs.roles.guest); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || guest === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageGuest(user, mod, guild); - // Checks if the user has Guest and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.guest)) { - // Remove the Guest role from the user - await guildMember.roles.remove(guest); - await interaction.reply({ - content: `Removed the ${guest.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Give Guest role to the user - await guildMember.roles.add(guest); - await interaction.reply({ - content: `Gave ${user} the ${guest.name} role!`, - fetchReply: true, - }); - - await user.send(`You have been given the ${guest.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); @@ -126,31 +98,45 @@ export class GuestCommand extends Command { return; } + const info = await this.manageGuest(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageGuest(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); const guest = guild.roles.cache.get(IDs.roles.guest); + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + if (guest === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + info.message = 'Error fetching guest role from cache!'; + return info; } // Checks if the user has Guest and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.guest)) { + if (member.roles.cache.has(IDs.roles.guest)) { // Remove the Guest role from the user - await user.roles.remove(guest); - await message.reply({ - content: `Removed the ${guest.name} role from ${user}`, - }); - } else { - // Give Guest role to the user - await user.roles.add(guest); - await message.reply({ - content: `Gave ${user} the ${guest.name} role!`, - }); - await user.send(`You have been given the ${guest.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(guest); + info.message = `Removed the ${guest.name} role from ${user}`; + return info; } + // Add Guest role to the user + await member.roles.add(guest); + info.message = `Gave ${user} the ${guest.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${guest.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/staff/mentor.ts b/src/commands/roles/staff/mentor.ts index 6d73510..9f711ff 100644 --- a/src/commands/roles/staff/mentor.ts +++ b/src/commands/roles/staff/mentor.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class MentorCommand extends Command { @@ -51,66 +51,39 @@ export class MentorCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || guild === null || mod === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const mentor = guild.roles.cache.get(IDs.roles.staff.mentor); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || mentor === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageMentor(user, mod, guild); - // Checks if the user has Mentor and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.staff.mentor)) { - // Remove the Mentor role from the user - await guildMember.roles.remove(mentor); - await interaction.reply({ - content: `Removed the ${mentor.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Add Mentor role to the user - await guildMember.roles.add(mentor); - await interaction.reply({ - content: `Gave ${user} the ${mentor.name} role!`, - fetchReply: true, - }); - await user.send(`You have been given the ${mentor.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); @@ -126,31 +99,45 @@ export class MentorCommand extends Command { return; } + const info = await this.manageMentor(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageMentor(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); const mentor = guild.roles.cache.get(IDs.roles.staff.mentor); + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + if (mentor === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + info.message = 'Error fetching mentor role from cache!'; + return info; } // Checks if the user has Mentor and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.staff.mentor)) { + if (member.roles.cache.has(IDs.roles.staff.mentor)) { // Remove the Mentor role from the user - await user.roles.remove(mentor); - await message.reply({ - content: `Removed the ${mentor.name} role from ${user}`, - }); - } else { - // Give Mentor role to the user - await user.roles.add(mentor); - await message.reply({ - content: `Gave ${user} the ${mentor.name} role!`, - }); - await user.send(`You have been given the ${mentor.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(mentor); + info.message = `Removed the ${mentor.name} role from ${user}`; + return info; } + // Add Mentor role to the user + await member.roles.add(mentor); + info.message = `Gave ${user} the ${mentor.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${mentor.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/staff/mod.ts b/src/commands/roles/staff/mod.ts index 44c32aa..25d05ed 100644 --- a/src/commands/roles/staff/mod.ts +++ b/src/commands/roles/staff/mod.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class ModCommand extends Command { @@ -50,70 +50,43 @@ export class ModCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || guild === null || mod === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const modRole = guild.roles.cache.get(IDs.roles.staff.moderator); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || modRole === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageMod(user, mod, guild); - // Checks if the user has Mod and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.staff.moderator)) { - // Remove the Mod role from the user - await guildMember.roles.remove(modRole); - await interaction.reply({ - content: `Removed the ${modRole.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Add Mod role to the user - await guildMember.roles.add(modRole); - await interaction.reply({ - content: `Gave ${user} the ${modRole.name} role!`, - fetchReply: true, - }); - await user.send(`You have been given the ${modRole.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); - await message.reply('Moderator coordinator not found! Try again or contact a developer!'); + await message.reply('Mod coordinator not found! Try again or contact a developer!'); return; } @@ -125,31 +98,45 @@ export class ModCommand extends Command { return; } - const modRole = guild.roles.cache.get(IDs.roles.staff.moderator); + const info = await this.manageMod(user, mod, guild); - if (modRole === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageMod(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); + const moderator = guild.roles.cache.get(IDs.roles.staff.moderator); + + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + + if (moderator === undefined) { + info.message = 'Error fetching the moderator role from cache!'; + return info; } // Checks if the user has Mod and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.staff.moderator)) { + if (member.roles.cache.has(IDs.roles.staff.moderator)) { // Remove the Mod role from the user - await user.roles.remove(modRole); - await message.reply({ - content: `Removed the ${modRole.name} role from ${user}`, - }); - } else { - // Give Mod role to the user - await user.roles.add(modRole); - await message.reply({ - content: `Gave ${user} the ${modRole.name} role!`, - }); - await user.send(`You have been given the ${modRole.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(moderator); + info.message = `Removed the ${moderator.name} role from ${user}`; + return info; } + // Add Mod role to the user + await member.roles.add(moderator); + info.message = `Gave ${user} the ${moderator.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${moderator.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/staff/restrictedAccess.ts b/src/commands/roles/staff/restrictedAccess.ts index 61de1e7..3d372db 100644 --- a/src/commands/roles/staff/restrictedAccess.ts +++ b/src/commands/roles/staff/restrictedAccess.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class RestrictedAccessCommand extends Command { @@ -51,70 +51,43 @@ export class RestrictedAccessCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || guild === null || mod === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const restrictedAccess = guild.roles.cache.get(IDs.roles.staff.restricted); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || restrictedAccess === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageRestrictedAccess(user, mod, guild); - // Checks if the user has RA and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.staff.restricted)) { - // Remove the Restricted Access role from the user - await guildMember.roles.remove(restrictedAccess); - await interaction.reply({ - content: `Removed the ${restrictedAccess.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Add Restricted Access role to the user - await guildMember.roles.add(restrictedAccess); - await interaction.reply({ - content: `Gave ${user} the ${restrictedAccess.name} role!`, - fetchReply: true, - }); - await user.send(`You have been given the ${restrictedAccess.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); - await message.reply('Moderator coordinator not found! Try again or contact a developer!'); + await message.reply('Mod coordinator not found! Try again or contact a developer!'); return; } @@ -126,31 +99,45 @@ export class RestrictedAccessCommand extends Command { return; } - const restrictedAccess = guild.roles.cache.get(IDs.roles.staff.restricted); + const info = await this.manageRestrictedAccess(user, mod, guild); - if (restrictedAccess === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageRestrictedAccess(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); + const restricted = guild.roles.cache.get(IDs.roles.staff.restricted); + + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + + if (restricted === undefined) { + info.message = 'Error fetching the restricted access role from cache!'; + return info; } // Checks if the user has RA and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.staff.restricted)) { + if (member.roles.cache.has(IDs.roles.staff.restricted)) { // Remove the Restricted Access role from the user - await user.roles.remove(restrictedAccess); - await message.reply({ - content: `Removed the ${restrictedAccess.name} role from ${user}`, - }); - } else { - // Give Restricted Access role to the user - await user.roles.add(restrictedAccess); - await message.reply({ - content: `Gave ${user} the ${restrictedAccess.name} role!`, - }); - await user.send(`You have been given the ${restrictedAccess.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(restricted); + info.message = `Removed the ${restricted.name} role from ${user}`; + return info; } + // Add Restricted Access role to the user + await member.roles.add(restricted); + info.message = `Gave ${user} the ${restricted.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${restricted.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/staff/stagehost.ts b/src/commands/roles/staff/stagehost.ts index aba98b1..3fd8b9b 100644 --- a/src/commands/roles/staff/stagehost.ts +++ b/src/commands/roles/staff/stagehost.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class StageHostCommand extends Command { @@ -50,67 +50,39 @@ export class StageHostCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || mod === null || guild === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const stageHost = guild.roles.cache.get(IDs.roles.stageHost); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || stageHost === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageStageHost(user, mod, guild); - // Checks if the user has Stage Host and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.stageHost)) { - // Remove the Veg Curious role from the user - await guildMember.roles.remove(stageHost); - await interaction.reply({ - content: `Removed the ${stageHost.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Give Stage Host role to the user - await guildMember.roles.add(stageHost); - await interaction.reply({ - content: `Gave ${user} the ${stageHost.name} role!`, - fetchReply: true, - }); - - await user.send(`You have been given the ${stageHost.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); @@ -126,31 +98,45 @@ export class StageHostCommand extends Command { return; } + const info = await this.manageStageHost(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageStageHost(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); const stageHost = guild.roles.cache.get(IDs.roles.stageHost); + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + if (stageHost === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + info.message = 'Error fetching stage host role from cache!'; + return info; } // Checks if the user has Stage Host and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.stageHost)) { + if (member.roles.cache.has(IDs.roles.stageHost)) { // Remove the Stage Host role from the user - await user.roles.remove(stageHost); - await message.reply({ - content: `Removed the ${stageHost.name} role from ${user}`, - }); - } else { - // Give Stage Host role to the user - await user.roles.add(stageHost); - await message.reply({ - content: `Gave ${user} the ${stageHost.name} role!`, - }); - await user.send(`You have been given the ${stageHost.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(stageHost); + info.message = `Removed the ${stageHost.name} role from ${user}`; + return info; } + // Add Stage Host role to the user + await member.roles.add(stageHost); + info.message = `Gave ${user} the ${stageHost.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${stageHost.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/staff/trialVerifier.ts b/src/commands/roles/staff/trialVerifier.ts index 9a38a2c..e1c9b5f 100644 --- a/src/commands/roles/staff/trialVerifier.ts +++ b/src/commands/roles/staff/trialVerifier.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class TrialVerifierCommand extends Command { @@ -50,66 +50,39 @@ export class TrialVerifierCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || guild === null || mod === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const verifier = guild.roles.cache.get(IDs.roles.staff.trialVerifier); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || verifier === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageTrialVerifier(user, mod, guild); - // Checks if the user has TV and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.staff.trialVerifier)) { - // Remove the Trial Verifier role from the user - await guildMember.roles.remove(verifier); - await interaction.reply({ - content: `Removed the ${verifier.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Add Trial Verifier role to the user - await guildMember.roles.add(verifier); - await interaction.reply({ - content: `Gave ${user} the ${verifier.name} role!`, - fetchReply: true, - }); - await user.send(`You have been given the ${verifier.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); @@ -125,31 +98,45 @@ export class TrialVerifierCommand extends Command { return; } - const verifier = guild.roles.cache.get(IDs.roles.staff.trialVerifier); + const info = await this.manageTrialVerifier(user, mod, guild); - if (verifier === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageTrialVerifier(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); + const trialVerifier = guild.roles.cache.get(IDs.roles.staff.trialVerifier); + + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + + if (trialVerifier === undefined) { + info.message = 'Error fetching the trial verifier role from cache!'; + return info; } // Checks if the user has TV and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.staff.trialVerifier)) { + if (member.roles.cache.has(IDs.roles.staff.trialVerifier)) { // Remove the Trial Verifier role from the user - await user.roles.remove(verifier); - await message.reply({ - content: `Removed the ${verifier.name} role from ${user}`, - }); - } else { - // Give Trial Verifier role to the user - await user.roles.add(verifier); - await message.reply({ - content: `Gave ${user} the ${verifier.name} role!`, - }); - await user.send(`You have been given the ${verifier.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(trialVerifier); + info.message = `Removed the ${trialVerifier.name} role from ${user}`; + return info; } + // Add Trial Verifier role to the user + await member.roles.add(trialVerifier); + info.message = `Gave ${user} the ${trialVerifier.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${trialVerifier.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/staff/verifier.ts b/src/commands/roles/staff/verifier.ts index da9dc58..a145f72 100644 --- a/src/commands/roles/staff/verifier.ts +++ b/src/commands/roles/staff/verifier.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class VerifierCommand extends Command { @@ -50,70 +50,39 @@ export class VerifierCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || guild === null || mod === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const verifier = guild.roles.cache.get(IDs.roles.staff.verifier); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || verifier === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageVerifier(user, mod, guild); - // Checks if the user has Verifier and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.staff.verifier)) { - // Remove the Verifier role from the user - await guildMember.roles.remove(verifier); - await interaction.reply({ - content: `Removed the ${verifier.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Add Verifier role to the user - await guildMember.roles.add(verifier); - await interaction.reply({ - content: `Gave ${user} the ${verifier.name} role!`, - fetchReply: true, - }); - await user.send(`You have been given the ${verifier.name} role by ${mod}!`) - .catch(() => {}); - - if (guildMember.roles.cache.has(IDs.roles.staff.trialVerifier)) { - await guildMember.roles.remove(IDs.roles.staff.trialVerifier); - } + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); @@ -129,35 +98,45 @@ export class VerifierCommand extends Command { return; } + const info = await this.manageVerifier(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageVerifier(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); const verifier = guild.roles.cache.get(IDs.roles.staff.verifier); + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + if (verifier === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + info.message = 'Error fetching verifier role from cache!'; + return info; } // Checks if the user has Verifier and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.staff.verifier)) { + if (member.roles.cache.has(IDs.roles.staff.verifier)) { // Remove the Verifier role from the user - await user.roles.remove(verifier); - await message.reply({ - content: `Removed the ${verifier.name} role from ${user}`, - }); - } else { - // Give Verifier role to the user - await user.roles.add(verifier); - await message.reply({ - content: `Gave ${user} the ${verifier.name} role!`, - }); - await user.send(`You have been given the ${verifier.name} role by ${mod}!`) - .catch(() => {}); - - if (user.roles.cache.has(IDs.roles.staff.trialVerifier)) { - await user.roles.remove(IDs.roles.staff.trialVerifier); - } + await member.roles.remove(verifier); + info.message = `Removed the ${verifier.name} role from ${user}`; + return info; } + // Add Verifier role to the user + await member.roles.add(verifier); + info.message = `Gave ${user} the ${verifier.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${verifier.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/verification/activist.ts b/src/commands/roles/verification/activist.ts index d8a1f73..585c1d9 100644 --- a/src/commands/roles/verification/activist.ts +++ b/src/commands/roles/verification/activist.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class ActivistCommand extends Command { @@ -51,98 +51,43 @@ export class ActivistCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || mod === null || guild === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const modMember = guild.members.cache.get(mod.user.id); - const activist = guild.roles.cache.get(IDs.roles.vegan.activist); - const verCoordinator = guild.roles.cache.get(IDs.roles.staff.verifierCoordinator); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined - || modMember === undefined - || activist === undefined - || verCoordinator === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageActivist(user, mod, guild); - // Checks if the user is an activist - if (guildMember.roles.cache.has(IDs.roles.vegan.activist) - && !(modMember.roles.cache.has(IDs.roles.staff.verifierCoordinator) - || modMember.roles.cache.has(IDs.roles.staff.modCoordinator))) { - await interaction.reply({ - content: `${user} is an activist, only ${verCoordinator.name} can run this!`, - ephemeral: true, - fetchReply: true, - }); - return; - } - - // Checks if the user has Activist and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.vegan.activist)) { - // Remove the Activist role from the user - await guildMember.roles.remove(activist); - await interaction.reply({ - content: `Removed the ${activist.name} role from ${user}`, - ephemeral: true, - fetchReply: true, - }); - return; - } - - // Add Activist role to the user - await guildMember.roles.add(activist); - await interaction.reply({ - content: `Gave ${user} the ${activist.name} role!`, - ephemeral: true, - fetchReply: true, - }); - - const activistMsg = `${user} you have been given the ${activist.name} role by ${mod}! ` - + `This means that if you'd wish to engage with non-vegans in <#${IDs.channels.nonVegan.general}>, you should follow these rules:\n\n` - + '1. Try to move conversations with non-vegans towards veganism/animal ethics\n' - + '2. Don\'t discuss social topics while activism is happening\n' - + '3. Have evidence for claims you make. "I don\'t know" is an acceptable answer. Chances are someone here knows or you can take time to find out\n' - + '4. Don\'t advocate for baby steps towards veganism. Participation in exploitation can stop today\n' - + '5. Differences in opinion between activists should be resolved in vegan spaces, not in the chat with non-vegans'; - await guildMember.send(activistMsg) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); - await message.reply('Verifier not found! Try again or contact a developer!'); + await message.reply('Staff not found! Try again or contact a developer!'); return; } @@ -154,48 +99,67 @@ export class ActivistCommand extends Command { return; } - // Gets guildMember whilst removing the ability of each other variables being null + const info = await this.manageActivist(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageActivist(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); + const modMember = guild.members.cache.get(mod.id); const activist = guild.roles.cache.get(IDs.roles.vegan.activist); - const verCoordinator = guild.roles.cache.get(IDs.roles.staff.verifierCoordinator); - if (activist === undefined - || verCoordinator === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; } - // Checks if the user is an activist - if (user.roles.cache.has(IDs.roles.vegan.activist) - && !(mod.roles.cache.has(IDs.roles.staff.verifierCoordinator) - || mod.roles.cache.has(IDs.roles.staff.modCoordinator))) { - await message.reply({ - content: `${user} is an activist, only ${verCoordinator.name} can run this!`, - }); - await message.react('❌'); - return; + if (modMember === undefined) { + info.message = 'Error fetching the staff\'s guild member!'; + return info; } - // Checks if the user has Activist and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.vegan.activist)) { + if (activist === undefined) { + info.message = 'Error fetching activist role from cache!'; + return info; + } + + // Checks if the user is Activist and to give them or remove them based on if they have it + if (member.roles.cache.has(IDs.roles.vegan.activist)) { + if (!modMember.roles.cache.hasAny( + IDs.roles.staff.verifierCoordinator, + IDs.roles.staff.modCoordinator, + )) { + info.message = 'You need to be a verifier coordinator to remove this role!'; + return info; + } + // Remove the Activist role from the user - await user.roles.remove(activist); - await message.react('✅'); - return; + await member.roles.remove(activist); + info.message = `Removed the ${activist.name} role from ${user}`; + return info; } // Add Activist role to the user - await user.roles.add(activist); - await message.react('✅'); + await member.roles.add(activist); + info.message = `Gave ${user} the ${activist.name} role!`; - const activistMsg = `${user} you have been given the ${activist.name} role by ${mod}! ` + await user.send( + `${user} you have been given the ${activist.name} role by ${mod}! ` + `This means that if you'd wish to engage with non-vegans in <#${IDs.channels.nonVegan.general}>, you should follow these rules:\n\n` + '1. Try to move conversations with non-vegans towards veganism/animal ethics\n' + '2. Don\'t discuss social topics while activism is happening\n' + '3. Have evidence for claims you make. "I don\'t know" is an acceptable answer. Chances are someone here knows or you can take time to find out\n' + '4. Don\'t advocate for baby steps towards veganism. Participation in exploitation can stop today\n' - + '5. Differences in opinion between activists should be resolved in vegan spaces, not in the chat with non-vegans'; - await user.send(activistMsg) - .catch(() => {}); + + '5. Differences in opinion between activists should be resolved in vegan spaces, not in the chat with non-vegans', + ).catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/verification/convinced.ts b/src/commands/roles/verification/convinced.ts index 8d12fc2..68aa7a8 100644 --- a/src/commands/roles/verification/convinced.ts +++ b/src/commands/roles/verification/convinced.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class ConvincedCommand extends Command { @@ -51,82 +51,43 @@ export class ConvincedCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || guild === null || mod === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const convinced = guild.roles.cache.get(IDs.roles.nonvegan.convinced); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || convinced === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageConvinced(user, mod, guild); - // Checks if the user is vegan - if (guildMember.roles.cache.has(IDs.roles.vegan.vegan)) { - await interaction.reply({ - content: `${user} is already vegan!`, - ephemeral: true, - fetchReply: true, - }); - } - - // Checks if the user has Convinced and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.nonvegan.convinced)) { - // Remove the Veg Curious role from the user - await guildMember.roles.remove(convinced); - await interaction.reply({ - content: `Removed the ${convinced.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Add Convinced role to the user - await guildMember.roles.add(convinced); - await interaction.reply({ - content: `Gave ${user} the ${convinced.name} role!`, - fetchReply: true, - }); - await user.send(`You have been given the ${convinced.name} role by ${mod}!` - + '\n\nThis role allows you to get access to the Diet Support section in this server that can help you go vegan ' - + 'and other parts of the server! :)' - + '\n\nThank you for caring about the animals 💚') - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); - await message.reply('Moderator not found! Try again or contact a developer!'); + await message.reply('Mod not found! Try again or contact a developer!'); return; } @@ -138,48 +99,54 @@ export class ConvincedCommand extends Command { return; } + const info = await this.manageConvinced(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageConvinced(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); const convinced = guild.roles.cache.get(IDs.roles.nonvegan.convinced); - if (convinced === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; } - // Checks if the user is vegan - if (user.roles.cache.has(IDs.roles.vegan.vegan)) { - await message.reply({ - content: `${user} is already vegan!`, - }); - await message.react('❌'); - return; + if (convinced === undefined) { + info.message = 'Error fetching coordinator role from cache!'; + return info; + } + + if (member.roles.cache.has(IDs.roles.vegan.vegan)) { + info.message = `${user} is already vegan, cannot give the ${convinced.name} role!`; + return info; } // Checks if the user has Convinced and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.nonvegan.convinced)) { - // Remove the Veg Curious role from the user - await user.roles.remove(convinced); - await message.reply({ - content: `Removed the ${convinced.name} role from ${user}`, - }); - } else { - // Give Convinced role to the user - await user.roles.add(convinced); - await message.reply({ - content: `Gave ${user} the ${convinced.name} role!`, - }); - await user.send(`You have been given the ${convinced.name} role by ${mod}!` - + '\n\nThis role allows you to get access to the Diet Support section in this server that can help you go vegan ' - + 'and other parts of the server! :)' - + '\n\nThank you for caring about the animals 💚') - .catch(() => message.reply('User\'s DMs are closed.')); + if (member.roles.cache.has(IDs.roles.nonvegan.convinced)) { + // Remove the Convinced role from the user + await member.roles.remove(convinced); + info.message = `Removed the ${convinced.name} role from ${user}`; + return info; } + // Add Convinced role to the user + await member.roles.add(convinced); + info.message = `Gave ${user} the ${convinced.name} role!`; - // Checks if the user is xlevra to send a very kind message - if (mod.id === '259624904746467329') { - await message.reply('Moist!'); - } + await user.send(`You have been given the ${convinced.name} role by ${mod}!` + + '\n\nThis role allows you to get access to the Diet Support section in this server that can help you go vegan ' + + 'and other parts of the server! :)' + + '\n\nThank you for caring about the animals 💚') + .catch(() => {}); - await message.react('✅'); + info.success = true; + return info; } } diff --git a/src/commands/roles/verification/plus.ts b/src/commands/roles/verification/plus.ts index 452698e..0de90b0 100644 --- a/src/commands/roles/verification/plus.ts +++ b/src/commands/roles/verification/plus.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class PlusCommand extends Command { @@ -51,80 +51,43 @@ export class PlusCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || guild === null || mod === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const plus = guild.roles.cache.get(IDs.roles.vegan.plus); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || plus === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.managePlus(user, mod, guild); - // Checks if the user is not vegan - if (!guildMember.roles.cache.has(IDs.roles.vegan.vegan)) { - await interaction.reply({ - content: `${user} is not vegan!`, - ephemeral: true, - fetchReply: true, - }); - return; - } - - // Checks if the user has Plus and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.vegan.plus)) { - // Remove the Plus role from the user - await guildMember.roles.remove(plus); - await interaction.reply({ - content: `Removed the ${plus.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Add Plus role to the user - await guildMember.roles.add(plus); - await interaction.reply({ - content: `Gave ${user} the ${plus.name} role!`, - fetchReply: true, - }); - await user.send(`You have been given the ${plus.name} role by ${mod}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); - await message.reply('Moderator not found! Try again or contact a developer!'); + await message.reply('Coordinator not found! Try again or contact a developer!'); return; } @@ -136,37 +99,45 @@ export class PlusCommand extends Command { return; } + const info = await this.managePlus(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async managePlus(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); const plus = guild.roles.cache.get(IDs.roles.vegan.plus); - if (plus === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; } - if (!user.roles.cache.has(IDs.roles.vegan.vegan)) { - await message.react('❌'); - await message.reply(`${user} is not vegan!`); - return; + if (plus === undefined) { + info.message = 'Error fetching plus role from cache!'; + return info; } // Checks if the user has Plus and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.vegan.plus)) { + if (member.roles.cache.has(IDs.roles.vegan.plus)) { // Remove the Plus role from the user - await user.roles.remove(plus); - await message.reply({ - content: `Removed the ${plus.name} role from ${user}`, - }); - } else { - // Give Plus role to the user - await user.roles.add(plus); - await message.reply({ - content: `Gave ${user} the ${plus.name} role!`, - }); - await user.send(`You have been given the ${plus.name} role by ${mod}!`) - .catch(() => {}); + await member.roles.remove(plus); + info.message = `Removed the ${plus.name} role from ${user}`; + return info; } + // Add Plus role to the user + await member.roles.add(plus); + info.message = `Gave ${user} the ${plus.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${plus.name} role by ${mod}!`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/verification/trusted.ts b/src/commands/roles/verification/trusted.ts index ed1e347..7897562 100644 --- a/src/commands/roles/verification/trusted.ts +++ b/src/commands/roles/verification/trusted.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class TrustedCommand extends Command { @@ -51,73 +51,43 @@ export class TrustedCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || guild === null || mod === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const trusted = guild.roles.cache.get(IDs.roles.trusted); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined || trusted === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageTrusted(user, mod, guild); - // Checks if the user has Trusted and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.trusted)) { - // Remove the Trusted role from the user - await guildMember.roles.remove(trusted); - await interaction.reply({ - content: `Removed the ${trusted.name} role from ${user}`, - fetchReply: true, - }); - return; - } - // Add Trusted role to the user - await guildMember.roles.add(trusted); - await interaction.reply({ - content: `Gave ${user} the ${trusted.name} role!`, - fetchReply: true, - }); - await user.send(`You have been given the ${trusted.name} role by ${mod}!` - + '\n\nThis role allows you to post attachments to the server and stream in VCs.' - + '\nMake sure that you follow the rules, don\'t post anything NSFW, anything objectifying animals and follow Discord\'s ToS.' - + `\nNot following these rules can result in the removal of the ${trusted.name} role.`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); - await message.reply('Moderator not found! Try again or contact a developer!'); + await message.reply('Mod not found! Try again or contact a developer!'); return; } @@ -129,34 +99,48 @@ export class TrustedCommand extends Command { return; } + const info = await this.manageTrusted(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageTrusted(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); const trusted = guild.roles.cache.get(IDs.roles.trusted); + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; + } + if (trusted === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + info.message = 'Error fetching trusted role from cache!'; + return info; } // Checks if the user has Trusted and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.trusted)) { - // Remove the Veg Curious role from the user - await user.roles.remove(trusted); - await message.reply({ - content: `Removed the ${trusted.name} role from ${user}`, - }); - } else { - // Give Trusted role to the user - await user.roles.add(trusted); - await message.reply({ - content: `Gave ${user} the ${trusted.name} role!`, - }); - await user.send(`You have been given the ${trusted.name} role by ${mod}!` - + '\n\nThis role allows you to post attachments to the server and stream in VCs.' - + '\nMake sure that you follow the rules, and don\'t post anything NSFW, anything objectifying animals and follow Discord\'s ToS.' - + `\nNot following these rules can result in the removal of the ${trusted.name} role.`) - .catch(() => {}); + if (member.roles.cache.has(IDs.roles.trusted)) { + // Remove the Trusted role from the user + await member.roles.remove(trusted); + info.message = `Removed the ${trusted.name} role from ${user}`; + return info; } + // Add Trusted role to the user + await member.roles.add(trusted); + info.message = `Gave ${user} the ${trusted.name} role!`; - await message.react('✅'); + await user.send(`You have been given the ${trusted.name} role by ${mod}!` + + '\n\nThis role allows you to post attachments to the server and stream in VCs.' + + '\nMake sure that you follow the rules, and don\'t post anything NSFW, anything objectifying animals and follow Discord\'s ToS.' + + `\nNot following these rules can result in the removal of the ${trusted.name} role.`) + .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/verification/vegan.ts b/src/commands/roles/verification/vegan.ts index a68a3c8..c21c005 100644 --- a/src/commands/roles/verification/vegan.ts +++ b/src/commands/roles/verification/vegan.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class VeganCommand extends Command { @@ -51,104 +51,43 @@ export class VeganCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mod = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || mod === null || guild === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const modMember = guild.members.cache.get(mod.user.id); - const vegan = guild.roles.cache.get(IDs.roles.vegan.vegan); - const verCoordinator = guild.roles.cache.get(IDs.roles.staff.verifierCoordinator); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined - || modMember === undefined - || vegan === undefined - || verCoordinator === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageVegan(user, mod, guild); - // Checks if the user is vegan - if (guildMember.roles.cache.has(IDs.roles.vegan.vegan) - && !(modMember.roles.cache.has(IDs.roles.staff.verifierCoordinator) - || modMember.roles.cache.has(IDs.roles.staff.modCoordinator))) { - await interaction.reply({ - content: `${user} is vegan, only ${verCoordinator.name} can run this!`, - ephemeral: true, - fetchReply: true, - }); - return; - } - - // Checks if the user has Vegan and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.vegan.vegan)) { - // Remove the Vegan role from the user - await guildMember.roles.add(IDs.roles.nonvegan.nonvegan); - await guildMember.roles.remove([ - vegan, - IDs.roles.vegan.activist, - IDs.roles.vegan.nvAccess, - ]); - await interaction.reply({ - content: `Removed the ${vegan.name} role from ${user}`, - ephemeral: true, - fetchReply: true, - }); - return; - } - - // Add Vegan role to the user - await guildMember.roles.add([ - vegan, - IDs.roles.vegan.nvAccess, - ]); - await guildMember.roles.remove([ - IDs.roles.nonvegan.nonvegan, - IDs.roles.nonvegan.convinced, - IDs.roles.nonvegan.vegCurious, - ]); - await interaction.reply({ - content: `Gave ${user} the ${vegan.name} role!`, - ephemeral: true, - fetchReply: true, - }); - - await guildMember.send(`You have been given the ${vegan.name} role by ${mod.user}!`) - .catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mod = message.member; + const mod = message.author; if (mod === null) { await message.react('❌'); - await message.reply('Verifier not found! Try again or contact a developer!'); + await message.reply('Staff not found! Try again or contact a developer!'); return; } @@ -160,50 +99,69 @@ export class VeganCommand extends Command { return; } - // Gets guildMember whilst removing the ability of each other variables being null + const info = await this.manageVegan(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageVegan(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); + const modMember = guild.members.cache.get(mod.id); const vegan = guild.roles.cache.get(IDs.roles.vegan.vegan); - const verCoordinator = guild.roles.cache.get(IDs.roles.staff.verifierCoordinator); - if (vegan === undefined - || verCoordinator === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; } - // Checks if the user is vegan - if (user.roles.cache.has(IDs.roles.vegan.vegan) - || !(mod.roles.cache.has(IDs.roles.staff.verifierCoordinator) - || mod.roles.cache.has(IDs.roles.staff.modCoordinator))) { - await message.reply({ - content: `${user} is vegan, only ${verCoordinator.name} can run this!`, - }); - await message.react('❌'); - return; + if (modMember === undefined) { + info.message = 'Error fetching the staff\'s guild member!'; + return info; } - // Checks if the user has Vegan and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.vegan.vegan)) { - // Remove the Veg Curious role from the user - await user.roles.add(IDs.roles.nonvegan.nonvegan); - await user.roles.remove([ + if (vegan === undefined) { + info.message = 'Error fetching vegan role from cache!'; + return info; + } + + // Checks if the user is Vegan and to give them or remove them based on if they have it + if (member.roles.cache.has(IDs.roles.vegan.vegan)) { + if (!modMember.roles.cache.hasAny( + IDs.roles.staff.verifierCoordinator, + IDs.roles.staff.modCoordinator, + )) { + info.message = 'You need to be a verifier coordinator to remove these roles!'; + return info; + } + + // Remove the Vegan role from the user + await member.roles.add(IDs.roles.nonvegan.nonvegan); + await member.roles.remove([ vegan, IDs.roles.vegan.activist, ]); - await message.react('✅'); - return; + info.message = `Removed the ${vegan.name} role from ${user}`; + return info; } // Add Vegan role to the user - await user.roles.add(vegan); - await user.roles.remove([ + await member.roles.add(vegan); + await member.roles.remove([ IDs.roles.nonvegan.nonvegan, IDs.roles.nonvegan.convinced, IDs.roles.nonvegan.vegCurious, ]); - await message.react('✅'); + info.message = `Gave ${user} the ${vegan.name} role!`; - await user.send(`You have been given the ${vegan.name} role by ${mod.user}!`) + await user.send(`You have been given the ${vegan.name} role by ${mod}!`) .catch(() => {}); + info.success = true; + return info; } } diff --git a/src/commands/roles/verification/vegcurious.ts b/src/commands/roles/verification/vegcurious.ts index cbef131..59988af 100644 --- a/src/commands/roles/verification/vegcurious.ts +++ b/src/commands/roles/verification/vegcurious.ts @@ -18,7 +18,7 @@ */ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; -import type { GuildMember, Message } from 'discord.js'; +import type { Guild, User, Message } from 'discord.js'; import IDs from '#utils/ids'; export class VegCuriousCommand extends Command { @@ -51,92 +51,43 @@ export class VegCuriousCommand extends Command { public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments - const user = interaction.options.getUser('user'); - const mentor = interaction.member; + const user = interaction.options.getUser('user', true); + const mod = interaction.user; const { guild } = interaction; // Checks if all the variables are of the right type - if (user === null || mentor === null || guild === null) { + if (guild === null) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching guild!', ephemeral: true, fetchReply: true, }); return; } - // Gets guildMember whilst removing the ability of each other variables being null - const guildMember = guild.members.cache.get(user.id); - const mentorMember = guild.members.cache.get(mentor.user.id); - const vegCurious = guild.roles.cache.get(IDs.roles.nonvegan.vegCurious); - const mentorCoordinator = guild.roles.cache.get(IDs.roles.staff.mentorCoordinator); - const dietSupport = guild.channels.cache.get(IDs.channels.dietSupport.main); + await interaction.deferReply({ ephemeral: true }); - // Checks if guildMember is null - if (guildMember === undefined - || mentorMember === undefined - || vegCurious === undefined - || mentorCoordinator === undefined - || dietSupport === undefined) { - await interaction.reply({ - content: 'Error fetching user!', - ephemeral: true, - fetchReply: true, - }); - return; - } + const info = await this.manageVegCurious(user, mod, guild); - // Checks if the user is vegan - if (guildMember.roles.cache.has(IDs.roles.vegan.vegan) - && !mentorMember.roles.cache.has(IDs.roles.staff.mentorCoordinator)) { - await interaction.reply({ - content: `${user} is vegan, only ${mentorCoordinator.name} can run this!`, - ephemeral: true, - fetchReply: true, - }); - return; - } - - // Checks if the user has Veg Curious and to give them or remove them based on if they have it - if (guildMember.roles.cache.has(IDs.roles.nonvegan.vegCurious)) { - // Remove the Veg Curious role from the user - await guildMember.roles.remove(vegCurious); - await interaction.reply({ - content: `Removed the ${vegCurious.name} role from ${user}`, - ephemeral: true, - fetchReply: true, - }); - return; - } - - // Add Veg Curious role to the user - await guildMember.roles.add(vegCurious); - await interaction.reply({ - content: `Gave ${user} the ${vegCurious.name} role!`, - ephemeral: true, - fetchReply: true, - }); - - await guildMember.send(`You have been given the ${vegCurious.name} role by ${mentor.user} ` - + `which gives you access to ${dietSupport}`).catch(() => {}); + await interaction.editReply(info.message); } public async messageRun(message: Message, args: Args) { // Get arguments - let user: GuildMember; + let user: User; try { - user = await args.pick('member'); + user = await args.pick('user'); } catch { await message.react('❌'); await message.reply('User was not provided!'); return; } - const mentor = message.member; + const mod = message.author; - if (mentor === null) { + if (mod === null) { await message.react('❌'); - await message.reply('Mentor not found! Try again or contact a developer!'); + await message.reply('Staff not found! Try again or contact a developer!'); return; } @@ -148,41 +99,57 @@ export class VegCuriousCommand extends Command { return; } + const info = await this.manageVegCurious(user, mod, guild); + + await message.reply(info.message); + await message.react(info.success ? '✅' : '❌'); + } + + private async manageVegCurious(user: User, mod: User, guild: Guild) { + const info = { + message: '', + success: false, + }; + const member = guild.members.cache.get(user.id); + const modMember = guild.members.cache.get(mod.id); const vegCurious = guild.roles.cache.get(IDs.roles.nonvegan.vegCurious); - const mentorCoordinator = guild.roles.cache.get(IDs.roles.staff.mentorCoordinator); - const dietSupport = guild.channels.cache.get(IDs.channels.dietSupport.main); - if (vegCurious === undefined - || mentorCoordinator === undefined - || dietSupport === undefined) { - await message.react('❌'); - await message.reply('Role not found! Try again or contact a developer!'); - return; + // Checks if user's GuildMember was found in cache + if (member === undefined) { + info.message = 'Error fetching guild member for the user!'; + return info; } - // Checks if the user is vegan - if (user.roles.cache.has(IDs.roles.vegan.vegan) - && !mentor.roles.cache.has(IDs.roles.staff.mentorCoordinator)) { - await message.reply({ - content: `${user} is vegan, only ${mentorCoordinator.name} can run this!`, - }); - await message.react('❌'); - return; + if (modMember === undefined) { + info.message = 'Error fetching the staff\'s guild member!'; + return info; } - // Checks if the user has Veg Curious and to give them or remove them based on if they have it - if (user.roles.cache.has(IDs.roles.nonvegan.vegCurious)) { + if (vegCurious === undefined) { + info.message = 'Error fetching veg curious role from cache!'; + return info; + } + + // Checks if the user is Veg Curious and to give them or remove them based on if they have it + if (member.roles.cache.has(IDs.roles.nonvegan.vegCurious)) { + if (!modMember.roles.cache.has(IDs.roles.staff.mentorCoordinator)) { + info.message = 'You need to be a mentor coordinator to remove this role!'; + return info; + } + // Remove the Veg Curious role from the user - await user.roles.remove(vegCurious); - await message.react('✅'); - return; + await member.roles.remove(vegCurious); + info.message = `Removed the ${vegCurious.name} role from ${user}`; + return info; } // Add Veg Curious role to the user - await user.roles.add(vegCurious); - await message.react('✅'); + await member.roles.add(vegCurious); + info.message = `Gave ${user} the ${vegCurious.name} role!`; - await user.send(`You have been given the ${vegCurious.name} role by ${mentor.user} ` - + `which gives you access to ${dietSupport}`).catch(() => {}); + await user.send(`You have been given the ${vegCurious.name} role by ${mod} ` + + 'which gives you access to the diet support section').catch(() => {}); + info.success = true; + return info; } }