From d3594be3ef773f1a25600f1eb18504979008c06b Mon Sep 17 00:00:00 2001 From: smyalygames Date: Fri, 17 Mar 2023 00:31:10 +0000 Subject: [PATCH] feat(arabot): add command to remove verify timeout --- .../verification/verifyTimeoutRemove.ts | 92 +++++++++++++++++++ src/utils/database/verification.ts | 21 +++++ 2 files changed, 113 insertions(+) create mode 100644 src/commands/verification/verifyTimeoutRemove.ts diff --git a/src/commands/verification/verifyTimeoutRemove.ts b/src/commands/verification/verifyTimeoutRemove.ts new file mode 100644 index 0000000..b24785a --- /dev/null +++ b/src/commands/verification/verifyTimeoutRemove.ts @@ -0,0 +1,92 @@ +// 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 . +*/ + +import { Command, RegisterBehavior } from '@sapphire/framework'; +import IDs from '#utils/ids'; +import { checkVerificationFinish } from '#utils/database/verification'; + +export class VerifyTimeoutRemoveCommand extends Command { + public constructor(context: Command.Context, options: Command.Options) { + super(context, { + ...options, + name: 'verifytimeoutremove', + description: 'Remove the verify timeout role', + preconditions: ['VerifierOnly'], + }); + } + + // Registers that this is a slash command + public override registerApplicationCommands(registry: Command.Registry) { + registry.registerChatInputCommand( + (builder) => builder + .setName(this.name) + .setDescription(this.description) + .addUserOption((option) => option.setName('user') + .setDescription('User to remove timeout from') + .setRequired(true)), + { + behaviorWhenNotIdentical: RegisterBehavior.Overwrite, + }, + ); + } + + // Command run + public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { + // Get the arguments + const user = interaction.options.getUser('user', true); + const { guild } = interaction; + + // Checks if all the variables are of the right type + if (guild === null) { + await interaction.reply({ + content: 'Error fetching guild!', + ephemeral: true, + fetchReply: true, + }); + return; + } + + await interaction.deferReply({ ephemeral: true }); + + let member = guild.members.cache.get(user.id); + + if (member === undefined) { + member = await guild.members.fetch(user.id) + .catch(undefined); + if (member === undefined) { + await interaction.editReply(`${user} is not on this server!`); + return; + } + } + + if (!member.roles.cache.has(IDs.roles.verifyBlock)) { + await interaction.editReply(`${user} is not blocked from verification!`); + return; + } + + if (await checkVerificationFinish(user.id)) { + await interaction.editReply(`Can't remove ${user}'s role as they failed their last verification`); + return; + } + + await member.roles.remove(IDs.roles.verifyBlock); + + await interaction.editReply(`Removed ${user}'s verification block`); + } +} diff --git a/src/utils/database/verification.ts b/src/utils/database/verification.ts index 83978c0..61d7dfc 100644 --- a/src/utils/database/verification.ts +++ b/src/utils/database/verification.ts @@ -229,3 +229,24 @@ export async function blockTime(userId: Snowflake) { // Creates the length of the time for the ban return (fibonacci(count) * 3600_000) - timeOff; } + +// Checks if the last verification was completed +export async function checkVerificationFinish(userId: Snowflake) { + const verification = await container.database.verify.findFirst({ + where: { + userId, + }, + orderBy: { + id: 'desc', + }, + select: { + finishTime: true, + }, + }); + + if (verification === null) { + return false; + } + + return verification.finishTime !== null; +}