From a9d3e6405b9bd8ab25822380a3655f376046d3cf Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 14 Jul 2022 02:45:59 +0100 Subject: [PATCH 1/2] feat(database): add new database checks and ids for roles --- prisma/schema.prisma | 2 +- src/utils/dbExistingUser.ts | 85 +++++++++++++++++++++++++++++++++++++ src/utils/ids.ts | 32 ++++++++++++++ tsconfig.json | 6 ++- 4 files changed, 122 insertions(+), 3 deletions(-) create mode 100644 src/utils/dbExistingUser.ts create mode 100644 src/utils/ids.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 0201dc1..8bbdc5d 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -32,11 +32,11 @@ model User { balance Int @default(0) lastDaily DateTime? vegan Boolean @default(false) + trusted Boolean @default(false) activist Boolean @default(false) plus Boolean @default(false) vegCurious Boolean @default(false) convinced Boolean @default(false) - trusted Boolean @default(false) muted Boolean @default(false) VerifyUser Verify[] @relation("user") VerifyVerifier Verify[] @relation("verifier") diff --git a/src/utils/dbExistingUser.ts b/src/utils/dbExistingUser.ts new file mode 100644 index 0000000..7acb131 --- /dev/null +++ b/src/utils/dbExistingUser.ts @@ -0,0 +1,85 @@ +// 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 userExists = 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. + if (userExists > 0) { + return true; + } + return false; +} + +// 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 userExists = await prisma.user.count({ + where: { + id: user.id, + }, + }); + + // If the user is already in the database + if (userExists > 0) { + return; + } + + // Checks what roles the user has + const hasVegan = user.roles.cache.has(IDs.roles.vegan); + const hasActivist = user.roles.cache.has(IDs.roles.activist); + const hasPlus = user.roles.cache.has(IDs.roles.plus); + const hasVegCurious = user.roles.cache.has(IDs.roles.vegCurious); + const hasConvinced = user.roles.cache.has(IDs.roles.convinced); + const hasTrusted = user.roles.cache.has(IDs.roles.trusted); + const hasMuted = user.roles.cache.has(IDs.roles.trusted); + + // Create the user in the database + await prisma.user.create({ + data: { + id: user.id, + vegan: hasVegan, + trusted: hasTrusted, + activist: hasActivist, + plus: hasPlus, + vegCurious: hasVegCurious, + convinced: hasConvinced, + muted: hasMuted, + }, + }); +} diff --git a/src/utils/ids.ts b/src/utils/ids.ts new file mode 100644 index 0000000..98840d5 --- /dev/null +++ b/src/utils/ids.ts @@ -0,0 +1,32 @@ +/* + 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 . +*/ + +const IDs = { + roles: { + nonvegan: '774763753308815400', + vegan: '788114978020392982', + activist: '730915638746546257', + plus: '798682625619132428', + vegCurious: '832656046572961803', + convinced: '797132019166871612', + trusted: '731563158011117590', + muted: '730924813681688596', + }, +}; + +export { IDs }; diff --git a/tsconfig.json b/tsconfig.json index fc65cb1..e2256ed 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -29,8 +29,10 @@ "module": "commonjs", /* Specify what module code is generated. */ "rootDir": "src", /* Specify the root folder within your source files. */ // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ - // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ - // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + //"baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + //"paths": { + // "@utils/*": ["./src/utils/*"] + //}, /* Specify a set of entries that re-map imports to additional lookup locations. */ // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ // "types": [], /* Specify type package names to be included without being referenced in a source file. */ From 753fdfb57c50ce09400504b95153803c0693818d Mon Sep 17 00:00:00 2001 From: Anthony Date: Thu, 14 Jul 2022 02:46:48 +0100 Subject: [PATCH 2/2] fix(arabot): let normal and slash commands be run at the same time --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index faa5d78..e97f2ca 100644 --- a/src/index.ts +++ b/src/index.ts @@ -27,6 +27,7 @@ require('dotenv').config(); // Setting up the Sapphire client const client = new SapphireClient({ defaultPrefix: process.env.DEFAULT_PREFIX, + loadMessageCommandListeners: true, logger: { level: LogLevel.Debug, },