import { Command } from 'discord-akairo'; import { Message } from 'discord.js'; import { colour, emmaServer } from '../../Config'; import * as svgCaptcha from 'svg-captcha'; import { MessageAttachment } from 'discord.js'; import { TextChannel } from 'discord.js'; import { captchaSettings, verificationRole, verificationChannel } from '../../Config'; export default class VerifyEmma extends Command { public constructor() { super('verify', { aliases: ['verify'], category: 'emma', description: { content: 'Allows to to verify yourself so you can access the rest of a server. Public version out soon!', usage: '', examples: [ '' ] }, ratelimit: 3 }); } public async exec(msg: Message): Promise { // TODO: Make this feature publically available, possibly by adding a verify channel to the guild model in the database and having automatic role creation for the un-verified role. if (msg.guild.id.toString() !== emmaServer) return; msg.delete(); if ((msg.channel as TextChannel).name !== verificationChannel) return; if (msg.channel.type ==='dm') return msg.channel.send('Sorry, verification needs to be initialized in the server\'s verification channel!'); const captcha = await svgCaptcha.create(captchaSettings); const attachment = new MessageAttachment(await this.client.img(captcha.data).then(r => r), 'captcha.png'); const embed = this.client.util.embed() .setColor(colour) .setDescription('Please reply with the following captcha. (**case-sensitive**).') //@ts-ignore .attachFiles(attachment) .setImage('attachment://captcha.png') .setFooter('Expires in 60 seconds.') const m1 = await msg.author.send('Generating captcha...').catch(async () => { const deleteMessage = await msg.reply('I\'m unable to send you a direct message. Please, make sure to allow direct messages from server members in the privacy settings!'); await this.wait(7000); return deleteMessage.delete(); }); m1.delete(); const message = await msg.author.send(embed); const response = await this.awaitReplyDM(msg.member, message, 60000); if (!response) return msg.author.send(`The captcha has expired! Try again by typing \`${this.client.commandHandler.prefix}verify\` in the server's verification channel!`).catch(async () => { const deleteMessage = await msg.reply('I\'m unable to send you a direct message. Please, make sure to allow direct messages from server members in the privacy settings!'); await this.wait(7000); return deleteMessage.delete(); }); if (response.includes(captcha.text)) { msg.author.send('You passed the captcha! Granting access...').then(async m => { const role = await msg.guild.roles.cache.find(r => r.name.toLowerCase() === verificationRole.toLowerCase()); if (role) msg.member.roles.remove(role.id); m.edit(`Access granted to **${msg.guild.name}**!`); }); } else { return msg.author.send(`Wrong captcha! Try again by typing \`${this.client.commandHandler.prefix}verify\` in the server's verification channel!`); } } public awaitReplyDM = async (member, msg, question, limit = 60000) => { const filter = m => member.id == member.id; try { const collected = await msg.channel.awaitMessages(filter, { max: 1, time: limit, errors: ['time'] }); return collected.first().content; } catch (error) { return false; } } // TODO: Use client function wait. public wait = require("util").promisify(setTimeout); }