summaryrefslogtreecommitdiff
path: root/server/src/commands/emma/Verify.ts
blob: 09bebbbf62988adf41ef3b73d3782b3e40280f51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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);
}