mirror of
https://github.com/veganhacktivists/arabot.git
synced 2025-11-03 03:39:48 +01:00
feat(arabot): add text channel to verification
This commit is contained in:
parent
30729a565f
commit
0d571dd923
@ -18,13 +18,12 @@
|
||||
*/
|
||||
|
||||
import { container, Listener } from '@sapphire/framework';
|
||||
import type {
|
||||
TextChannel, VoiceChannel, CategoryChannel, VoiceState,
|
||||
} from 'discord.js';
|
||||
import type { VoiceChannel, CategoryChannel, VoiceState } from 'discord.js';
|
||||
import { maxVCs } from '../../utils/verificationConfig';
|
||||
import { joinVerification, startVerification } from '../../utils/database/verification';
|
||||
import IDs from '../../utils/ids';
|
||||
|
||||
export class VerificationJoinVCListener extends Listener {
|
||||
export default class VerificationJoinVCListener extends Listener {
|
||||
public constructor(context: Listener.Context, options: Listener.Options) {
|
||||
super(context, {
|
||||
...options,
|
||||
@ -40,6 +39,9 @@ export class VerificationJoinVCListener extends Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
// Variable if this channel is a Verifiers only VC
|
||||
let verifier = false;
|
||||
|
||||
// Checks if a verifier has joined
|
||||
if (newState.channel.members.size === 2) {
|
||||
await newState.channel!.permissionOverwrites.set([
|
||||
@ -51,31 +53,72 @@ export class VerificationJoinVCListener extends Listener {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if a verifier joined a verification VC and update database
|
||||
if (newState.channel.members.size === 2) {
|
||||
if (!newState.channel.name.includes(' - Verification')) {
|
||||
return;
|
||||
}
|
||||
|
||||
await startVerification(newState.member!, newState.channelId!);
|
||||
return;
|
||||
}
|
||||
|
||||
// Checks if there is more than one person who has joined or if the channel has members
|
||||
if (newState.channel.members.size !== 1
|
||||
|| !newState.channel.members.has(newState.member!.id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO add database information
|
||||
|
||||
const channel = newState.channel!;
|
||||
const { client } = container;
|
||||
const guild = client.guilds.cache.get(newState.guild.id)!;
|
||||
const currentChannel = guild.channels.cache.get(newState.channel.id) as VoiceChannel;
|
||||
const currentChannel = guild.channels.cache.get(newState.channelId!) as VoiceChannel;
|
||||
|
||||
// Check if the user has the verifiers role
|
||||
if (newState.member?.roles.cache.has(IDs.roles.staff.verifier)) { // TODO add check if they are trial-verifiers
|
||||
if (newState.member?.roles.cache.has(IDs.roles.staff.verifier)
|
||||
|| newState.member?.roles.cache.has(IDs.roles.staff.trialVerifier)) {
|
||||
await channel.setName('Verifier Meeting');
|
||||
verifier = true;
|
||||
} else {
|
||||
await channel.setName(`${newState.member?.displayName} - Verification`);
|
||||
await currentChannel.send(`Hiya ${newState.member?.user}, please be patient as a verifier has been called out to verify you.\n\nIf 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(newState.member!, channel.id);
|
||||
}
|
||||
|
||||
// Check how many voice channels there are
|
||||
const category = guild.channels.cache.get(IDs.categories.verification) as CategoryChannel;
|
||||
const listVoiceChannels = category.children.filter((c) => c.type === 'GUILD_VOICE');
|
||||
|
||||
// Create a text channel for verifiers only
|
||||
// Checks if there are more than 10 voice channels
|
||||
if (!verifier) {
|
||||
const verificationText = await guild.channels.create(`✅┃${newState.member?.displayName}-verification`, {
|
||||
type: 'GUILD_TEXT',
|
||||
topic: `Channel for verifiers only. ${newState.member?.id} (Please do not change this)`,
|
||||
parent: IDs.categories.verification,
|
||||
userLimit: 1,
|
||||
permissionOverwrites: [
|
||||
{
|
||||
id: guild.roles.everyone,
|
||||
deny: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
|
||||
},
|
||||
{
|
||||
id: IDs.roles.verifyBlock,
|
||||
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES'],
|
||||
},
|
||||
{
|
||||
id: IDs.roles.staff.verifier,
|
||||
allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
// Send a message that someone wants to be verified
|
||||
await verificationText.send(`${newState.member?.user} wants to be verified in ${newState.channel}
|
||||
\n<@&${IDs.roles.staff.verifier}> <@&${IDs.roles.staff.trialVerifier}>`);
|
||||
}
|
||||
|
||||
// Create a new channel for others to join
|
||||
|
||||
// Checks if there are more than 10 voice channels
|
||||
@ -89,6 +132,10 @@ export class VerificationJoinVCListener extends Listener {
|
||||
id: guild.roles.everyone,
|
||||
deny: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
|
||||
},
|
||||
{
|
||||
id: IDs.roles.verifyBlock,
|
||||
deny: ['VIEW_CHANNEL', 'CONNECT', 'SEND_MESSAGES'],
|
||||
},
|
||||
{
|
||||
id: IDs.roles.verifyingAsVegan,
|
||||
allow: ['VIEW_CHANNEL'],
|
||||
@ -110,6 +157,10 @@ export class VerificationJoinVCListener extends Listener {
|
||||
id: guild.roles.everyone,
|
||||
deny: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
|
||||
},
|
||||
{
|
||||
id: IDs.roles.verifyBlock,
|
||||
deny: ['VIEW_CHANNEL', 'CONNECT', 'SEND_MESSAGES'],
|
||||
},
|
||||
{
|
||||
id: IDs.roles.verifyingAsVegan,
|
||||
allow: ['VIEW_CHANNEL'],
|
||||
@ -138,9 +189,5 @@ export class VerificationJoinVCListener extends Listener {
|
||||
},
|
||||
]);
|
||||
await currentChannel.setUserLimit(0);
|
||||
|
||||
// Send a message that someone wants to be verified
|
||||
const verifyChannel = client.channels.cache.get(IDs.channels.staff.verifiers) as TextChannel;
|
||||
await verifyChannel.send(`${newState.member?.user} wants to be verified in ${newState.channel}`);
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,8 +18,11 @@
|
||||
*/
|
||||
|
||||
import { container, Listener } from '@sapphire/framework';
|
||||
import type { VoiceState, CategoryChannel, VoiceChannel } from 'discord.js';
|
||||
import type {
|
||||
VoiceState, CategoryChannel, VoiceChannel, TextChannel,
|
||||
} from 'discord.js';
|
||||
import { maxVCs } from '../../utils/verificationConfig';
|
||||
import { getUser } from '../../utils/database/verification';
|
||||
import IDs from '../../utils/ids';
|
||||
|
||||
export class VerificationLeaveVCListener extends Listener {
|
||||
@ -42,12 +45,30 @@ export class VerificationLeaveVCListener extends Listener {
|
||||
// Allow more people to join VC if there are less than 10 VCs
|
||||
const { client } = container;
|
||||
const guild = client.guilds.cache.get(newState.guild.id)!;
|
||||
const user = guild.members.cache.get(oldState.member!.id)!;
|
||||
let verifier = false;
|
||||
|
||||
// Remove verify as vegan and give non vegan role
|
||||
if (!user.roles.cache.has(IDs.roles.vegan.vegan)) {
|
||||
await user.roles.remove(IDs.roles.verifyingAsVegan);
|
||||
await user.roles.add(IDs.roles.nonvegan.nonvegan);
|
||||
// Get the user that was being verified
|
||||
const userSnowflake = await getUser(oldState.channel.id);
|
||||
if (userSnowflake === null) {
|
||||
verifier = true;
|
||||
}
|
||||
|
||||
if (!verifier) {
|
||||
console.log(userSnowflake);
|
||||
const user = guild.members.cache.get(userSnowflake!)!;
|
||||
|
||||
/*
|
||||
// Add the user to the database if it's not a verifier meeting
|
||||
if (!oldState.channel.name.includes(' - Verification')) {
|
||||
await finishVerification(oldState.channelId!, true, true, false, false);
|
||||
}
|
||||
*/
|
||||
|
||||
// Remove verify as vegan and give non vegan role
|
||||
if (!user.roles.cache.has(IDs.roles.vegan.vegan)) {
|
||||
await user.roles.remove(IDs.roles.verifyingAsVegan);
|
||||
await user.roles.add(IDs.roles.nonvegan.nonvegan);
|
||||
}
|
||||
}
|
||||
|
||||
// Check how many voice channels there are
|
||||
@ -60,6 +81,19 @@ export class VerificationLeaveVCListener extends Listener {
|
||||
await oldState.channel!.delete();
|
||||
}
|
||||
|
||||
// Delete text channel
|
||||
if (!verifier) {
|
||||
// Gets a list of all the text channels in the verification category
|
||||
const listTextChannels = category.children.filter((c) => c.type === 'GUILD_TEXT');
|
||||
listTextChannels.forEach((c) => {
|
||||
const textChannel = c as TextChannel;
|
||||
// Checks if the channel topic has the user's snowflake
|
||||
if (textChannel.topic!.includes(userSnowflake!)) {
|
||||
textChannel.delete();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// If there are no VCs left in verification after having the channel deleted
|
||||
if (listVoiceChannels.size === 0) {
|
||||
// Create a verification channel
|
||||
@ -72,6 +106,10 @@ export class VerificationLeaveVCListener extends Listener {
|
||||
id: guild.roles.everyone,
|
||||
deny: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
|
||||
},
|
||||
{
|
||||
id: IDs.roles.verifyBlock,
|
||||
deny: ['VIEW_CHANNEL', 'CONNECT', 'SEND_MESSAGES'],
|
||||
},
|
||||
{
|
||||
id: IDs.roles.verifyingAsVegan,
|
||||
allow: ['VIEW_CHANNEL'],
|
||||
|
||||
@ -19,7 +19,7 @@
|
||||
|
||||
import type { GuildMember, GuildMemberRoleManager } from 'discord.js';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { IDs } from './ids';
|
||||
import IDs from '../ids';
|
||||
|
||||
// Checks if the user exists on the database
|
||||
export async function userExists(user: GuildMember) {
|
||||
@ -131,3 +131,25 @@ export async function updateUser(user: GuildMember) {
|
||||
// Close the database connection
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
|
||||
export async function fetchRoles(user: string) {
|
||||
// Initialises Prisma Client
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// Get the user's roles
|
||||
const roles = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: user,
|
||||
},
|
||||
select: {
|
||||
trusted: true,
|
||||
plus: true,
|
||||
vegCurious: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Close the database connection
|
||||
await prisma.$disconnect();
|
||||
|
||||
return roles;
|
||||
}
|
||||
153
src/utils/database/verification.ts
Normal file
153
src/utils/database/verification.ts
Normal file
@ -0,0 +1,153 @@
|
||||
// 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 type { GuildMember } from 'discord.js';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { updateUser } from './dbExistingUser';
|
||||
|
||||
export async function joinVerification(user: GuildMember, channelId: string) {
|
||||
// Update the user on the database with the current roles they have
|
||||
await updateUser(user);
|
||||
|
||||
// Initialises Prisma Client
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
await prisma.verify.create({
|
||||
data: {
|
||||
id: channelId,
|
||||
user: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Close database connection
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
|
||||
export async function startVerification(verifier: GuildMember, channelId: string) {
|
||||
// Initialises Prisma Client
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
await prisma.verify.update({
|
||||
where: {
|
||||
id: channelId,
|
||||
},
|
||||
data: {
|
||||
verifier: {
|
||||
connect: {
|
||||
id: verifier.id,
|
||||
},
|
||||
},
|
||||
startTime: new Date(),
|
||||
},
|
||||
});
|
||||
|
||||
// Close database connection
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
|
||||
export async function getUser(channelId: string) {
|
||||
// Initialises Prisma Client
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
// Get the snowflake of the user verifying
|
||||
const user = await prisma.verify.findUnique({
|
||||
where: {
|
||||
id: channelId,
|
||||
},
|
||||
select: {
|
||||
userId: true,
|
||||
},
|
||||
});
|
||||
|
||||
// Close database connection
|
||||
await prisma.$disconnect();
|
||||
|
||||
// Check the user could be found
|
||||
if (user === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Return the user's snowflake
|
||||
return user.userId;
|
||||
}
|
||||
|
||||
export async function finishVerification(
|
||||
channelId: string,
|
||||
timedOut: boolean,
|
||||
vegan: boolean,
|
||||
text:boolean,
|
||||
serverVegan: 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,
|
||||
},
|
||||
});
|
||||
|
||||
// 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);
|
||||
*/
|
||||
}
|
||||
@ -41,15 +41,18 @@ export const devIDs = {
|
||||
staff: {
|
||||
coordinator: '999431675165556822',
|
||||
devCoordinator: '999431675165556818',
|
||||
diversityCoordinator: '999431675140382808',
|
||||
mentorCoordinator: '999431675140382809',
|
||||
verifierCoordinator: '999431675140382810',
|
||||
restricted: '999431675123597407',
|
||||
moderator: '999431675123597408',
|
||||
verifier: '999431675123597406',
|
||||
trialVerifier: '999431675123597405',
|
||||
},
|
||||
patron: '999431675098447935',
|
||||
patreon: '999431675098447936',
|
||||
verifyingAsVegan: '999431675081666597',
|
||||
verifyBlock: '1007477161835372574',
|
||||
},
|
||||
channels: {
|
||||
staff: {
|
||||
|
||||
@ -50,10 +50,12 @@ let IDs = {
|
||||
restricted: '851624392928264222',
|
||||
moderator: '826157475815489598',
|
||||
verifier: '871802735031373856',
|
||||
trialVerifier: '982635638010572850',
|
||||
},
|
||||
patron: '765370219207852055',
|
||||
patreon: '993848684640997406',
|
||||
verifyingAsVegan: '854725899576279060',
|
||||
verifyBlock: '',
|
||||
},
|
||||
channels: {
|
||||
staff: {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user