summaryrefslogtreecommitdiff
path: root/src/commands/server/quotemessage.ts
blob: 7dc2965d0581b9e2eb5e315a02fb4d285304b804 (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
import { Command, CommandoMessage } from 'discord.js-commando';
import emoji from 'emoji-random'
import { MessageEmbed } from 'discord.js';
import tt from '../../utils/truncateText.js'
import path from 'path'

module.exports = class QuoteMessageServer extends Command {
    constructor(client) {
        super(client, {
            name: 'quotemessage',
            aliases: [
                'quote-message',
                'quotemsg',
                'quote-msg'
            ],
            group: 'fun',
            memberName: 'quotemessage',
            description: 'Quote a message from a text channel.',
            examples: ['uwu!quotemessage 424936127154094080'],
            throttling: {
                usages: 5,
                duration: 30
            },
            userPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
            clientPermissions: ['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'],
            args: [
                {
                    key: 'mMsg',
                    prompt: 'What message would you like to quote?',
                    type: 'message',
                    label: 'message ID'
                }
            ]
        });
    }
    run(msg: CommandoMessage, { mMsg }) {
        let emb = new MessageEmbed()
            .setColor(0xFFCC4D)
            .setTimestamp(mMsg.createdAt)
            .setAuthor(mMsg.author.tag, mMsg.author.avatarUrl) // TODO: fix avatarurl not working
            .addFields([
                {
                    name: 'Channel',
                    value: mMsg.channel.toString()
                },
                {
                    name: 'Message',
                    value: `[Jump to](https://discordapp.com/channels/${mMsg.guild.id}/${mMsg.channel.id}/${mMsg.id})`
                }
            ])

            // check if msg had content
        console.debug('Does the message have content:', Boolean(mMsg.content))
        if (mMsg.content) emb.setDescription(tt(mMsg.content))

        // get img from msg
        let messageImage
        // valid img file extensions
        const extensions = ['.png', '.jpg', '.jpeg', '.gif', '.webp']
        // regex for url to img
        const linkRegex = /https?:\/\/(?:\w+\.)?[\w-]+\.[\w]{2,3}(?:\/[\w-_.]+)+\.(?:png|jpg|jpeg|gif|webp)/;

        // embed (that may or may not exist) with an img in it
        const imageEmbed = mMsg.embeds.find(
            msgEmbed => msgEmbed.type === 'rich' && msgEmbed.image && extensions.includes(path.extname(msgEmbed.image.url))
        )
        if (imageEmbed) messageImage = imageEmbed.image.url

        // uploaded img
        const attachment = mMsg.attachments.find(file => extensions.includes(path.extname(file.url)))
        if (attachment) {
            messageImage = attachment.url
        }

        // if there wasnt an uploaded img check if there was a url to one
        if (!messageImage) {
            const linkMatch = mMsg.content.match(linkRegex)
            if (linkMatch && extensions.includes(path.extname(linkMatch[0]))) {
                [messageImage] = linkMatch
            }
        }

        // if there was an img, set embed image to it
        if (messageImage) emb.setImage(messageImage)

        msg.say(emb)
    }
};