feat(verification): confirm add roles

This commit is contained in:
Anthony 2022-08-27 02:48:08 +01:00
parent b71a7ef2ea
commit 0043fdddfb
3 changed files with 158 additions and 54 deletions

View File

@ -57,10 +57,22 @@ model Verify {
startTime DateTime?
finishTime DateTime?
timedOut Boolean @default(false) // If they got kicked out of verification because they timed out
//incomplete Boolean @default(false) // If the verification was incomplete
//complete Boolean @default(false) // If the verification was incomplete
// Roles they got from verification
vegan Boolean @default(false) // If they got verified as a vegan
activist Boolean @default(false) // If they got the activist role when they verified
trusted Boolean @default(false) // If they got the trusted role when they verified
vegCurious Boolean @default(false) // If they got the Veg Curious role
convinced Boolean @default(false)
text Boolean @default(false) // If they used text verification
serverVegan Boolean @default(false) // People that went vegan on the server
// Stats on verification
reason Int?
where Int?
length Int?
reasoning Int?
life Int?
food Int?
notes String?
}

View File

@ -24,6 +24,7 @@ import type {
TextChannel,
VoiceChannel,
VoiceState,
GuildMember,
} from 'discord.js';
import {
ButtonInteraction,
@ -33,7 +34,7 @@ import {
MessageEmbed,
} from 'discord.js';
import { maxVCs, questionInfo, serverFind } from '../../utils/verificationConfig';
import { joinVerification, startVerification } from '../../utils/database/verification';
import { joinVerification, startVerification, finishVerification } from '../../utils/database/verification';
import IDs from '../../utils/ids';
class VerificationJoinVCListener extends Listener {
@ -93,7 +94,7 @@ class VerificationJoinVCListener extends Listener {
return;
}
await startVerification(member, channel.id);
await startVerification(channel.id);
return;
}
@ -113,7 +114,7 @@ class VerificationJoinVCListener extends Listener {
await currentChannel.send(`Hiya ${member.user}, please be patient as a verifier has been called out to verify you.\n\n`
+ 'If you leave this voice channel, you will automatically be given the non-vegan role where you gain access to this server and if you\'d like to verify as a vegan again, you\'d have to contact a Mod, which could be done via ModMail.');
// Adds to the database that the user joined verification
await joinVerification(member, channel.id);
await joinVerification(channel.id, member);
}
// Check how many voice channels there are
@ -147,7 +148,7 @@ class VerificationJoinVCListener extends Listener {
await verificationText.send(`${member.user} wants to be verified in ${channel}
\n<@&${IDs.roles.staff.verifier}> <@&${IDs.roles.staff.trialVerifier}>`);
await this.verificationProcess(verificationText);
await this.verificationProcess(verificationText, channel.id, member);
}
// Create a new channel for others to join
@ -224,6 +225,8 @@ class VerificationJoinVCListener extends Listener {
private async verificationProcess(
channel: TextChannel,
verId: string,
user: GuildMember,
) {
const embedColor = '#0099ff';
const info = {
@ -354,6 +357,50 @@ class VerificationJoinVCListener extends Listener {
components: buttons,
});
}
// Confirmation to give roles to the user being verified
if (info.page === questionLength) {
// Create embed with all the roles the user has
embed = new MessageEmbed()
.setColor(embedColor)
.setTitle(`Give these roles to ${user.displayName}?`)
.setThumbnail(user.avatarURL()!)
.addFields(
{ name: 'Roles:', value: this.getTextRoles(info.roles) },
);
// Create buttons for input
buttons = [new MessageActionRow<MessageButton>()
.addComponents(
new MessageButton()
.setCustomId('confirm')
.setLabel('Yes')
.setStyle(Constants.MessageButtonStyles.SUCCESS),
new MessageButton()
.setCustomId('cancel')
.setLabel('No')
.setStyle(Constants.MessageButtonStyles.DANGER),
)];
await message.edit({
embeds: [embed],
components: buttons,
});
}
}
// Confirming and finishing the verification
if (button.customId === 'confirm' && info.page >= questionLength) {
await finishVerification(verId, info);
await this.giveRoles(user, info.roles);
embed = new MessageEmbed()
.setColor('#34c000')
.setTitle(`Successfully verified ${user.displayName}!`)
.setThumbnail(user.avatarURL()!)
.addFields(
{ name: 'Roles:', value: this.getTextRoles(info.roles) },
);
await message.edit({
embeds: [embed],
components: [],
});
}
});
}
@ -392,6 +439,67 @@ class VerificationJoinVCListener extends Listener {
}
return parseInt(buttonChoice, 10);
}
private getTextRoles(
roles: {
vegan: boolean,
activist: boolean,
trusted: boolean,
vegCurious: boolean,
convinced: boolean
},
) {
let rolesText = '';
if (roles.convinced) {
rolesText += `<@&${IDs.roles.nonvegan.convinced}>`;
}
if (roles.vegan) {
rolesText += `<@&${IDs.roles.vegan.vegan}>`;
} else {
rolesText += `<@&${IDs.roles.nonvegan.nonvegan}>`;
}
if (roles.activist) {
rolesText += `<@&${IDs.roles.vegan.activist}>`;
}
if (roles.trusted) {
rolesText += `<@&${IDs.roles.trusted}>`;
}
if (roles.vegCurious) {
rolesText += `<@&${IDs.roles.nonvegan.vegCurious}>`;
}
return rolesText;
}
private async giveRoles(
user: GuildMember,
roles: {
vegan: boolean,
activist: boolean,
trusted: boolean,
vegCurious: boolean,
convinced: boolean
},
) {
const rolesAdd = [];
if (roles.convinced) {
rolesAdd.push(IDs.roles.nonvegan.convinced);
}
if (roles.vegan) {
rolesAdd.push(IDs.roles.vegan.vegan);
} else {
rolesAdd.push(IDs.roles.nonvegan.nonvegan);
}
if (roles.activist) {
rolesAdd.push(IDs.roles.vegan.activist);
}
if (roles.trusted) {
rolesAdd.push(IDs.roles.trusted);
}
if (roles.vegCurious) {
rolesAdd.push(IDs.roles.nonvegan.vegCurious);
}
await user.roles.add(rolesAdd);
}
}
export default VerificationJoinVCListener;

View File

@ -21,7 +21,7 @@ import type { GuildMember } from 'discord.js';
import { PrismaClient } from '@prisma/client';
import { updateUser } from './dbExistingUser';
export async function joinVerification(user: GuildMember, channelId: string) {
export async function joinVerification(channelId: string, user: GuildMember) {
// Update the user on the database with the current roles they have
await updateUser(user);
@ -43,7 +43,7 @@ export async function joinVerification(user: GuildMember, channelId: string) {
await prisma.$disconnect();
}
export async function startVerification(verifier: GuildMember, channelId: string) {
export async function startVerification(channelId: string) {
// Initialises Prisma Client
const prisma = new PrismaClient();
@ -52,11 +52,6 @@ export async function startVerification(verifier: GuildMember, channelId: string
id: channelId,
},
data: {
verifier: {
connect: {
id: verifier.id,
},
},
startTime: new Date(),
},
});
@ -93,61 +88,50 @@ export async function getUser(channelId: string) {
export async function finishVerification(
channelId: string,
timedOut: boolean,
vegan: boolean,
text:boolean,
serverVegan: boolean,
info: {
page: number,
find: {
reason: number,
where: number
},
length: number,
reasoning: number,
life: number,
food: number,
roles: {
vegan: boolean,
activist: boolean,
trusted: boolean,
vegCurious: boolean,
convinced: boolean
} },
) {
// Initialises Prisma Client
const prisma = new PrismaClient();
/*
const user = await prisma.verify.findUnique({
where: {
id: channelId,
},
select: {
userId: true,
},
});
*/
// TODO potentially add an incomplete tracker?
await prisma.verify.update({
where: {
id: channelId,
},
data: {
timedOut,
vegan,
text,
serverVegan,
finishTime: new Date(),
// Roles
vegan: info.roles.vegan,
activist: info.roles.activist,
trusted: info.roles.trusted,
vegCurious: info.roles.vegCurious,
convinced: info.roles.convinced,
// Statistics
reason: info.find.reason,
where: info.find.where,
length: info.length,
reasoning: info.reasoning,
life: info.life,
food: info.food,
},
});
// Close database connection
await prisma.$disconnect();
// TODO add a way to give roles back after adding the new verification
/*
const roles = await fetchRoles(user!.userId);
if (roles === null) {
return;
}
// Give roles to the user
const giveRoles = [];
if (roles.trusted) {
giveRoles.push(IDs.roles.trusted);
}
if (roles.plus) {
giveRoles.push(IDs.roles.vegan.plus);
}
if (roles.vegCurious) {
giveRoles.push(IDs.roles.nonvegan.vegCurious);
}
await user.roles.add(giveRoles);
*/
}