summaryrefslogtreecommitdiff
path: root/server/src/commands/owner/DM.ts
blob: e0e793aead91385d3d8506781ff767bdf2827ba8 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Command } from 'discord-akairo';
import { Message } from 'discord.js';
import { colour } from '../../Config';

export default class DMOwner extends Command {
    public constructor() {
        super('dm', {
            aliases: ['dm', 'pm'],
            category: 'owner',
            description: {
                content: 'DM a specified user.',
                usage: '[user id] [message]',
                examples: [
                    '217348698294714370 hi'
                ]
            },
            ratelimit: 3,
            args: [
                {
                    id: 'user',
                    type: 'string'
                },
                {
                    id: 'type',
                    type: 'string',
                    prompt: {
                        start: 'What type of DM would you like to send the specified user?',
                        retry: 'That is not a valid DM type!'
                    }
                },
                {
                    id: 'text',
                    type: 'string',
                    prompt: {
                        start: 'What would you like to send to the specified user?'
                    },
                    match: 'rest'
                }
            ],
            ownerOnly: true
        });
    }
    
    public exec(msg: Message, { user, type, text }): Promise<Message> {
        if (type == 'embed') {
            function uuidv4() {
                return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
                    let r = Math.random() * 16 | 0, v = c == 'x' ? r : (4 & 0x3 | 0x8);
                    return v.toString(16);
                });
            }
            
            const uuid = uuidv4();
            
            user = this.client.users.resolve(user);
            if (!user) return msg.channel.send('An incorrect user ID was provided.');
            
            const embed = this.client.util.embed()
                .setTitle('You received a message from the developer!')
                .setColor(colour)
                .setDescription(text)
                .setFooter(`If you wish to respond, use the following command: ${this.client.commandHandler.prefix}feedback --reply ${uuid} <message>`)
                .setTimestamp();
                
            let attachment = (msg.attachments).array();
            if (attachment[0]) {
                this.client.users.resolve(user).send(embed, { files: [attachment[0].url] })
                    .then(() => { return msg.channel.send(`A DM has successfully been sent to ${user.username}.`)})
                    .catch(() => { return msg.channel.send(`Could not send a DM to ${user.username}.`)});
            } else {
                this.client.users.resolve(user).send(embed)
                    .then(() => { return msg.channel.send(`A DM has successfully been sent to ${user.tag}.`)})
                    .catch(() => { return msg.channel.send(`Could not send a DM to ${user.tag}.`)});
            }
        } else if (type === 'normal') {
            let attachment = (msg.attachments).array();
            if (attachment[0]) {
                this.client.users.resolve(user).send(text, { files: [attachment[0].url] })
                    .then(() => { return msg.channel.send(`A DM has successfully been sent to ${user.username}.`)})
                    .catch(() => { return msg.channel.send(`Could not send a DM to ${user.username}.`)});
            } else {
                this.client.users.resolve(user).send(text)
                    .then(() => { return msg.channel.send(`A DM has successfully been sent to ${user.tag}.`)})
                    .catch(() => { return msg.channel.send(`Could not send a DM to ${user.tag}.`)});
            }
        }
        
        return;
    }
}