-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (61 loc) · 2.13 KB
/
index.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
75
// subclasses
const config = require("./config.json");
const getStatus = require("./status.js");
const makeEmbed = require("./embed.js");
const data = require("./data.js");
// globals
let message;
let channel;
let current = 0;
// imports
const Discord = require("discord.js");
const client = new Discord.Client();
// main function
client.on("ready", async () => {
// set presence
console.log(`Logged in as ${client.user.tag}!`);
client.user.setPresence({activity: {name: "with game servers"}, status: "dnd"});
// fetch channel and message
channel = await client.channels.fetch(config.channel);
const messageId = data("message");
// try editing the message to avoid notifications
// if editing failed send a new message instead
try {
message = await channel.messages.fetch(messageId);
console.log(message.content);
await message.edit("⏰ De gameservers worden geladen..", {embed: null});
} catch(err) {
console.log("Editing failed:", err);
await channel.bulkDelete(99);
message = await channel.send("⏰ De gameservers worden geladen..");
}
// save the message id in case it changed
// (for example when sending a new message)
data("message", message.id);
setInterval(update, config.timeout);
update();
});
// the update function
function update() {
// sometimes discord breaks...
// surrounded in a try/catch to avoid restarting
try {
// fetch servers and edit the message with the embed
getStatus(servers => {
const embed = makeEmbed(servers);
message.edit("", {embed: embed});
current += 1;
if (typeof servers[current] === "undefined") current = 0;
let quickString = `${servers[current].name} on ${servers[current].ip}`;
if (servers[current].type === "online") {
quickString += ` - Spelers ${s.status.players.length}/${s.status.maxplayers || s.maxPlayers}`;
} else if (servers[current].type === "offline") {
quickString += " - is offline!";
}
client.user.setPresence({activity: {name: quickString}, status: "dnd"});
});
} catch(err) {
console.error("Failed to catch servers!", err);
}
}
client.login(config.token);