This repository has been archived by the owner on Jul 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.js
106 lines (97 loc) · 2.68 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { prefix, commands_enabled, token, frequency } = require("./config/config.json");
const servers = require("./config/servers.json");
const parsers = require("./parsers");
const Gamedig = require("gamedig");
const Discord = require("discord.js");
const bot = new Discord.Client();
// Collection to store sent message ID's
const collection = new Discord.Collection();
bot.on("ready", async () => {
bot.user.setStatus("online");
bot.user.setPresence({
status: "online",
afk: false,
activity: {
name: `${servers.length} servers`,
type: "WATCHING",
},
});
console.log(`Logged in as ${bot.user.tag}!`);
setInterval(fetchStatus, frequency * 60 * 1000);
});
bot.on("message", async (message) => {
if (!commands_enabled) return;
if (message.author.bot || !message.guild) return;
if (!message.content.startsWith(prefix)) return;
const [CMD_NAME, ...args] = message.content
.trim()
.substring(prefix.length)
.split(/\s+/);
if (CMD_NAME === "status") {
fetchStatus(message.channel);
}
});
/**
* @param {Discord.TextChannel} channel
*/
const fetchStatus = async (channel) => {
servers.forEach((server) => {
Gamedig.query({
type: server.game,
host: server.host,
port: server.port,
})
.then((state) => {
let embed;
switch (server.game) {
case "bfbc2":
embed = parsers.BFBC2(state);
break;
case "bf3":
embed = parsers.BF3(state);
break;
}
if (embed) {
embed.setFooter("Server Monitor").setTimestamp();
sendEmbed(channel, embed, server);
}
})
.catch((error) => {
const embed = new Discord.MessageEmbed()
.setColor("#eb4d4b")
.setAuthor(server.name)
.setDescription("**Status:** Offline")
.setFooter("Server Monitor")
.setTimestamp();
sendEmbed(channel, embed, server);
});
});
};
/**
* @param {Discord.TextChannel} channel
* @param {Discord.MessageEmbed} embed
*/
function sendEmbed(channel, embed, server) {
if (channel) channel.send(embed);
// command is executed
else {
// retrieve previous message
const prevId = collection.get(servers.indexOf(server));
if (prevId) {
// previous message found
bot.channels.cache
.get(server.embed_channel)
.messages.fetch(prevId)
.then((msg) => msg.edit(embed));
} else {
// send a new embed and save to collection
bot.channels.cache
.get(server.embed_channel)
.send(embed)
.then((sentMsg) => {
collection.set(servers.indexOf(server), sentMsg.id);
});
}
}
}
bot.login(token);