Update slashCommands/verify/verify.js
Adding my code changes to sync up together
This commit is contained in:
@@ -2,21 +2,22 @@ const { EmbedBuilder, ApplicationCommandType, Collection, AttachmentBuilder } =
|
|||||||
const { createCanvas, loadImage } = require('canvas');
|
const { createCanvas, loadImage } = require('canvas');
|
||||||
const cfg = require('../../config/config.js');
|
const cfg = require('../../config/config.js');
|
||||||
const captchaUser = new Collection();
|
const captchaUser = new Collection();
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'verify',
|
name: 'verify',
|
||||||
description: 'To verify yourself.',
|
description: 'To verify yourself.',
|
||||||
type: ApplicationCommandType.ChatInput,
|
type: ApplicationCommandType.ChatInput,
|
||||||
run: async (client, interaction) => {
|
run: async (client, interaction) => {
|
||||||
const guildId = interaction.guild.id;
|
const guildId = interaction.guild.id;
|
||||||
//functions here
|
|
||||||
async function generateCaptcha(count) {
|
async function generateCaptcha(count) {
|
||||||
|
|
||||||
var upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
var upchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
var lowchars = "abcdefghijklmnopqrstuvwxyz";
|
var lowchars = "abcdefghijklmnopqrstuvwxyz";
|
||||||
var nums = "0123456789";
|
var nums = "0123456789";
|
||||||
//get a random letter/number from upchars, lowchars, and nums
|
//get a random letter/number from upchars, lowchars, and nums
|
||||||
var chars = upchars + lowchars + nums;
|
var chars = upchars + lowchars + nums;
|
||||||
var captcha = "";
|
var captcha = "";
|
||||||
|
|
||||||
for (var i = 0; i < count; i++) {
|
for (var i = 0; i < count; i++) {
|
||||||
var c = Math.floor(Math.random() * 3);
|
var c = Math.floor(Math.random() * 3);
|
||||||
if (c === 0) {
|
if (c === 0) {
|
||||||
@@ -26,8 +27,8 @@ module.exports = {
|
|||||||
} else {
|
} else {
|
||||||
captcha += nums.charAt(Math.floor(Math.random() * nums.length));
|
captcha += nums.charAt(Math.floor(Math.random() * nums.length));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
return captcha;
|
return captcha;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,75 +45,95 @@ module.exports = {
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check if user is in the correct channel
|
|
||||||
if (interaction.channel.id !== cfg.guilds[guildId].channelId) {
|
if (interaction.channel.id !== cfg.guilds[guildId].channelId) {
|
||||||
return interaction.reply({ content: cfg.messages.wrongChannel, ephemeral: true })
|
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)) {
|
if (interaction.member.roles.cache.has(cfg.guilds[guildId].roleToAdd)) {
|
||||||
return interaction.reply({ content: cfg.messages.alreadyVerified, ephemeral: true })
|
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 buff = await createCaptcha(captcha);
|
||||||
|
|
||||||
|
|
||||||
const attachment = new AttachmentBuilder(buff, { name: 'captcha.png' });
|
const attachment = new AttachmentBuilder(buff, { name: 'captcha.png' });
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setTitle("Captcha Verification")
|
.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')
|
.setColor('Green')
|
||||||
.setImage('attachment://captcha.png')
|
.setImage('attachment://captcha.png')
|
||||||
.setTimestamp();
|
.setTimestamp();
|
||||||
|
if (attempts > 1) {
|
||||||
|
await interaction.user.send(cfg.messages.fail);
|
||||||
|
}
|
||||||
|
|
||||||
await interaction.user.send({
|
await interaction.user.send({
|
||||||
embeds: [embed], files: [attachment]
|
embeds: [embed], files: [attachment]
|
||||||
}).then(async (msg) => {
|
}).then(async (msg) => {
|
||||||
await interaction.reply({
|
const filter = m => !m.author.bot && m.author.id === interaction.user.id && m.channel.id === msg.channel.id;
|
||||||
content: cfg.messages.toVerify,
|
|
||||||
ephemeral: true
|
|
||||||
})
|
|
||||||
const filter = m => m.author.id === interaction.user.id;
|
|
||||||
const collector = msg.channel.createMessageCollector({
|
const collector = msg.channel.createMessageCollector({
|
||||||
filter,
|
filter,
|
||||||
time: cfg.captchaTimeout
|
time: cfg.captchaTimeout
|
||||||
});
|
});
|
||||||
|
|
||||||
collector.on('collect', async (m) => {
|
collector.on('collect', async (m) => {
|
||||||
if (m.content === captcha) {
|
|
||||||
//if member left the server before the verification is done
|
//if member left the server before the verification is done
|
||||||
if (!interaction.guild.members.cache.has(interaction.user.id)) {
|
if (!interaction.guild.members.cache.has(interaction.user.id)) {
|
||||||
await interaction.user.send(cfg.messages.userLeft);
|
await interaction.user.send(cfg.messages.userLeft);
|
||||||
collector.stop();
|
collector.stop();
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
if (m.content === captcha) {
|
||||||
|
validated = true;
|
||||||
await interaction.member.roles.add(cfg.guilds[guildId].roleToAdd);
|
await interaction.member.roles.add(cfg.guilds[guildId].roleToAdd);
|
||||||
await interaction.member.roles.remove(cfg.guilds[guildId].firstRole);
|
await interaction.member.roles.remove(cfg.guilds[guildId].firstRole);
|
||||||
await interaction.user.send(cfg.messages.success);
|
await interaction.user.send(cfg.messages.success);
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
// If the content doesn't match captcha
|
||||||
collector.stop();
|
collector.stop();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
await interaction.user.send(cfg.messages.fail);
|
|
||||||
collector.stop();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
collector.on('end', async (collected, reason) => {
|
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);
|
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({
|
await interaction.reply({
|
||||||
content: cfg.messages.unableToDM,
|
content: cfg.messages.unableToDM,
|
||||||
ephemeral: true
|
ephemeral: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
const captcha = await generateCaptcha(cfg.captchaCount);
|
||||||
|
verifyUser(captcha);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Note from Naoki:
|
Note from Naoki:
|
||||||
|
|||||||
Reference in New Issue
Block a user