From 37e37011ad17d46b97ec861c8cb95ee5e1267694 Mon Sep 17 00:00:00 2001 From: smyalygames Date: Fri, 28 Oct 2022 14:28:41 +0100 Subject: [PATCH] feat(arabot): add more db checks --- src/commands/mod/ban.ts | 68 ++++++++++++++++++++-------- src/commands/mod/unban.ts | 34 ++++++++++++++ src/utils/database/dbExistingUser.ts | 28 ++++++++++++ 3 files changed, 111 insertions(+), 19 deletions(-) diff --git a/src/commands/mod/ban.ts b/src/commands/mod/ban.ts index 0b3279f..15eab5b 100644 --- a/src/commands/mod/ban.ts +++ b/src/commands/mod/ban.ts @@ -21,6 +21,7 @@ import { Args, Command, RegisterBehavior } from '@sapphire/framework'; import type { User, Message, TextChannel } from 'discord.js'; import IDs from '../../utils/ids'; import { addBan, checkActive } from '../../utils/database/ban'; +import { addEmptyUser, addExistingUser, userExists } from '../../utils/database/dbExistingUser'; class BanCommand extends Command { public constructor(context: Command.Context, options: Command.Options) { @@ -68,13 +69,13 @@ class BanCommand extends Command { return; } - // Gets guildMember - const guildMember = guild.members.cache.get(user.id); + // Gets mod's GuildMember + const modGuildMember = guild.members.cache.get(mod.user.id); // Checks if guildMember is null - if (guildMember === undefined) { + if (modGuildMember === undefined) { await interaction.reply({ - content: 'Error fetching user!', + content: 'Error fetching mod!', ephemeral: true, fetchReply: true, }); @@ -86,24 +87,41 @@ class BanCommand extends Command { return; } - // Checks if the user is not restricted - if (guildMember.roles.cache.has(IDs.roles.vegan.vegan) - || guildMember.roles.cache.has(IDs.roles.nonvegan.nonvegan)) { - await interaction.reply({ - content: `You need to restrict ${user} first!`, - ephemeral: true, - fetchReply: true, - }); - return; + // Check if mod is in database + if (!await userExists(modGuildMember.id)) { + await addExistingUser(modGuildMember); } - // Send DM for reason of ban - await guildMember.send(`You have been banned from ARA for: ${reason}` - + '\n\nhttps://vbcamp.org/ARA') - .catch(() => {}); + // Gets guildMember + const guildMember = guild.members.cache.get(user.id); - // Ban the user - await guildMember.ban({ reason }); + if (guildMember !== undefined) { + // Checks if the user is not restricted + if (guildMember.roles.cache.has(IDs.roles.vegan.vegan) + || guildMember.roles.cache.has(IDs.roles.nonvegan.nonvegan)) { + await interaction.reply({ + content: 'You need to restrict the user first!', + ephemeral: true, + fetchReply: true, + }); + return; + } + + // Check if user and mod are on the database + if (!await userExists(guildMember.id)) { + await addExistingUser(guildMember); + } + + // Send DM for reason of ban + await user.send(`You have been banned from ARA for: ${reason}` + + '\n\nhttps://vbcamp.org/ARA') + .catch(() => {}); + + // Ban the user + await guildMember.ban({ reason }); + } else if (!await userExists(user.id)) { + await addEmptyUser(user.id); + } await interaction.reply({ content: `${user} has been banned.`, @@ -175,6 +193,11 @@ class BanCommand extends Command { return; } + // Check if mod is in database + if (!await userExists(mod.id)) { + await addExistingUser(mod); + } + // Gets guildMember const guildMember = guild.members.cache.get(user.id); @@ -189,6 +212,11 @@ class BanCommand extends Command { return; } + // Check if user and mod are on the database + if (!await userExists(guildMember.id)) { + await addExistingUser(guildMember); + } + // Send DM for reason of ban await user.send(`You have been banned from ARA for: ${reason}` + '\n\nhttps://vbcamp.org/ARA') @@ -196,6 +224,8 @@ class BanCommand extends Command { // Ban the user await guildMember.ban({ reason }); + } else if (!await userExists(user.id)) { + await addEmptyUser(user.id); } // Add ban to database diff --git a/src/commands/mod/unban.ts b/src/commands/mod/unban.ts index 5da0eac..07e9c6b 100644 --- a/src/commands/mod/unban.ts +++ b/src/commands/mod/unban.ts @@ -26,6 +26,7 @@ import type { } from 'discord.js'; import IDs from '../../utils/ids'; import { removeBan, checkActive, addBan } from '../../utils/database/ban'; +import { addEmptyUser, addExistingUser, userExists } from '../../utils/database/dbExistingUser'; class UnbanCommand extends Command { public constructor(context: Command.Context, options: Command.Options) { @@ -69,6 +70,24 @@ class UnbanCommand extends Command { return; } + // Gets mod's GuildMember + const modGuildMember = guild.members.cache.get(mod.user.id); + + // Checks if guildMember is null + if (modGuildMember === undefined) { + await interaction.reply({ + content: 'Error fetching mod!', + ephemeral: true, + fetchReply: true, + }); + return; + } + + // Check if mod is in database + if (!await userExists(modGuildMember.id)) { + await addExistingUser(modGuildMember); + } + if (!await checkActive(user.id)) { let ban: GuildBan; try { @@ -89,6 +108,11 @@ class UnbanCommand extends Command { reason = ''; } + // Check if user and mod are on the database + if (!await userExists(user.id)) { + await addEmptyUser(user.id); + } + // Add missing ban await addBan(user.id, mod.user.id, `(Mod who banned is not accurate) - ${reason}`); } @@ -148,6 +172,11 @@ class UnbanCommand extends Command { return; } + // Check if mod is in database + if (!await userExists(mod.id)) { + await addExistingUser(mod); + } + if (!await checkActive(user.id)) { let ban: GuildBan; try { @@ -169,6 +198,11 @@ class UnbanCommand extends Command { reason = ''; } + // Check if user and mod are on the database + if (!await userExists(user.id)) { + await addEmptyUser(user.id); + } + // Add missing ban await addBan(user.id, mod.user.id, `(Mod who banned is not accurate) - ${reason}`); } diff --git a/src/utils/database/dbExistingUser.ts b/src/utils/database/dbExistingUser.ts index 37f66cc..cc19c85 100644 --- a/src/utils/database/dbExistingUser.ts +++ b/src/utils/database/dbExistingUser.ts @@ -95,6 +95,34 @@ export async function addExistingUser(user: GuildMember) { await prisma.$disconnect(); } +// Add an empty user to database in case they are not on the server +export async function addEmptyUser(userId: string) { + // Initialises Prisma Client + const prisma = new PrismaClient(); + + // Counts if the user is on the database by their snowflake + const userQuery = await prisma.user.count({ + where: { + id: userId, + }, + }); + + // If the user is already in the database + if (userQuery > 0) { + return; + } + + // Create the user in the database + await prisma.user.create({ + data: { + id: userId, + }, + }); + + // Close the database connection + await prisma.$disconnect(); +} + export async function updateUser(user: GuildMember) { // Check if the user is already on the database if (!(await userExists(user.id))) {