feat(arabot): add more db checks

This commit is contained in:
smyalygames 2022-10-28 14:28:41 +01:00
parent baaa52aa14
commit 37e37011ad
3 changed files with 111 additions and 19 deletions

View File

@ -21,6 +21,7 @@ import { Args, Command, RegisterBehavior } from '@sapphire/framework';
import type { User, Message, TextChannel } from 'discord.js'; import type { User, Message, TextChannel } from 'discord.js';
import IDs from '../../utils/ids'; import IDs from '../../utils/ids';
import { addBan, checkActive } from '../../utils/database/ban'; import { addBan, checkActive } from '../../utils/database/ban';
import { addEmptyUser, addExistingUser, userExists } from '../../utils/database/dbExistingUser';
class BanCommand extends Command { class BanCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) { public constructor(context: Command.Context, options: Command.Options) {
@ -68,13 +69,13 @@ class BanCommand extends Command {
return; return;
} }
// Gets guildMember // Gets mod's GuildMember
const guildMember = guild.members.cache.get(user.id); const modGuildMember = guild.members.cache.get(mod.user.id);
// Checks if guildMember is null // Checks if guildMember is null
if (guildMember === undefined) { if (modGuildMember === undefined) {
await interaction.reply({ await interaction.reply({
content: 'Error fetching user!', content: 'Error fetching mod!',
ephemeral: true, ephemeral: true,
fetchReply: true, fetchReply: true,
}); });
@ -86,24 +87,41 @@ class BanCommand extends Command {
return; return;
} }
// Check if mod is in database
if (!await userExists(modGuildMember.id)) {
await addExistingUser(modGuildMember);
}
// Gets guildMember
const guildMember = guild.members.cache.get(user.id);
if (guildMember !== undefined) {
// Checks if the user is not restricted // Checks if the user is not restricted
if (guildMember.roles.cache.has(IDs.roles.vegan.vegan) if (guildMember.roles.cache.has(IDs.roles.vegan.vegan)
|| guildMember.roles.cache.has(IDs.roles.nonvegan.nonvegan)) { || guildMember.roles.cache.has(IDs.roles.nonvegan.nonvegan)) {
await interaction.reply({ await interaction.reply({
content: `You need to restrict ${user} first!`, content: 'You need to restrict the user first!',
ephemeral: true, ephemeral: true,
fetchReply: true, fetchReply: true,
}); });
return; return;
} }
// Check if user and mod are on the database
if (!await userExists(guildMember.id)) {
await addExistingUser(guildMember);
}
// Send DM for reason of ban // Send DM for reason of ban
await guildMember.send(`You have been banned from ARA for: ${reason}` await user.send(`You have been banned from ARA for: ${reason}`
+ '\n\nhttps://vbcamp.org/ARA') + '\n\nhttps://vbcamp.org/ARA')
.catch(() => {}); .catch(() => {});
// Ban the user // Ban the user
await guildMember.ban({ reason }); await guildMember.ban({ reason });
} else if (!await userExists(user.id)) {
await addEmptyUser(user.id);
}
await interaction.reply({ await interaction.reply({
content: `${user} has been banned.`, content: `${user} has been banned.`,
@ -175,6 +193,11 @@ class BanCommand extends Command {
return; return;
} }
// Check if mod is in database
if (!await userExists(mod.id)) {
await addExistingUser(mod);
}
// Gets guildMember // Gets guildMember
const guildMember = guild.members.cache.get(user.id); const guildMember = guild.members.cache.get(user.id);
@ -189,6 +212,11 @@ class BanCommand extends Command {
return; return;
} }
// Check if user and mod are on the database
if (!await userExists(guildMember.id)) {
await addExistingUser(guildMember);
}
// Send DM for reason of ban // Send DM for reason of ban
await user.send(`You have been banned from ARA for: ${reason}` await user.send(`You have been banned from ARA for: ${reason}`
+ '\n\nhttps://vbcamp.org/ARA') + '\n\nhttps://vbcamp.org/ARA')
@ -196,6 +224,8 @@ class BanCommand extends Command {
// Ban the user // Ban the user
await guildMember.ban({ reason }); await guildMember.ban({ reason });
} else if (!await userExists(user.id)) {
await addEmptyUser(user.id);
} }
// Add ban to database // Add ban to database

View File

@ -26,6 +26,7 @@ import type {
} from 'discord.js'; } from 'discord.js';
import IDs from '../../utils/ids'; import IDs from '../../utils/ids';
import { removeBan, checkActive, addBan } from '../../utils/database/ban'; import { removeBan, checkActive, addBan } from '../../utils/database/ban';
import { addEmptyUser, addExistingUser, userExists } from '../../utils/database/dbExistingUser';
class UnbanCommand extends Command { class UnbanCommand extends Command {
public constructor(context: Command.Context, options: Command.Options) { public constructor(context: Command.Context, options: Command.Options) {
@ -69,6 +70,24 @@ class UnbanCommand extends Command {
return; 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)) { if (!await checkActive(user.id)) {
let ban: GuildBan; let ban: GuildBan;
try { try {
@ -89,6 +108,11 @@ class UnbanCommand extends Command {
reason = ''; reason = '';
} }
// Check if user and mod are on the database
if (!await userExists(user.id)) {
await addEmptyUser(user.id);
}
// Add missing ban // Add missing ban
await addBan(user.id, mod.user.id, `(Mod who banned is not accurate) - ${reason}`); await addBan(user.id, mod.user.id, `(Mod who banned is not accurate) - ${reason}`);
} }
@ -148,6 +172,11 @@ class UnbanCommand extends Command {
return; return;
} }
// Check if mod is in database
if (!await userExists(mod.id)) {
await addExistingUser(mod);
}
if (!await checkActive(user.id)) { if (!await checkActive(user.id)) {
let ban: GuildBan; let ban: GuildBan;
try { try {
@ -169,6 +198,11 @@ class UnbanCommand extends Command {
reason = ''; reason = '';
} }
// Check if user and mod are on the database
if (!await userExists(user.id)) {
await addEmptyUser(user.id);
}
// Add missing ban // Add missing ban
await addBan(user.id, mod.user.id, `(Mod who banned is not accurate) - ${reason}`); await addBan(user.id, mod.user.id, `(Mod who banned is not accurate) - ${reason}`);
} }

View File

@ -95,6 +95,34 @@ export async function addExistingUser(user: GuildMember) {
await prisma.$disconnect(); 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) { export async function updateUser(user: GuildMember) {
// Check if the user is already on the database // Check if the user is already on the database
if (!(await userExists(user.id))) { if (!(await userExists(user.id))) {