mirror of
https://github.com/veganhacktivists/arabot.git
synced 2025-05-19 10:34:16 +02:00
feat(arabot): add more db checks
This commit is contained in:
parent
baaa52aa14
commit
37e37011ad
@ -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;
|
||||
}
|
||||
|
||||
// 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
|
||||
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!`,
|
||||
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 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')
|
||||
.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
|
||||
|
@ -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}`);
|
||||
}
|
||||
|
@ -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))) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user