-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
577 lines (474 loc) · 17.8 KB
/
index.js
File metadata and controls
577 lines (474 loc) · 17.8 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
const { Client, GatewayIntentBits, PermissionsBitField, Collection, REST, Routes, ModalBuilder, TextInputBuilder, ActionRowBuilder, TextInputStyle, EmbedBuilder, AttachmentBuilder } = require('discord.js');
const path = require('path');
const fs = require("fs");
const axios = require('axios');
require('dotenv').config();
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const token = process.env.DISCORD_BOT_TOKEN;
const api_url = process.env.MCSS_API_URL;
const application_id = process.env.APPLICATION_ID;
const { QuickDB } = require("quick.db");
const ConsoleCache = {};
client.db = new QuickDB();
client.commands = new Collection();
client.commands.jsonArray = [];
const instance = axios.create({
baseURL: api_url,
timeout: 2000,
httpsAgent: new (require('https').Agent)({
rejectUnauthorized: false,
}),
});
// Maybe eventually use this to handle api limits or whatever idk im doing this just in case i have to
client.fetch = async function(url, headers) {
try {
let response = await instance.get(url, headers);
return response.data
} catch (error) {
return null;
}
}
client.post = async function(url, data, headers) {
try {
let response = await instance.post(url, data, headers);
return response.data
} catch (error) {
return null;
}
}
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
client.commands.set(command.data.name, command);
client.commands.jsonArray.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
const rest = new REST().setToken(token);
(async () => {
try {
console.log(`Started refreshing ${client.commands.size} application (/) commands.`);
const data = await rest.put(
Routes.applicationCommands(application_id),
{ body: client.commands.jsonArray },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
console.error(error);
}
})();
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('messageCreate', async (message) => {
if(message.author.bot) {
return
}
let server = await client.db.get(`tracking.${message.guild.id}`)
if(server.api_key && server.channels) {
let chat_channel = server.channels.find(c => c.chat_channel === message.channel.id)
let console_channel = server.channels.find(c => c.console_channel === message.channel.id)
if(chat_channel) {
await client.post(`/api/v2/servers/execute/command`, {
"serverIds": [
chat_channel.uuid
],
"command": "say " + `[${message.member.displayName}]: ` + message.content.replaceAll("\n", " "),
},
{
headers: {
apiKey: server.api_key,
}
}
);
} else if(console_channel) {
let response = await client.post(`/api/v2/servers/execute/command`, {
"serverIds": [
console_channel.uuid
],
"command": message.content,
},
{
headers: {
apiKey: server.api_key,
}
}
);
if(response == null) {
message.reply("Command failed!")
} else {
message.reply("Command sent!")
}
}
}
});
client.on("interactionCreate", async interaction => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error(error);
if (interaction.replied || interaction.deferred) {
await interaction.followUp({ content: 'There was an error while executing this command!', ephemeral: true });
} else {
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
}
}
});
client.on('interactionCreate', async interaction => {
if (!interaction.member.permissions.has(PermissionsBitField.Flags.Administrator)) {
return;
}
if (interaction.isStringSelectMenu()) {
if (interaction.customId === 'settings-menu') {
let selectedSetting = interaction.values[0];
let modal = new ModalBuilder()
.setCustomId(`settings-modal-${selectedSetting}`)
.setTitle('Update Setting');
switch(selectedSetting) {
case "api_key": {
let row = new ActionRowBuilder();
let input = new TextInputBuilder()
.setCustomId('setting-input')
.setRequired(true)
.setLabel('Enter the API key:')
.setPlaceholder('')
.setStyle(TextInputStyle.Short)
row.addComponents(input);
modal.addComponents(row);
break;
}
case "channels_add":
let row = new ActionRowBuilder();
let row2 = new ActionRowBuilder();
let row3 = new ActionRowBuilder();
let row4 = new ActionRowBuilder();
let row5 = new ActionRowBuilder();
let input = new TextInputBuilder()
.setCustomId('setting-input')
.setRequired(true)
.setLabel('Enter the server name:')
.setPlaceholder('')
.setStyle(TextInputStyle.Short)
let input2 = new TextInputBuilder()
.setCustomId('setting-input2')
.setRequired(true)
.setLabel('Enter the chat channel id:')
.setPlaceholder('')
.setStyle(TextInputStyle.Short)
let input3 = new TextInputBuilder()
.setCustomId('setting-input3')
.setRequired(true)
.setLabel('Enter the console channel id:')
.setPlaceholder('')
.setStyle(TextInputStyle.Short)
let input4 = new TextInputBuilder()
.setCustomId('setting-input4')
.setRequired(true)
.setLabel('Enter the status channel id:')
.setPlaceholder('')
.setStyle(TextInputStyle.Short)
let input5 = new TextInputBuilder()
.setCustomId('setting-input5')
.setRequired(true)
.setLabel('Enter a link to your server logo. (PNG ONLY!)')
.setPlaceholder('')
.setStyle(TextInputStyle.Short)
row.addComponents(input);
row2.addComponents(input2);
row3.addComponents(input3);
row4.addComponents(input4);
row5.addComponents(input5);
modal.addComponents(row);
modal.addComponents(row2);
modal.addComponents(row3);
modal.addComponents(row4);
modal.addComponents(row5);
break;
case "channels_remove": {
let row = new ActionRowBuilder();
let input = new TextInputBuilder()
.setCustomId('setting-input')
.setRequired(true)
.setLabel('Enter the server name:')
.setPlaceholder('')
.setStyle(TextInputStyle.Short)
row.addComponents(input);
modal.addComponents(row);
break;
}
}
await interaction.showModal(modal);
}
}
if (interaction.isModalSubmit()) {
if (interaction.customId.startsWith('settings-modal-')) {
let selectedSetting = interaction.customId.split('-')[2];
switch(selectedSetting) {
case "api_key": {
let apiKey = interaction.fields.getTextInputValue('setting-input');
let response = await client.fetch("/api/v2", {
headers: {
apiKey: apiKey,
}
});
if(!response) {
return await interaction.reply({content: "Invalid API key!", ephemeral: true})
}
await client.db.set(`tracking.${interaction.guild.id}.api_key`, apiKey);
await interaction.reply({content: "API Key added!", ephemeral: true})
break;
}
case "channels_add": {
let serverName = interaction.fields.getTextInputValue('setting-input');
let chatChannelId = interaction.fields.getTextInputValue('setting-input2');
let consoleChannelId = interaction.fields.getTextInputValue('setting-input3');
let statusChannelId = interaction.fields.getTextInputValue('setting-input4');
let pngUrl = interaction.fields.getTextInputValue('setting-input5');
let channels = await client.db.get(`tracking.${interaction.guild.id}.channels`) || [];
let channel = interaction.guild.channels.cache.get(chatChannelId)
let channel2 = interaction.guild.channels.cache.get(consoleChannelId)
let channel3 = interaction.guild.channels.cache.get(statusChannelId)
let apiKey = await client.db.get(`tracking.${interaction.guild.id}.api_key`)
if(channels.some(channel => channel.name === serverName)) {
return await interaction.reply({content: "That server is already being tracked!", ephemeral: true});
} else if(!channel) {
return await interaction.reply({content: "The channel for the chat doesn't exist!", ephemeral: true});
} else if(!channel2) {
return await interaction.reply({content: "The channel for the console doesn't exist!", ephemeral: true});
} else if(!channel3) {
return await interaction.reply({content: "The channel for the server status doesn't exist!", ephemeral: true});
} else if(channels.some(c => c.chat_channel === channel.id)) {
return await interaction.reply({content: "The channel for the chat is already used for tracking!", ephemeral: true});
} else if(channels.some(c => c.console_channel === channel2.id)) {
return await interaction.reply({content: "The channel for the console is already used for tracking!", ephemeral: true});
} else if (!channel.permissionsFor(client.user).has(PermissionsBitField.Flags.SendMessages)) {
return await interaction.reply({content: "I don't have permission to send messages in that channel!", ephemeral: true});
} else if(!pngUrl.includes('.png')) {
return await interaction.reply({content: "The url you sent is not a png!", ephemeral: true});
} else if(!apiKey) {
return await interaction.reply({content: "You need to set up an API key!", ephemeral: true});
}
let png = await client.fetch(pngUrl, { responseType: 'arraybuffer' });
if(!png) {
return await interaction.reply({content: "The url you sent is not a png!", ephemeral: true});
}
let buffer = Buffer.from(png);
let pngSignature = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
let isPNG = pngSignature.every((byte, index) => byte === buffer[index]);
if (!isPNG) {
return await interaction.reply({content: "The url you sent is not a png!", ephemeral: true});
}
let response = await client.fetch("/api/v2/servers", {
headers: {
apiKey: apiKey,
}
});
if(!response) {
return await interaction.reply({content: "No access!", ephemeral: true})
}
let server = response.find(server => server.name.toLowerCase() === serverName.toLowerCase())
if(!server) {
return await interaction.reply({content: "That server doesn't exist!", ephemeral: true})
}
await client.db.push(`tracking.${interaction.guild.id}.channels`, {name: server.name, uuid: server.serverId, chat_channel: chatChannelId, console_channel: consoleChannelId, status_channel: statusChannelId, thumbnail: pngUrl});
await interaction.reply({content: "Server tracking added!", ephemeral: true})
break;
}
case "channels_remove": {
let serverName = interaction.fields.getTextInputValue('setting-input');
let channels = await client.db.get(`tracking.${interaction.guild.id}.channels`) || [];
if(!channels.some(channel => channel.name.toLowerCase() === serverName.toLowerCase())) {
return await interaction.reply({content: "That server doesn't exist!", ephemeral: true});
}
channels = channels.filter(channel => channel.name.toLowerCase() !== serverName.toLowerCase());
await client.db.set(`tracking.${interaction.guild.id}.channels`, channels);
await interaction.reply({content: "Server tracking removed!", ephemeral: true})
break;
}
}
}
}
});
setInterval(async () => {
let servers = await client.db.get("tracking") || {};
for (let [guildId, value] of Object.entries(servers)) {
if(!value.channels) {
continue;
}
for (let server of value.channels) {
try {
let response = await client.fetch(`/api/v2/servers/${server.uuid}/console?amountOfLines=20`, {
headers: { apiKey: value.api_key },
});
if (response) {
let previousLines = ConsoleCache[server.uuid] || [];
let newLines = response.filter(line => !previousLines.includes(line));
ConsoleCache[server.uuid] = response;
let chat_message = ""
let currentChunk = "";
let console_messages = []
newLines.forEach(line => {
let console_line = line;
let timestamp = ""
let time = console_line.match(/\[([^\]]+)\]/);
if(time) {
console_line = console_line.substring(time[0].length + 1).trim();
timestamp = `\u001b[0;31m${time[0]}\u001b[0;0m`
}
let count = 0;
console_line = console_line.replace(/\[.*?\]/g, match => {
if (count < 2) {
count++;
return `\u001b[0;34m${match}\u001b[0;0m`;
}
return match;
});
let formattedLine = `${timestamp} ${console_line}\n`;
if (currentChunk.length + formattedLine.length > 2000) {
console_messages.push(currentChunk.trim());
currentChunk = "";
}
currentChunk += formattedLine;
if(line.includes("[Server thread/INFO] [minecraft/MinecraftServer]")) {
let timestamp = `\u001b[0;31m[${line.match(/\[([^\]]+)\]/)[1]}]\u001b[0;0m `;
if(line.includes("[Not Secure]")) {
let pointer = line.indexOf("[Not Secure]") + "[Not Secure]".length;
line = line.substring(pointer).trim();
line = line.replace(/\[.*?\]/, match => `\u001b[0;34m${match}\u001b[0;0m`);
} else {
let pointer = line.indexOf("[Server thread/INFO] [minecraft/MinecraftServer]:") + "[Server thread/INFO] [minecraft/MinecraftServer]:".length;
line = line.substring(pointer).trim();
if(line.includes('[Server]')) {
line = line.replace(/\[.*?\]/, match => `\u001b[0;34m${match}\u001b[0;0m`);
} else {
let is_match = false
line = line.replace(/\<.*?\>/, match => {
is_match = true
return `\u001b[0;34m[${match.slice(1, -1)}]\u001b[0;0m`;
});
if(!is_match) {
return
}
}
}
chat_message += timestamp + line + "\n"
}
});
if(chat_message.length > 0) {
let guild = await client.guilds.cache.get(guildId)
let channel = await guild.channels.cache.get(server.chat_channel)
if(channel) {
await channel.send("\`\`\`ansi\n" + chat_message + "\`\`\`").catch(() => {})
}
}
if (currentChunk.trim().length > 0) {
console_messages.push(currentChunk.trim());
}
if(console_messages.length > 0) {
let guild = await client.guilds.cache.get(guildId)
let channel = await guild.channels.cache.get(server.console_channel)
if(channel) {
console_messages.forEach(async (console_message) => {
await channel.send("\`\`\`ansi\n" + console_message + "\`\`\`").catch(() => {})
});
}
}
}
} catch (error) {
console.error(`Error fetching console lines for server ${server.uuid}:`, error);
}
}
}
}, 10000);
setInterval(async () => {
let servers = await client.db.get("tracking") || {};
for (let [guildId, value] of Object.entries(servers)) {
if(!value.channels) {
continue;
}
for (let server of value.channels) {
if(server.status_channel) {
let guild = await client.guilds.cache.get(guildId)
let channel = await guild.channels.cache.get(server.status_channel)
if(channel) {
let statusEmbed
if(server.status_embed) {
statusEmbed = await channel.messages.fetch(server.status_embed).catch(() => {})
} else {
statusEmbed = null
}
let response = await client.fetch(`/api/v2/servers/${server.uuid}`, {
headers: {
apiKey: value.api_key,
}
});
let response2 = await client.fetch(`/api/v2/servers/${server.uuid}/stats`, {
headers: {
apiKey: value.api_key,
}
});
if(!response || !response2) {
return
}
let status;
let color;
switch(response.status) {
case 0:
status = "Offline"
color = 0xff0000
break;
case 1:
status = "Online"
color = 0x00ff00
break;
case 2:
status = "Restarting"
color = 0xff9900
break;
case 3:
status = "Starting"
color = 0xff9900
break;
default:
status = "Stopping"
color = 0xff9900
break;
}
let embed = new EmbedBuilder()
.setColor(color)
.setTitle(response.name)
.setThumbnail(server.thumbnail)
.setDescription(`**Status**: \`${status}\`\n**Players**: \`${response2.latest.playersOnline}/${response2.latest.playerLimit}\`\n**Start Time**: <t:${response2.latest.startDate}:t>`)
.setTimestamp();
if(statusEmbed) {
await statusEmbed.edit({embeds: [embed]}).catch(() => {})
} else {
await channel.send({embeds: [embed]}).then(async (newStatusEmbed) => {
let channels = value.channels || [];
let serverIndex = channels.findIndex((s) => s.uuid === server.uuid);
if (serverIndex !== -1) {
channels[serverIndex].status_embed = newStatusEmbed.id;
await client.db.set(`tracking.${guildId}.channels`, channels);
}
}).catch(() => {});
}
}
}
}
}
}, 60000);
client.login(token);