summaryrefslogtreecommitdiff
path: root/discord.js
blob: 985fa3f86f7e473cef285f26257840c6843d771d (plain)
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
function startDiscord() {
    try {
        const { Client, GatewayIntentBits } = require('discord.js');
        const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

        client.on('ready', () => {
            console.log(`Logged in as ${client.user.tag}!`);
        });

        client.on('messageCreate', (msg) => {
            if (!msg.guild || msg.author.bot) return;

            let message = msg.content;

            if (message.startsWith(".") && !message.startsWith("..") && message.trim() !== "." && message.trim() !== ".c.") {
                let command;

                try {
                    command = message.replaceAll(/ +/g, " ");
                    if (command.startsWith(". ")) command = "." + command.substring(2);
                    command = command.split(" ")[0].substring(1).replaceAll("/", "-");
                    let parameter = message.split(" ").splice(1).join(" ");

                    require('./handler')({
                        command,
                        parameter,
                        source: "discord",
                        message: msg,
                        channel: msg.channel,
                        client
                    });
                } catch (e) {
                    msg.channel.send("❓ Hmm, something isn't quite right! I was trying to process your message and something wrong happened. I think you need to report this so it can be fixed!\n\n```plaintext\n" + e.stack + "\n```");
                }
            } else if (message.includes("<@" + client.user.id + ">")) {
                msg.channel.send("👋 Hello! I think you forgot my prefix is `.`, use `.help` to get help.");
            } else {
                if (!lastMessages[msg.channel.id]) lastMessages[msg.channel.id] = [];
                lastMessages[msg.channel.id].unshift(message);
                lastMessages[msg.channel.id] = lastMessages[msg.channel.id].splice(0, 5);
            }
        });

        client.login(require('./credentials.json').discord);
    } catch (e) {
        setTimeout(() => {
            startDiscord();
        }, 300000);
    }
}

startDiscord();