-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
225 lines (202 loc) · 5.68 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const {
Client,
GatewayIntentBits,
EmbedBuilder,
ActionRowBuilder,
ButtonStyle,
ButtonBuilder
} = require("discord.js");
require("dotenv").config();
const Gamedig = require("gamedig");
const QuickChart = require("quickchart-js");
const { BOT_TOKEN, STATUS_CHANNEL, HOST, PORT, DISPLAY_HOST, DISPLAY_PORT } = process.env;
let tic = false;
const time = [];
const online = [];
let link;
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on("ready", async (c) => {
console.log(`Ready! Logged in as ${c.user.tag}`);
const statusChannel = client.channels.cache.get(STATUS_CHANNEL);
if (!statusChannel) {
console.log("error: id of channel invalid!");
return;
}
const statusMessage = await createStatusMessage(statusChannel);
if (!statusMessage) {
console.log("error: unable to send status message!");
return;
}
const collector = statusMessage.channel.createMessageComponentCollector();
collector.on("collect", async (i) => {
try {
await i.deferUpdate();
let shouldUpdateGraph = false;
if(i.message.embeds[0]?.image?.url == undefined){
shouldUpdateGraph = true;
}
await i.editReply({
embeds: [await generateStatusEmbed(shouldUpdateGraph)]
});
} catch (error) {
console.error("error while updating status message:\n", error);
}
});
setInterval(async () => {
try {
if (time.length > 0) {
const now = new Date();
const currentHour = now.getHours();
const lastHour = parseInt(
time[time.length - 1].split(":")[0],
10
);
await statusMessage.edit({
embeds: [
await generateStatusEmbed(currentHour != lastHour)
]
});
}
} catch (error) {
console.error("error while updating status message:\n", error);
}
}, 300000);
});
async function createStatusMessage(statusChannel) {
await clearOldMessages(statusChannel, 1);
const statusMessage = await getLastMessage(statusChannel);
if (statusMessage && statusMessage.embeds[0]?.image?.url) {
return statusMessage;
}
await clearOldMessages(statusChannel, 0);
const embed = await generateStatusEmbed(true);
const button = new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId("button")
.setLabel("🔄️")
.setStyle(ButtonStyle.Primary)
);
return statusChannel.send({ embeds: [embed], components: [button] });
}
async function clearOldMessages(statusChannel, nbr) {
try {
const messages = await statusChannel.messages.fetch({ limit: 99 });
let i = 0;
for (const message of messages.values()) {
if (i >= nbr) {
await message.delete().catch(() => {});
}
i += 1;
}
} catch (error) {
console.error("error while deleting old status messages:\n", error.message);
}
}
async function getLastMessage(statusChannel) {
try {
const messages = await statusChannel.messages.fetch({ limit: 20 });
const filteredMessages = messages.filter((message) => {
return true;
});
return filteredMessages.first();
} catch (e) {
console.error("error while getting last status message (does not exist):\n", error.message);
return null;
}
}
async function generateStatusEmbed(update_graph) {
let embed;
tic = !tic;
let ticEmoji = tic ? "[⚪]" : "[⚫]";
try {
const state = await Gamedig.query({
type: "minecraftbe",
host: HOST,
port: PORT,
maxAttempts: 5,
socketTimeout: 1000,
debug: false
});
try {
if (update_graph) {
let timeStr = new Date().toString().split(" ")[4].substring(":", 5);
if (online.length >= 15 || time.length >= 15) {
online.shift();
time.shift();
}
time.push(`${timeStr}`);
online.push(state.players.length);
const myChart = new QuickChart();
myChart.setConfig({
type: "line",
data: {
labels: time,
datasets: [{ label: "online", data: online }]
}
});
myChart.setWidth(800);
myChart.setHeight(400);
myChart.setBackgroundColor("white");
link = await myChart.getShortUrl();
}
client.user.setActivity({
name: `✅ ${state.players.length} / ${state.maxplayers}`
});
embed = new EmbedBuilder()
.setTitle("Server online!")
.setColor("#00CC00")
.setImage(link)
.addFields(
{ name: "Address", value: `${"`" + DISPLAY_HOST + "`"}` },
{
name: "Port",
value: "`" + DISPLAY_PORT + "`",
inline: true
},
{
name: "Online:",
value: `${
"`" +
state.players.length +
" / " +
state.maxplayers +
"`"
}`,
inline: true
}
)
.setFooter({ text: `${ticEmoji} Cool kids never sleep` })
.setTimestamp();
} catch (e) {
client.user.setActivity({
name: `🆘 1000-7`
});
console.error("error while creating status message:\n", e);
embed = new EmbedBuilder()
.setTitle("Server offline!")
.setColor("#ff0000")
.setDescription(
"Looks like admin have fucked up. Our team probably already working on this issue. If you don't think so, please contact the administration."
)
.setFooter({
text: ticEmoji + " Even cool kids need to sleep"
})
.setTimestamp();
}
} catch (e) {
client.user.setActivity({
name: `🆘 1000-7`
});
console.error("error while creating status message:\n", e);
embed = new EmbedBuilder()
.setTitle("Server offline!")
.setColor("#ff0000")
.setDescription(
"Looks like admin have fucked up. Our team probably already working on this issue. If you don't think so, please contact the administration."
)
.setFooter({ text: ticEmoji + " Even cool kids need to sleep" })
.setTimestamp();
}
return embed;
}
client.login(BOT_TOKEN);