feat(verification): add schedule with verify block

This commit is contained in:
Anthony 2022-08-28 02:42:04 +01:00
parent 63bce0d405
commit f89d6c0730
3 changed files with 92 additions and 4 deletions

View File

@ -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;

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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;

View File

@ -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;
}