forked from tedztar/mcstatusbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcbot.js
70 lines (61 loc) · 2.18 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
//credit to vegeta897 for the request URL part from his 'Simple Minecraft server status bot'
const Discord = require("discord.js");
const client = new Discord.Client();
const settings = require('./config.json');
var statustring = "No signal";
var request = require('request');
var mcCommand = '/minecraft'; // Command for triggering
var mcIP = settings.ip; // Your MC server IP
var mcPort = settings.port; // Your MC server port
var url = 'http://mcapi.us/server/status?ip=' + mcIP + '&port=' + mcPort;
function update() {
/*seconds = seconds + 1;
secondsString = seconds.toString();
client.user.setActivity(secondsString, { type: 'Playing' })
.then(presence => console.log(`Activity set to ${presence.game ? presence.game.name : 'none'}`))
.catch(console.error);*/
request(url, function(err, response, body) {
if(err) {
console.log(err);
//return message.reply('Error getting Minecraft server status...');
}
body = JSON.parse(body);
var status = 'Server offline';
console.log(body.motd);
if(body.online) {
if((body.motd=="&cWe are under maintenance.")||(body.players.now>=body.players.max)){
client.user.setStatus('idle')
//.then(console.log)
.catch(console.error);
}else{
client.user.setStatus('online')
//.then(console.log)
.catch(console.error);
}
if(body.players.now) {
status = ' ' + body.players.now + ' of ' + body.players.max;
} else {
status = ' 0 of ' + body.players.max;
}
} else {
client.user.setStatus('dnd')
//.then(console.log)
.catch(console.error);
}
client.user.setActivity(status, { type: 'PLAYING' })
.then(presence => console.log(status))
.catch(console.error);
});
}
client.on("ready", () => {
console.log("I am ready!");
client.setInterval(update,30000);
});
/*client.on("message", (message) => {
if (message.content.startsWith("ping")) {
message.channel.send("pong!");
update();
}
}
);*/
client.login(settings.token);