-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
767 lines (651 loc) · 21.6 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
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
const Discord = require("discord.js");
const config = require("./configs/config.json");
const { DateTime } = require("luxon");
const fetch = require("node-fetch");
const client = new Discord.Client({ intents: 34815 });
// TODO: ASF (version, ...)
const re1 = /(addlicense|rp)\sasf\s(?:s\/|)((?:\d+[,\s]*)+)/gi;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
//Can be Changed/Updated
//commands with one optional botname argument
const apiBot = ["pause", "resume", "start", "stop", "addlicense", "redeempoints"];
const apiASF = ["exit", "restart", "update"];
const BotVersion = "v2.0.0";
//Can be Changed/Updated
const translations = {
PurchaseResultDetail: {},
Result: {}
}
const schemaMapping = {
"SteamKit2.EPurchaseResultDetail": "PurchaseResultDetail",
"SteamKit2.EResult": "Result",
};
const colorCrit = "#F04747"
const colorWarn = "#F09C48"
const colorBase = "#48F0F0"
client.once("ready", (c) => {
basicCLog(`[${c.user.username}] I am Booting...!`);
heartbeat();
});
client.on("interactionCreate", async (interaction) => {
if (interaction.user.id != config.secruity.USER_ID) return;
if (!interaction.isChatInputCommand()) return;
try {
await interaction.reply(basicEmbed("Fetching data...", colorWarn));
await onlineCheck();
let response;
let body = {};
if (apiBot.includes(interaction.commandName)) {
let IDs;
let time;
let bots = await fetchBots()
if (bots != null) {
let botname = interaction.options.getString("botname");
if (!botname) {
basicCLog(`- - - - - - - - - - -\n> ${interaction.user.tag} executed ${interaction.commandName}`)
if (interaction.commandName === "pause") {
body = {
"Permanent": true,
"ResumeInSeconds": 0
}
if ((time = Math.abs(interaction.options.getInteger("time"))) != 0) {
body.Permanent = false;
body.ResumeInSeconds = time;
response = await responseBodyP(body);
basicCLog(response.message);
return await interaction.editReply(basicEmbed(response.message, response.color));
} else {
response = await responseBodyP(body);
basicCLog(response.message);
return await interaction.editReply(basicEmbed(response.message, response.color));
}
}
else if (interaction.commandName === "addlicense") {
body = {
Apps: interaction.options.getString("license_ids").split(/[,\s]+/),
Packages: interaction.options.getString("license_ids").split(/[,\s]+/),
};
response = await responseBodyAL(body)
basicCLog(response);
return await interaction.editReply(basicEmbed(response, colorBase));
}
else if (interaction.commandName === "redeempoints") {
IDs = interaction.options.getString("item_ids").split(/[,\s]+/);
response = await responseBodyRP(IDs);
basicCLog(response);
return await interaction.editReply(basicEmbed(response, colorBase));
}
else {
response = await sendIPC(interaction.commandName);
basicCLog(response.message);
return await interaction.editReply(basicEmbed(response.message, response.color));
}
}
else if (bots.includes(botname)) {
basicCLog(`- - - - - - - - - - -\n> ${interaction.user.tag} executed ${interaction.commandName} for <${botname}>`)
if (interaction.commandName === "pause") {
body = {
"Permanent": true,
"ResumeInSeconds": 0
}
if ((time = Math.abs(interaction.options.getInteger("time"))) != 0) {
body.Permanent = false;
body.ResumeInSeconds = time;
response = await responseBodyP(body, botname);
basicCLog(response.message);
return await interaction.editReply(basicEmbed(response.message, response.color));
} else {
response = await responseBodyP(body, botname);
basicCLog(response.message);
return await interaction.editReply(basicEmbed(response.message, response.color));
}
}
else if (interaction.commandName === "addlicense") {
body = {
Apps: interaction.options.getString("license_ids").split(/[,\s]+/),
Packages: interaction.options.getString("license_ids").split(/[,\s]+/),
};
response = await responseBodyAL(body, botname)
basicCLog(response);
return await interaction.editReply(basicEmbed(response, colorBase));
}
else if (interaction.commandName === "redeempoints") {
IDs = interaction.options.getString("item_ids").split(/[,\s]+/);
response = await responseBodyRP(IDs, botname);
basicCLog(response);
return await interaction.editReply(basicEmbed(response, colorBase));
}
else {
response = await sendIPC(interaction.commandName, botname);
basicCLog(response.message);
return await interaction.editReply(basicEmbed(response.message, response.color));
}
}
} else {
basicCLog("Add Bots in ASF first!");
return interaction.editReply(basicEmbed("Add Bots in ASF first!", colorCrit));
}
}
else if (apiASF.includes(interaction.commandName)) {
basicCLog(`- - - - - - - - - - -\n> ${interaction.user.tag} executed ${interaction.commandName}`)
if (interaction.commandName === "update") {
body = {
"Channel": null,
"Forced": false
}
response = await responseBodyUp(body)
basicCLog(response.message)
return await interaction.editReply(basicEmbed(response.message, response.color))
}
else {
response = await sendIPC(interaction.commandName);
basicCLog(response.message);
return await interaction.editReply(basicEmbed(response.message, response.color));
}
} else {
switch (interaction.commandName) {
case "ping":
const startTimestamp = Date.now();
const latency = Date.now() - startTimestamp;
await interaction.editReply(basicEmbed(`Pong! The bots latency is ${latency}ms.`, colorBase));
break;
case "botversion":
await interaction.editReply(basicEmbed(BotVersion, colorBase));
break;
}
}
} catch (error) {
console.error('Error handling command:', error);
return await interaction.editReply(basicEmbed("An error occurred while processing your command", colorCrit));
}
});
client.on("messageCreate", async (message) => {
if (message.channel.id != config.input.CHANNEL_ID) return;
for (let i = 0; i < message.embeds.length; i++) {
if (message.embeds[i].description === undefined) return;
const cmdd = re1.exec(message.embeds[i].description);
if (cmdd != null) {
if (cmdd[1].toLowerCase() === "addlicense") {
let command = {
Apps: cmdd[2].split(/[,\s]+/),
Packages: cmdd[2].split(/[,\s]+/),
};
const responseMessage = await responseBodyAL(command);
message.channel.send(basicEmbed(responseMessage, colorBase));
} else {
let IDs = cmdd[2].split(/[,\s]+/);
const responseMessage = await responseBodyRP(IDs);
message.channel.send(basicEmbed(responseMessage, colorBase));
}
}
};
});
async function sendIPC(cmd, bot) {
try {
await onlineCheck();
let response;
if (apiBot.includes(cmd)) {
if (!bot) {
response = await fetch(
"https://" + config.secruity.IP + "/Api/Bot/ASF/" + cmd.charAt(0).toUpperCase() + cmd.slice(1),
{
method: "post",
headers: {
"Content-Type": "application/json",
Authentication: config.secruity.IPC_PASSWORD,
},
}
)
} else {
response = await fetch(
"https://" + config.secruity.IP + "/Api/Bot/" + bot + "/" + cmd.charAt(0).toUpperCase() + cmd.slice(1),
{
method: "post",
headers: {
"Content-Type": "application/json",
Authentication: config.secruity.IPC_PASSWORD,
},
}
)
}
} else if (apiASF.includes(cmd)) {
response = await fetch(
"https://" + config.secruity.IP + "/Api/ASF/" + cmd.charAt(0).toUpperCase() + cmd.slice(1),
{
method: "post",
headers: {
"Content-Type": "application/json",
Authentication: config.secruity.IPC_PASSWORD,
},
}
)
}
let body = await response.json();
if (body.Success) {
return {
message: body.Message,
color: colorBase
};
} else if (!body.Success) {
return {
message: body.Message,
color: colorWarn
};
} else {
console.log("func Error:", body.title);
console.log("func Status:", body.status);
if (Array.isArray(body.errors)) {
console.log("Validation Errors:");
body.errors.forEach((error, index) => {
console.log(`Error ${index + 1}:`, error.message);
console.log("Field:", error.field);
console.log("Details:", error.details);
});
} else {
console.log("Unknown validation error occurred");
}
console.log("Trace ID:", body.traceId);
}
} catch (error) {
console.error("Fetch error:", error);
}
}
async function heartbeat() {
client.user.setActivity("ASF | pinging...", {
type: Discord.ActivityType.WATCHING,
});
await client.user.setStatus('idle')
setInterval(async () => {
try {
let response = await fetch(
"https://" + config.secruity.IP + "/HealthCheck",
{
method: "get",
}
);
if (response.status == 200) {
if (client.user.presence.activities[0].name != "ASF | Online") {
basicCLog(`ASF is online`);
client.user.setActivity("ASF | Online", {
type: Discord.ActivityType.WATCHING,
});
client.user.setStatus("online");
if (Object.keys(translations.PurchaseResultDetail).length === 0) {
fetchAllTranslations().then(() => {
basicCLog(`Translations loaded`);
basicCLog(`[${client.user.username}] Ready!`);
});
}
}
} else if (response.status == 502) {
if (client.user.presence.activities[0].name != "ASF | booting...") {
basicCLog(`ASF is starting`);
client.user.setActivity("ASF | booting...", {
type: Discord.ActivityType.WATCHING,
});
client.user.setStatus("idle");
}
} else {
basicCLog(response);
}
} catch (error) {
if (error.code == "ETIMEDOUT") {
if (client.user.presence.activities[0].name != "ASF | Offline") {
basicCLog(`ASF is offline`);
client.user.setActivity("ASF | Offline", {
type: Discord.ActivityType.WATCHING,
});
client.user.setStatus("dnd");
}
} else if (error.code == "ECONNREFUSED") {
if (client.user.presence.activities[0].name != "ASF | booting...") {
basicCLog(`ASF is starting`);
client.user.setActivity("ASF | booting...", {
type: Discord.ActivityType.WATCHING,
});
client.user.setStatus("idle");
}
} else {
console.error("Fetch error:", error);
}
}
}, 10000);
}
async function onlineCheck() {
const currentActivity = client.user.presence.activities?.[0]?.name;
if (currentActivity === "ASF | pinging...") {
basicCLog(`Bot is currently pinging...`);
await new Promise((resolve) => {
const checkInterval = setInterval(async () => {
const updatedActivity = client.user.presence.activities?.[0]?.name;
if (updatedActivity === "ASF | Online") {
basicCLog(`Bot is now online.`);
clearInterval(checkInterval);
resolve();
}
if (updatedActivity === "ASF | booting...") {
clearInterval(checkInterval);
resolve();
basicCLog(`ASF is starting. Waiting for it to become online...`);
await new Promise((resolve) => {
const checkInterval2 = setInterval(async () => {
const updatedActivity = client.user.presence.activities?.[0]?.name;
if (updatedActivity === "ASF | Online") {
basicCLog(`ASF is now online.`);
clearInterval(checkInterval2);
resolve();
}
if (updatedActivity === "ASF | Offline") {
basicCLog(`ASF is Offline`);
clearInterval(checkInterval2);
resolve();
return {
message: "ASF is Offline",
color: colorCrit
}
}
}, 1000);
});
}
if (updatedActivity === "ASF | Offline") {
basicCLog(`ASF is Offline`);
clearInterval(checkInterval);
resolve();
return {
message: "ASF is Offline",
color: colorCrit
}
}
}, 1000);
});
}
}
async function fetchBots() {
try {
const response = await fetch(
"https://" + config.secruity.IP + "/Api/Bot/ASF",
{
method: "get",
headers: {
"Content-Type": "application/json",
Authentication: config.secruity.IPC_PASSWORD,
},
}
);
const body = await response.json();
if (body.Success) {
return Object.keys(body.Result);
}
throw new Error("Failed to fetch bot names");
} catch (error) {
console.error("Fetch error:", error);
return [];
}
}
async function fetchTranslations(schemaName) {
try {
const response = await fetch(
"https:" + config.secruity.IP + "/swagger/ASF/swagger.json"
);
if (!response.ok) throw new Error("Network response was not ok");
const data = await response.json();
const schema = data.components.schemas[schemaName];
if (schema && schema["x-definition"]) {
const xDefinition = schema["x-definition"];
const translationKey = schemaMapping[schemaName];
if (translationKey) {
for (const [code, translation] of Object.entries(xDefinition)) {
translations[translationKey][code] = translation;
}
} else {
console.error(`No mapping found for schema name "${schemaName}"`);
}
} else {
console.error(`Expected structure not found in the JSON response for ${schemaName}`);
}
} catch (error) {
console.error("Error fetching translations:", error);
}
}
async function fetchAllTranslations() {
const schemasToFetch = [
"SteamKit2.EPurchaseResultDetail",
"SteamKit2.EResult",
];
for (const schema of schemasToFetch) {
await fetchTranslations(schema);
}
}
async function getTranslation(schema, code) {
if (translations[schema]) {
if (Object.keys(translations[schema]).length === 0) {
return `Translations are still loading...`;
}
return Object.keys(translations[schema]).find(key => translations[schema][key] === code) || "Translation not found";
} else {
console.error(`Schema "${schema}" not found.`);
return "Schema not found";
}
}
function basicEmbed(description, color) {
let embed = new Discord.EmbedBuilder()
.setColor(color)
.setAuthor({
name: "ASF Rcon Commands",
})
.setDescription(description)
.setTimestamp(Date.now())
.setFooter({
text: `ASF-Bot ${BotVersion}`,
iconURL: `https://cdn.discordapp.com/avatars/${config.bot.ID}/${client.user.avatar}.webp?size=512`,
});
return { embeds: [embed] };
}
function basicCLog(message) {
if (message.includes('\n')) {
let lines = message.split('\n');
lines.forEach(line => {
console.log(`${getTime()} | ` + line);
});
} else {
console.log(`${getTime()} | ` + message);
}
}
async function responseBodyUp(data) {
let response
try {
const res = await fetch(
`https://${config.secruity.IP}/Api/ASF/Update`,
{
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
Authentication: config.secruity.IPC_PASSWORD,
}
}
);
const body = await res.json();
if (body.Success) {
response = body.Message
return {
message: response,
color: colorBase
};
} else if (!body.Success) {
response = body.Message
return {
message: response,
color: colorWarn
};
}
} catch (error) {
console.error("Fetch error:", error);
}
}
async function responseBodyP(data, bot) {
let response = ""
let resumeTimeFormat = ""
try {
let link
if (bot) {
response = `<${bot}> `
link = `https://${config.secruity.IP}/Api/Bot/${bot}/Pause`
} else {
link = `https://${config.secruity.IP}/Api/Bot/ASF/Pause`
}
const res = await fetch(
link,
{
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
Authentication: config.secruity.IPC_PASSWORD,
}
}
);
const body = await res.json();
if (data.ResumeInSeconds != 0) {
let resumeTime = await Math.floor(Date.now() / 1000) + data.ResumeInSeconds
resumeTimeFormat = ` Resuming in <t:${resumeTime}:R>`
}
if (body.Success) {
response = response + body.Message + resumeTimeFormat
return {
message: response,
color: colorBase
};
} else if (!body.Success) {
response = response + body.Message
return {
message: response,
color: colorWarn
};
}
} catch (error) {
console.error("Fetch error:", error);
}
}
async function responseBodyAL(data, bot) {
let response
try {
let link
if (bot) {
link = `https://${config.secruity.IP}/Api/Bot/${bot}/AddLicense`
} else {
link = `https://${config.secruity.IP}/Api/Bot/ASF/AddLicense`
}
const res = await fetch(
link,
{
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
Authentication: config.secruity.IPC_PASSWORD,
}
}
);
const body = await res.json();
if (body.Success) {
response = body.Result
}
} catch (error) {
console.error("Fetch error:", error);
}
let output = "";
for (let botName of Object.keys(response)) {
let apps = response[botName].Apps;
let packages = response[botName].Packages;
for (let id of Object.keys(apps)) {
let appDetail = apps[id];
let packageDetail = packages[id];
output += `<${botName}> Apps ID: ${id} | Status: ${await getTranslation("Result", appDetail.Result)} | Status Detail: ${await getTranslation("PurchaseResultDetail", appDetail.PurchaseResultDetail)}\n`;
output += `<${botName}> Packages ID: ${id} | Status: ${await getTranslation("Result", packageDetail.Result)} | Status Detail: ${await getTranslation("PurchaseResultDetail", packageDetail.PurchaseResultDetail)}\n`;
output += "\n";
}
output += "\n";
}
return output.trim();
}
async function responseBodyRP(IDs, bot) {
let results = [];
try {
let link
if (bot) {
link = `https://${config.secruity.IP}/Api/Bot/${bot}/RedeemPoints/`
} else {
link = `https://${config.secruity.IP}/Api/Bot/ASF/RedeemPoints/`
}
for (const id of IDs) {
try {
const res = await fetch(
link + id,
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authentication: config.secruity.IPC_PASSWORD,
},
}
);
if (!res.ok) {
throw new Error(`Error fetching ID ${id}: ${res.statusText}`);
}
const body = await res.json();
if (body && body.Result) {
results.push({
id,
message: body.Message,
result: body.Result,
});
} else {
throw new Error(`Invalid response for ID ${id}`);
}
} catch (error) {
console.error(`Error with ID ${id}:`, error);
results.push({
id,
message: `Error: ${error.message}`,
result: null,
});
}
}
let output = "";
let botGroups = {};
// Organize the results into bot groups
for (const { id, message, result } of results) {
if (result) {
for (const [botName, statusDetail] of Object.entries(result)) {
if (!botGroups[botName]) {
botGroups[botName] = [];
}
botGroups[botName].push({
id,
status: message,
statusDetail: await getTranslation("Result", statusDetail),
});
}
}
}
// Build the output message
for (const [botName, entries] of Object.entries(botGroups)) {
entries.forEach(({ id, status, statusDetail }) => {
output += `<${botName}> ID: ${id} | Status: ${status} | Status Detail: ${statusDetail}\n`;
});
output += "\n";
}
return output.trim();
} catch (error) {
console.error("General fetch error:", error);
return "An error occurred while processing the requests.";
}
}
function getTime(ms) {
const now = DateTime.local().setZone(config.TZ);
const newTime = now.plus({ milliseconds: ms });
const formattedTime = newTime.toFormat("[dd HH:mm:ss]");
return formattedTime;
}
client.login(config.bot.token);