From 02298f2089abc92c58c2cd88b333e0e5f8a1dab7 Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Thu, 4 Jan 2024 11:23:32 +0000 Subject: [PATCH] feat(arabot): completed warn command --- src/commands/mod/warn.ts | 143 ++++++++++++++++++++++++++++----- src/utils/database/warnings.ts | 9 ++- src/utils/devIDs.ts | 1 + src/utils/ids.ts | 1 + 4 files changed, 130 insertions(+), 24 deletions(-) diff --git a/src/commands/mod/warn.ts b/src/commands/mod/warn.ts index 7610a51..08d9944 100644 --- a/src/commands/mod/warn.ts +++ b/src/commands/mod/warn.ts @@ -17,26 +17,81 @@ along with this program. If not, see . */ -import { Args, Command } from '@sapphire/framework'; -import type { User, Message, Snowflake, Guild } from 'discord.js'; -import { addExistingUser, updateUser } from '#utils/database/dbExistingUser'; +import { + Args, + Command, + container, + RegisterBehavior, +} from '@sapphire/framework'; +import type { User, Message, Snowflake, Guild, TextChannel } from 'discord.js'; +import { updateUser } from '#utils/database/dbExistingUser'; import { addWarn } from '#utils/database/warnings'; +import { EmbedBuilder } from 'discord.js'; +import IDs from '#utils/ids'; -/* - This command is not intended to be functional for now, this is purely to log - warnings onto a database, so if we were to switch purely to ARA Bot, it would - mean we would have a lot of the warns already in the database. -*/ export class WarnCommand extends Command { public constructor(context: Command.LoaderContext, options: Command.Options) { super(context, { ...options, name: 'warn', - description: 'Warns a user (only used for logging to a database for now)', + description: 'Warns a user', preconditions: [['CoordinatorOnly', 'ModOnly']], }); } + // Registers that this is a slash command + public override registerApplicationCommands(registry: Command.Registry) { + registry.registerChatInputCommand( + (builder) => + builder + .setName(this.name) + .setDescription(this.description) + .addUserOption((option) => + option + .setName('user') + .setDescription('User to warn') + .setRequired(true), + ) + .addStringOption((option) => + option + .setName('reason') + .setDescription('Reason for the warning') + .setRequired(true), + ), + { + behaviorWhenNotIdentical: RegisterBehavior.Overwrite, + }, + ); + } + + // Command run + public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { + // Get the arguments + const user = interaction.options.getUser('user', true); + const reason = interaction.options.getString('reason', true); + const mod = interaction.user; + const { guild } = interaction; + + // Checks if all the variables are of the right type + if (guild === null) { + await interaction.reply({ + content: 'Error fetching guild!', + ephemeral: true, + fetchReply: true, + }); + return; + } + + await interaction.deferReply({ ephemeral: true }); + + const info = await this.warn(user.id, mod.id, reason, guild); + + await interaction.editReply({ + content: info.message, + }); + } + + // Non Application Command method for warning a user public async messageRun(message: Message, args: Args) { // Get arguments let user: User; @@ -76,9 +131,10 @@ export class WarnCommand extends Command { if (!warn.success) { await message.react('❌'); + return; } - // await message.react('✅'); + await message.react('✅'); } private async warn( @@ -104,24 +160,67 @@ export class WarnCommand extends Command { // Check if mod is in database await updateUser(mod); - // Gets guildMember - let member = guild.members.cache.get(userId); + // Gets User for person being restricted + let user = guild.client.users.cache.get(userId); - if (member === undefined) { - member = await guild.members.fetch(userId).catch(() => undefined); + if (user === undefined) { + user = await guild.client.users.fetch(userId); + if (user === undefined) { + info.message = 'Error fetching user'; + return info; + } } - if (member === undefined) { - info.message = 'User is not on this server'; - return info; - } - - await addExistingUser(member); - await addWarn(userId, modId, reason); - info.message = `Warned ${member}`; + info.message = `Warned ${user}`; info.success = true; + + // DM the reason + + const dmEmbed = new EmbedBuilder() + .setColor('#FF6700') + .setAuthor({ + name: "You've been warned!", + iconURL: `${user.displayAvatarURL()}`, + }) + .addFields({ name: 'Reason', value: reason }) + .setTimestamp(); + + await user.send({ embeds: [dmEmbed] }).catch(() => {}); + + // Log the ban + let logChannel = guild.channels.cache.get(IDs.channels.logs.sus) as + | TextChannel + | undefined; + + if (logChannel === undefined) { + logChannel = (await guild.channels.fetch( + IDs.channels.logs.restricted, + )) as TextChannel | undefined; + if (logChannel === undefined) { + container.logger.error('Warn Error: Could not fetch log channel'); + info.message = `Warned ${user} but could not find the log channel. This has been logged to the database.`; + return info; + } + } + + const message = new EmbedBuilder() + .setColor('#FF6700') + .setAuthor({ + name: `Warned ${user.tag}`, + iconURL: `${user.displayAvatarURL()}`, + }) + .addFields( + { name: 'User', value: `${user}`, inline: true }, + { name: 'Moderator', value: `${mod}`, inline: true }, + { name: 'Reason', value: reason }, + ) + .setTimestamp() + .setFooter({ text: `ID: ${userId}` }); + + await logChannel.send({ embeds: [message] }); + return info; } } diff --git a/src/utils/database/warnings.ts b/src/utils/database/warnings.ts index ca13e47..ea5d121 100644 --- a/src/utils/database/warnings.ts +++ b/src/utils/database/warnings.ts @@ -9,8 +9,13 @@ export async function addWarn( await container.database.warning.create({ data: { user: { - connect: { - id: userId, + connectOrCreate: { + where: { + id: userId, + }, + create: { + id: userId, + }, }, }, mod: { diff --git a/src/utils/devIDs.ts b/src/utils/devIDs.ts index 96bb6b2..6450381 100644 --- a/src/utils/devIDs.ts +++ b/src/utils/devIDs.ts @@ -122,6 +122,7 @@ const devIDs = { logs: { restricted: '999431681217937513', economy: '999431681599623198', + sus: '999431681599623199', }, }, categories: { diff --git a/src/utils/ids.ts b/src/utils/ids.ts index 304bf23..708bc89 100644 --- a/src/utils/ids.ts +++ b/src/utils/ids.ts @@ -124,6 +124,7 @@ let IDs = { logs: { restricted: '920993034462715925', economy: '932050015034159174', + sus: '872884989950324826', }, }, categories: {