This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
66 lines (56 loc) · 1.79 KB
/
bot.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
const Discord = require("discord.js");
const fs = require("fs");
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
const client = new Discord.Client();
const commandIs = (str, msg) => msg.content.toLowerCase().startsWith("!" + str);
const status = (callback, ip) => {
const ourRequest = new XMLHttpRequest();
ourRequest.open("GET", "https://mcapi.us/server/status?ip=" + ip, true);
ourRequest.onload = () => {
const ourData = JSON.parse(ourRequest.responseText);
callback(null, checkStatus(ourData));
};
ourRequest.onerror = () => console.error(ourRequest.statusText);
ourRequest.send();
};
const checkStatus = (data) =>
data.online
? `The MC server is online, players currently online: ${data.players.now}/${data.players.max}`
: "server offline";
client.on("ready", () => {
console.log("The bot in online");
});
client.on("message", (message) => {
const args = message.content.split(/[ ]+/);
// Simple ping pong
if (commandIs("hello", message)) {
message.reply("Hello there");
}
// Responds with the server status and current amount of online players
if (commandIs("status", message)) {
switch (args.length) {
case 1:
message.channel.send(
"You did not define an argument variable. Usage: `!status [ip]`"
);
break;
case 2:
status((error, result) => {
if (error) {
message.channel.send("error!");
return;
}
message.channel.send(result);
}, args[1]);
break;
default:
message.channel.send(
"You defined too many arguments. Usage: `!status [ip]`"
);
}
}
});
// parse the client token from the json file
let rawdata = fs.readFileSync("key.json");
let key = JSON.parse(rawdata);
client.login(key.token);