From 768ac13f8b88291e3a7adf17ef68ba118e6db747 Mon Sep 17 00:00:00 2001 From: Anthony Date: Sat, 11 Nov 2023 19:23:31 +0100 Subject: [PATCH] feat(db): add counting listener for a counting channel --- src/listeners/counting.ts | 86 ++++++++++++++++++++++++++++++++++ src/utils/database/counting.ts | 30 ++++++++++++ src/utils/devIDs.ts | 3 ++ src/utils/ids.ts | 3 ++ 4 files changed, 122 insertions(+) create mode 100644 src/listeners/counting.ts create mode 100644 src/utils/database/counting.ts diff --git a/src/listeners/counting.ts b/src/listeners/counting.ts new file mode 100644 index 0000000..8d33a03 --- /dev/null +++ b/src/listeners/counting.ts @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +/* + Animal Rights Advocates Discord Bot + Copyright (C) 2023 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 . + + I developed this whilst I was on holiday in Norway for some reason +*/ + +import { Listener } from '@sapphire/framework'; +import type { Message } from 'discord.js'; +import { getLastCount, addCount } from '#utils/database/counting'; +import IDs from '#utils/ids'; + +export class XpListener extends Listener { + public constructor(context: Listener.Context, options: Listener.Options) { + super(context, { + ...options, + event: 'messageCreate', + }); + } + + public async run(message: Message) { + if (message.channelId !== IDs.channels.misc.counting) return; + + const user = message.author; + + if (user.bot) { + return; + } + + let lastCount = await getLastCount(); + + // If no counts exist on the database, then create the first count from the bot + if (lastCount === null) { + if (this.container.client.id === null) { + message.channel.send('An unexpected error occurred trying to set up the counting channel, please contact a developer!'); + return; + } + + const countUserId = this.container.client.id; + const countNumber = 0; + + // Adds the bot as the first person with the count + await addCount(countUserId, countNumber); + + lastCount = await getLastCount(); + if (lastCount === null) { + message.channel.send('An unexpected error occurred, please contact a developer!'); + return; + } + } + + // Checks if it is the same user that counted since last time + if (message.author.id === lastCount.userId) { + return; + } + + const regex = new RegExp(`^${lastCount.number + 1}\\b`); + + // Checks if the count was correct + if (regex.test(message.content)) { + await addCount(message.author.id, lastCount.number + 1); + await message.react('✅'); + return; + } + + // If the count was not correct, restart it + await addCount(message.author.id, 0); + await message.react('❌'); + await message.reply(`${message.author} counted incorrectly! The count got up to ${lastCount.number}! ` + + 'The count has been reset and the next number is 1'); + } +} diff --git a/src/utils/database/counting.ts b/src/utils/database/counting.ts new file mode 100644 index 0000000..f4af13b --- /dev/null +++ b/src/utils/database/counting.ts @@ -0,0 +1,30 @@ +import { container } from '@sapphire/framework'; +import type { Snowflake } from 'discord.js'; + +export async function getLastCount() { + const count = await container.database.counting.findFirst({ + orderBy: { + id: 'desc', + }, + }); + + return count; +} + +export async function addCount(userId: Snowflake, number: number) { + await container.database.counting.create({ + data: { + number, + user: { + connectOrCreate: { + where: { + id: userId, + }, + create: { + id: userId, + }, + }, + }, + }, + }); +} diff --git a/src/utils/devIDs.ts b/src/utils/devIDs.ts index 2b34a2a..96bb6b2 100644 --- a/src/utils/devIDs.ts +++ b/src/utils/devIDs.ts @@ -110,6 +110,9 @@ const devIDs = { potgm: '999431679053660189', disabilities: '999431679527628810', }, + misc: { + counting: '1172905446227595314', + }, restricted: { welcome: '999431679812845655', moderators: '999431679812845656', diff --git a/src/utils/ids.ts b/src/utils/ids.ts index 01299ef..95e3d87 100644 --- a/src/utils/ids.ts +++ b/src/utils/ids.ts @@ -113,6 +113,9 @@ let IDs = { potgm: '956224095509442600', disabilities: '933078769365823518', }, + misc: { + counting: '', + }, restricted: { welcome: '992060036312481874', moderators: '928349536395604029',