From f89d6c07306a467fee2a46e450a1dc20fea02ca7 Mon Sep 17 00:00:00 2001 From: Anthony Date: Sun, 28 Aug 2022 02:42:04 +0100 Subject: [PATCH] feat(verification): add schedule with verify block --- src/listeners/verification/leaveVC.ts | 15 ++++++-- src/scheduled-tasks/verifyUnblock.ts | 54 +++++++++++++++++++++++++++ src/utils/database/verification.ts | 27 ++++++++++++++ 3 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 src/scheduled-tasks/verifyUnblock.ts diff --git a/src/listeners/verification/leaveVC.ts b/src/listeners/verification/leaveVC.ts index 7c67514..8564ebb 100644 --- a/src/listeners/verification/leaveVC.ts +++ b/src/listeners/verification/leaveVC.ts @@ -22,10 +22,10 @@ import type { VoiceState, CategoryChannel, VoiceChannel, TextChannel, } from 'discord.js'; import { maxVCs } from '../../utils/verificationConfig'; -import { getUser } from '../../utils/database/verification'; +import { getUser, checkFinish } from '../../utils/database/verification'; import IDs from '../../utils/ids'; -export class VerificationLeaveVCListener extends Listener { +class VerificationLeaveVCListener extends Listener { public constructor(context: Listener.Context, options: Listener.Options) { super(context, { ...options, @@ -82,9 +82,14 @@ export class VerificationLeaveVCListener extends Listener { */ // Remove verify as vegan and give non vegan role - if (!user.roles.cache.has(IDs.roles.vegan.vegan)) { + if (!await checkFinish(channel.id)) { await user.roles.remove(IDs.roles.verifyingAsVegan); - await user.roles.add(IDs.roles.nonvegan.nonvegan); + await user.roles.add([ + IDs.roles.nonvegan.nonvegan, + IDs.roles.verifyBlock, + ]); + // @ts-ignore + this.container.tasks.create('verifyUnblock', { userId: user.id, guildId: guild.id }, 30000); } } @@ -153,3 +158,5 @@ export class VerificationLeaveVCListener extends Listener { ]); } } + +export default VerificationLeaveVCListener; diff --git a/src/scheduled-tasks/verifyUnblock.ts b/src/scheduled-tasks/verifyUnblock.ts new file mode 100644 index 0000000..351c20e --- /dev/null +++ b/src/scheduled-tasks/verifyUnblock.ts @@ -0,0 +1,54 @@ +// 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 { ScheduledTask } from '@sapphire/plugin-scheduled-tasks'; +import IDs from '../utils/ids'; + +export class VerifyUnblock extends ScheduledTask { + public constructor(context: ScheduledTask.Context, options: ScheduledTask.Options) { + super(context, options); + } + + public async run(payload: { userId: string, guildId: string }) { + // Get the guild where the user is in + const guild = this.container.client.guilds.cache.get(payload.guildId); + if (guild === undefined) { + console.error('verifyUnblock: Guild not found!'); + return; + } + + // Find GuildMember for the user + const user = guild.members.cache.get(payload.userId); + if (user === undefined) { + console.error('verifyUnblock: GuildMember not found!'); + return; + } + + // Remove the 'verify block' role + await user.roles.remove(IDs.roles.verifyBlock); + } +} + +declare module '@sapphire/plugin-scheduled-tasks' { + interface ScheduledTasks { + verifyUnblock: never; + } +} + +export default VerifyUnblock; diff --git a/src/utils/database/verification.ts b/src/utils/database/verification.ts index e533f92..554c900 100644 --- a/src/utils/database/verification.ts +++ b/src/utils/database/verification.ts @@ -141,3 +141,30 @@ export async function finishVerification( // Close database connection await prisma.$disconnect(); } + +// Checks if verification was complete +export async function checkFinish(channelId: string) { + // Initialises Prisma Client + const prisma = new PrismaClient(); + + // Get the snowflake of the user verifying + const finish = await prisma.verify.findUnique({ + where: { + id: channelId, + }, + select: { + finishTime: true, + }, + }); + + // Close database connection + await prisma.$disconnect(); + + // Checks if query returned is null + if (finish === null) { + return false; + } + + // Return if a finish time has been set meaning verification is complete + return finish.finishTime !== null; +}