From 75b768046d09d00ee4151f8fee05a796d361f31f Mon Sep 17 00:00:00 2001 From: Anthony Date: Tue, 23 Aug 2022 02:13:35 +0100 Subject: [PATCH] feat(verification): add util config fixes --- src/commands/mod/sus.ts | 2 +- src/utils/database/dbExistingUser.ts | 11 ++-- src/utils/dbExistingUser.ts | 87 ---------------------------- src/utils/verificationConfig.ts | 21 +++++++ 4 files changed, 26 insertions(+), 95 deletions(-) delete mode 100644 src/utils/dbExistingUser.ts create mode 100644 src/utils/verificationConfig.ts diff --git a/src/commands/mod/sus.ts b/src/commands/mod/sus.ts index fb2d9d7..13f960f 100644 --- a/src/commands/mod/sus.ts +++ b/src/commands/mod/sus.ts @@ -23,7 +23,7 @@ import { } from 'discord.js'; import { PrismaClient } from '@prisma/client'; import { isMessageInstance } from '@sapphire/discord.js-utilities'; -import { addExistingUser, userExists } from '../../utils/dbExistingUser'; +import { addExistingUser, userExists } from '../../utils/database/dbExistingUser'; import IDs from '../../utils/ids'; // TODO add a check when they join the server to give the user the sus role again diff --git a/src/utils/database/dbExistingUser.ts b/src/utils/database/dbExistingUser.ts index 36f5c62..b86824b 100644 --- a/src/utils/database/dbExistingUser.ts +++ b/src/utils/database/dbExistingUser.ts @@ -27,7 +27,7 @@ export async function userExists(user: GuildMember) { const prisma = new PrismaClient(); // Counts if the user is on the database by their snowflake - const userExists = await prisma.user.count({ + const userQuery = await prisma.user.count({ where: { id: user.id, }, @@ -37,10 +37,7 @@ export async function userExists(user: GuildMember) { await prisma.$disconnect(); // If the user is found on the database, then return true, otherwise, false. - if (userExists > 0) { - return true; - } - return false; + return userQuery > 0; } function getRoles(roles: GuildMemberRoleManager) { @@ -65,14 +62,14 @@ export async function addExistingUser(user: GuildMember) { const prisma = new PrismaClient(); // Counts if the user is on the database by their snowflake - const userExists = await prisma.user.count({ + const userQuery = await prisma.user.count({ where: { id: user.id, }, }); // If the user is already in the database - if (userExists > 0) { + if (userQuery > 0) { return; } diff --git a/src/utils/dbExistingUser.ts b/src/utils/dbExistingUser.ts deleted file mode 100644 index 14a5892..0000000 --- a/src/utils/dbExistingUser.ts +++ /dev/null @@ -1,87 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -/* - Animal Rights Advocates Discord Bot - Copyright (C) 2022 Anthony Berg - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -import type { GuildMember } from 'discord.js'; -import { PrismaClient } from '@prisma/client'; -import IDs from './ids'; - -// Checks if the user exists on the database -export async function userExists(user: GuildMember) { - // 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: user.id, - }, - }); - - // Close the database connection - await prisma.$disconnect(); - - // If the user is found on the database, then return true, otherwise, false. - return userQuery > 0; -} - -// Adds the user to the database if they were already on the server before the bot/database -export async function addExistingUser(user: GuildMember) { - // 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: user.id, - }, - }); - - // If the user is already in the database - if (userQuery > 0) { - return; - } - - // Checks what roles the user has - const hasVegan = user.roles.cache.has(IDs.roles.vegan.vegan); - const hasActivist = user.roles.cache.has(IDs.roles.vegan.activist); - const hasPlus = user.roles.cache.has(IDs.roles.vegan.plus); - const hasNotVegan = user.roles.cache.has(IDs.roles.nonvegan.nonvegan); - const hasVegCurious = user.roles.cache.has(IDs.roles.nonvegan.vegCurious); - const hasConvinced = user.roles.cache.has(IDs.roles.nonvegan.convinced); - const hasTrusted = user.roles.cache.has(IDs.roles.trusted); - const hasMuted = user.roles.cache.has(IDs.roles.restrictions.muted); - - // Create the user in the database - await prisma.user.create({ - data: { - id: user.id, - vegan: hasVegan, - trusted: hasTrusted, - activist: hasActivist, - plus: hasPlus, - notVegan: hasNotVegan, - vegCurious: hasVegCurious, - convinced: hasConvinced, - muted: hasMuted, - }, - }); - - // Close the database connection - await prisma.$disconnect(); -} diff --git a/src/utils/verificationConfig.ts b/src/utils/verificationConfig.ts new file mode 100644 index 0000000..0416129 --- /dev/null +++ b/src/utils/verificationConfig.ts @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2022 Anthony Berg + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +// eslint-disable-next-line import/prefer-default-export +export const maxVCs = 10;