diff options
Diffstat (limited to 'server/src/commands/emma/Verify.ts')
| -rw-r--r-- | server/src/commands/emma/Verify.ts | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/server/src/commands/emma/Verify.ts b/server/src/commands/emma/Verify.ts new file mode 100644 index 0000000..09bebbb --- /dev/null +++ b/server/src/commands/emma/Verify.ts @@ -0,0 +1,73 @@ +import { Command } from 'discord-akairo'; +import { Message } from 'discord.js'; +import { colour } 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: 'moderation', + 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<Message> { + 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.add(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; + } + } + + public wait = require("util").promisify(setTimeout); +}
\ No newline at end of file |