summaryrefslogtreecommitdiff
path: root/server/src/commands/fun/MinecraftServerStatus.ts
blob: 064f137a1129b5ede2df2582e7864b00c654267f (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
import { Command } from 'discord-akairo';
import { Message, MessageAttachment } from 'discord.js';
import Axios from 'axios';
import { colour } from '../../Config';

export default class MinecraftServerStatusFun extends Command {
    public constructor() {
        super('minecraftserverstatus', {
            aliases: ['minecraftserverstatus', 'mcss'],
            category: 'fun',
            description: {
                content: 'Check the status of a specified Minecraft server.',
                usage: '',
                examples: [
                    ''
                ]
            },
            ratelimit: 3,
            args: [
                {
                    id: "ip",
                    prompt: {
                        start: "What is the IP of the server?",
                        optional: false
                    }
                },
                {
                    id: "port",
                    prompt: {
                        start: "What is the port of the server?",
                        optional: true
                    }
                }
            ]
        });
    }
    
    public async exec(msg: Message, { ip, port }): Promise<Message> {
        const response = (
            await Axios(`https://mcapi.us/server/status?ip=${ip}&port=${port}`).catch(err => {
                console.error(err);
                return msg.reply('Woops, there was an error with the (https://mcapi.us) API.');
            })
        //@ts-ignore
        ).data;

        if (response.status !== "success") {
            return msg.reply('Woops, there was an error with the (https://mcapi.us) API.');
        }

        // https://github.com/discordjs/discord.js/issues/2175#issuecomment-538948474

        let embed = this.client.util.embed()
            .setTitle(ip)
            .setTimestamp(response.last_updated)
            .setColor(colour)
            .setThumbnail(`https://eu.mc-api.net/v3/server/favicon/${ip}${port ? ':' : ''}`)

        if (response.online) {
            embed.addField("Server Status", "Currently online.", true);
            embed.addField("Version", response.server.name, true);
            embed.addField("Members", `${response.players.now}/${response.players.max}`, true);
            embed.addField("MOTD", `\`\`\`${response.motd}\`\`\``, true);
        } else if (response.last_online) {
            embed.addField("Server Status", `Offline. Last seen ${new Date(response.last_online)}`, true);
        } else {
            embed.addField("Server Status", "Offline. Never seen online before.", true);
        }
        return msg.channel.send(embed);
    }
}