forked from tedztar/mcstatusbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcbot.js
74 lines (66 loc) · 2.59 KB
/
mcbot.js
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
const Discord = require("discord.js");
const mcping = require('mc-ping-updated');
const chalk = require('chalk');
const escape = require('markdown-escape');
const fs = require('fs');
const client = new Discord.Client();
const settings = require('./config.json');
var hasIcon = 'n/a';
pingFrequency = (settings.pingInterval * 1000);
embedColor = ("0x" + settings.embedColor);
function getDate() {
date = new Date();
cleanDate = date.toLocaleTimeString();
}
function getServerStatus() {
mcping(settings.ip, settings.port, function(err, res) {
if (!(typeof err === 'undefined' || err === null)) {
client.user.setStatus('dnd');
serverStatus = 'Server offline';
client.user.setActivity(serverStatus, { type: 'PLAYING' });
getDate()
console.log((chalk.yellow('\[' + cleanDate + '\]:') + chalk.white(' Ping: ' +
'Error getting server status')));
console.error(err);
return;
}
if (typeof res.players.sample === 'undefined') { client.user.setStatus('idle') }
if (!(typeof res.players.sample === 'undefined')) { client.user.setStatus('online') }
serverStatus = res.players.online + ' / ' + res.players.max;
getDate()
client.user.setActivity(serverStatus, { type: 'PLAYING' }).then(presence => console.log(
chalk.cyan('\[' + cleanDate + '\]:') + chalk.white(' Ping: ' + serverStatus)
)).catch(console.error);
})
}
//Command Handler
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
client.events = new Discord.Collection();
fs.readdir("./commands/", (err, files) => {
if (err) return console.log(err);
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./commands/${file}`);
console.log("Successfully loaded " + file)
let commandName = file.split(".")[0];
client.commands.set(commandName, props);
});
});
//Events "handler"
fs.readdir('./events/', (err, files) => {
if (err) console.log(err);
files.forEach(file => {
let eventFunc = require(`./events/${file}`);
console.log("Successfully loaded " + file);
let eventName = file.split(".")[0];
client.on(eventName, (...args) => eventFunc.run(client, settings, ...args));
});
});
//Startup:
client.on("ready", () => {
console.log("Ready!");
getServerStatus()
client.setInterval(getServerStatus, pingFrequency);
});
client.login(settings.token);