Update slashCommands/verify/verify.js

Adding my code changes to sync up together
This commit is contained in:
2024-02-04 08:56:50 +00:00
parent a3dcecf520
commit 13b5a8196c
+43 -22
View File
@@ -2,21 +2,22 @@ const { EmbedBuilder, ApplicationCommandType, Collection, AttachmentBuilder } =
const { createCanvas, loadImage } = require('canvas');
const cfg = require('../../config/config.js');
const captchaUser = new Collection();
module.exports = {
name: 'verify',
description: 'To verify yourself.',
type: ApplicationCommandType.ChatInput,
run: async (client, interaction) => {
const guildId = interaction.guild.id;
//functions here
async function generateCaptcha(count) {
var upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lowchars = "abcdefghijklmnopqrstuvwxyz";
var nums = "0123456789";
//get a random letter/number from upchars, lowchars, and nums
var chars = upchars + lowchars + nums;
var captcha = "";
for (var i = 0; i < count; i++) {
var c = Math.floor(Math.random() * 3);
if (c === 0) {
@@ -26,8 +27,8 @@ module.exports = {
} else {
captcha += nums.charAt(Math.floor(Math.random() * nums.length));
}
}
}
return captcha;
}
@@ -44,75 +45,95 @@ module.exports = {
return buffer;
}
//check if user is in the correct channel
if (interaction.channel.id !== cfg.guilds[guildId].channelId) {
return interaction.reply({ content: cfg.messages.wrongChannel, ephemeral: true })
}
//check if user has the roleToRemove
if (interaction.member.roles.cache.has(cfg.guilds[guildId].roleToAdd)) {
return interaction.reply({ content: cfg.messages.alreadyVerified, ephemeral: true })
}
const captcha = await generateCaptcha(cfg.captchaCount);
let attempts = 0;
const maxAttempts = 3;
let validated = false; // Flag to track if user is already validated
async function verifyUser(captcha) {
attempts++;
if (attempts > maxAttempts) {
await interaction.user.send(cfg.messages.maxAttemptsExceeded);
return;
}
const buff = await createCaptcha(captcha);
const attachment = new AttachmentBuilder(buff, { name: 'captcha.png' });
const embed = new EmbedBuilder()
.setTitle("Captcha Verification")
.setDescription(`Please type the captcha below. You have ${cfg.captchaTimeout / 1000} seconds to respond.`)
.setDescription(`Please type the captcha below. You have ${cfg.captchaTimeout / 1000} seconds to respond. Attempt ${attempts}/${maxAttempts}`)
.setColor('Green')
.setImage('attachment://captcha.png')
.setTimestamp();
if (attempts > 1) {
await interaction.user.send(cfg.messages.fail);
}
await interaction.user.send({
embeds: [embed], files: [attachment]
}).then(async (msg) => {
await interaction.reply({
content: cfg.messages.toVerify,
ephemeral: true
})
const filter = m => m.author.id === interaction.user.id;
const filter = m => !m.author.bot && m.author.id === interaction.user.id && m.channel.id === msg.channel.id;
const collector = msg.channel.createMessageCollector({
filter,
time: cfg.captchaTimeout
});
collector.on('collect', async (m) => {
if (m.content === captcha) {
//if member left the server before the verification is done
if (!interaction.guild.members.cache.has(interaction.user.id)) {
await interaction.user.send(cfg.messages.userLeft);
collector.stop();
} else {
if (m.content === captcha) {
validated = true;
await interaction.member.roles.add(cfg.guilds[guildId].roleToAdd);
await interaction.member.roles.remove(cfg.guilds[guildId].firstRole);
await interaction.user.send(cfg.messages.success);
return
} else {
// If the content doesn't match captcha
collector.stop();
}
} else {
await interaction.user.send(cfg.messages.fail);
collector.stop();
}
});
collector.on('end', async (collected, reason) => {
if (reason === 'time') {
if (reason === 'time' && !validated) { // Send timeout message only if not already validated
await interaction.user.send(cfg.messages.tookTooLong);
} else if (!validated) {
// Only generate a new captcha and attempt verification if attempts < maxAttempts
const newCaptcha = await generateCaptcha(cfg.captchaCount);
await verifyUser(newCaptcha);
}
});
}
).catch(async (err) => {
})
.catch(async (err) => {
await interaction.reply({
content: cfg.messages.unableToDM,
ephemeral: true
});
});
}
}
const captcha = await generateCaptcha(cfg.captchaCount);
verifyUser(captcha);
}
}
/*
Note from Naoki: