-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
1674 lines (1510 loc) · 55.8 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
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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
let colors = {
Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
FgGray: "\x1b[90m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m",
BgGray: "\x1b[100m",
};
const fs = require("fs");
/* The Data, We Need It! */ let data = JSON.parse(
fs.readFileSync("./AppData/data.json"),
);
let processPath = '.';
if (data.structureType != '1') {
if (fs.existsSync('./Legacy')) {
processPath = './Legacy/';
fs.writeFileSync('./Legacy/AppData/data.json', JSON.stringify(data));
}
}
let textCommands = {};
let messageCommands = {};
let interactionHandlers = {
3: {},
2: {},
8: {},
6: {},
5: {}
};
let interactionTokenMap = {};
let specificInteractionHandlers = {};
let interactionVariables = {};
let customIDs = 0;
let bridgeGlobals = {};
let cachedActions = {};
fs.readdirSync(`${processPath}/AppData/Actions`).forEach(file => {
try {
let action = require(`${processPath}/AppData/Actions/${file}`);
cachedActions[file] = action;
} catch (e) { }
});
function runInits(action, data, surroundingActions, aind) {
try {
if (action?.init) {
action.init(data, {
interactionHandlers,
actions: surroundingActions,
atAction: aind,
data: {
id: surroundingActions[aind].id
},
transf: (string) => { return string },
createGlobal: (blob) => {
if (blob.class) {
if (!bridgeGlobals[blob.class]) {
bridgeGlobals[blob.class] = {}
}
bridgeGlobals[blob.class][blob.name] = blob.value
} else {
bridgeGlobals[blob.name] = blob.value
}
},
file: (fn) => {
let fileName = bridge.transf(fn);
if (fs.existsSync(`${data.prjSrc}`)) {
return `${data.prjSrc}\\${fileName}`
} else if (fs.existsSync(`./${fileName}`) && !fs.existsSync(fileName)) {
return `./${fileName}`
} else {
return fileName
}
},
getGlobal: (blob) => {
try {
if (blob.class) {
return bridgeGlobals[blob.class][blob.name]
} else {
return bridgeGlobals[blob.name]
}
} catch (e) { }
},
generateCustomID: () => {
customIDs++
return 'PERS-' + customIDs
},
})
}
} catch (e) { console.error(e) }
try {
action.UI.forEach(e => {
try {
let element = e?.element;
if (element == 'menu') {
data[e.storeAs].forEach(menuElement => {
runInits(e.UItypes[menuElement.type], menuElement.data)
})
} else if (element == 'actions') {
let actions = data[e.storeAs];
actions.forEach((act, aind) => {
runInits(cachedActions[act.file], act.data, actions, aind)
})
} else if (element == 'case' || element == 'condition') {
if (data[e.storeAs].type == 'runActions') {
let actions = data[e.storeActionsAs];
actions.forEach((act, aind) => {
runInits(cachedActions[act.file], act.data, actions, aind)
})
}
}
} catch (error) { }
});
} catch (e) { }
}
try {
data.commands.map(command => command.actions).forEach((act, index) => {
act.forEach((action, aind) => {
try {
runInits(cachedActions[action.file], action.data, act, aind)
} catch (err) { }
});
})
} catch (error) { }
data.commands.forEach((command, index) => {
if (command.type != 'event') {
if (command.trigger == 'textCommand') {
textCommands[command.name.toLowerCase()] = {
name: command.name,
trigger: command.trigger,
boundary: command.boundary,
rejectionScenario: command.rejectionScenario,
parameters: command.parameters,
description: command.description,
index
}
if (command.aliases) {
command.aliases.forEach(alias => {
if (alias != '') {
textCommands[alias.toLowerCase()] = { ...textCommands[command.name], name: alias };
}
})
}
} else if (command.trigger == 'messageContent') {
messageCommands[command.name.toLowerCase()] = {
name: command.name,
trigger: command.trigger,
boundary: command.boundary,
rejectionScenario: command.rejectionScenario,
parameters: command.parameters,
description: command.description,
index
}
if (command.aliases) {
command.aliases.forEach(alias => {
if (alias != '') {
messageCommands[alias] = { ...messageCommands[command.name], name: alias };
}
})
}
}
}
});
let globVars = {};
let serVars = {};
try {
if (fs.existsSync('./vars.json')) {
globVars = require('./vars.json').global
serVars = require('./vars.json').server
}
} catch (no) { }
let commandVars = {};
try {
const discord = require("oceanic.js");
const {
ApplicationCommandOptionTypes,
ApplicationCommandTypes,
InteractionTypes,
CommandInteraction,
UncachedEventMessage,
PermissionNames,
} = require("oceanic.js");
const client = new discord.Client({
auth: `Bot ${data.btk}`,
gateway: {
intents: data.intents ? Object.keys(data.intents).filter(i => data.intents[i] == true) : ["ALL"],
getAllUsers: true
},
collectionLimits: {
auditLogEntries: 500,
groupChannels: 5000,
messages: 10000,
privateChannels: 3500
}
});
client.getPossiblyUncachedMessage = (message) => {
if (message instanceof UncachedEventMessage) {
if (client.getChannel(message.channelID)) {
}
} else {
return message;
}
}
client.connect().catch(err => {
if (`${err}`.includes('Failed to get gateway information')) {
console.log(
colors.FgWhite,
colors.BgRed,
`Cannot Log Into Your Bot - Invalid Token`,
colors.Reset,
colors.BgYellow,
colors.FgBlack,
`
In order to fix this, paste in your bot's token in Settings > Bot & Project > Bot Token`,
colors.Reset
)
} else {
console.log(
colors.FgWhite,
colors.BgRed,
`Cannot Log Into Your Bot - Unexpected Error`,
colors.Reset,
colors.BgYellow,
colors.FgBlack,
`
This might be because you have disallowed some intents in the developer portal. Please Google "disallowed intents in portal discord"`,
colors.Reset
)
}
});
process.on('unhandledRejection', (err) => {
console.log(
colors.FgWhite,
colors.BgRed,
`Unhandled Rejection`,
colors.Reset,
colors.BgRed,
colors.FgWhite,
err,
colors.Reset
)
});
process.on('uncaughtException', (err) => {
console.log(
colors.FgWhite,
colors.BgRed,
`Unhandled Rejection`,
colors.Reset,
colors.BgRed,
colors.FgWhite,
err,
colors.Reset
)
});
client.on('error', (err) => {
console.log(
colors.FgWhite,
colors.BgRed,
`Cannot Log Into Your Bot - Unexpected Error`,
colors.Reset,
colors.BgYellow,
colors.FgBlack,
`
This might be because you have disallowed some intents in the developer portal. Please Google "disallowed intents in portal discord"`,
colors.Reset,
colors.Reset,
"\n",
`
`,
colors.BgRed,
colors.FgWhite,
err,
colors.Reset
)
})
let eventStorage = {};
/* Project Startup */ console.log(
`${colors.BgWhite}${colors.FgBlue}${data.name}${colors.Reset}${colors.FgGray} is starting up...${colors.Reset}`,
);
let wasEverStarted = false;
let IOqueue = [];
let cachedIO;
let IO /* In / Out */ = {
write: (newIO) => {
cachedIO = newIO;
try {
let dir = data.prjSrc;
fs.writeFileSync(`${dir}/AppData/Toolkit/storedData.json`, JSON.stringify(newIO));
} catch (err) {
fs.writeFileSync(`./AppData/Toolkit/storedData.json`, JSON.stringify(newIO));
}
},
get: () => {
if (cachedIO) return cachedIO;
try {
let dir = data.prjSrc;
let endData = JSON.parse(fs.readFileSync(`${dir}/AppData/Toolkit/storedData.json`, 'utf8'));
return endData;
} catch (err) {
console.log(err)
let endData = JSON.parse(fs.readFileSync(`./AppData/Toolkit/storedData.json`, 'utf8'));
cachedIO = endData;
return endData;
}
}
}
client.on("ready", async () => {
if (wasEverStarted) return;
wasEverStarted = true;
/* Project Start */ console.log(
`${colors.FgGreen}${data.name} started successfully!${colors.Reset}`,
);
registerCommands();
for (let i in data.commands) {
if (data.commands[i].type == "event") {
try {
let eventData = data.commands[i].eventData;
let event = require(`${processPath}/AppData/Events/${data.commands[i].eventFile}`);
const run = (eventOptions, interaction) => {
let endData = {}
eventOptions.forEach((storageOption, option) => {
endData[eventData[option]] = storageOption;
})
runActionArray(i, interaction, endData)
}
event.initialize(client, eventData, run);
} catch (err) { console.log(err) }
}
};
let guilds = client.guilds.toArray();
let limit = 20;
let fetchingLimit = 40;
for (let i in guilds) {
let guild = guilds[i];
let channels = guild.channels.toArray()
for (let iteration in channels) {
/**
* @type {discord.TextableChannel}
*/
let channel = channels[iteration];
let types = discord.ChannelTypes;
let acceptedChannelTypes = [types.GUILD_TEXT, types.GUILD_VOICE, types.PUBLIC_THREAD];
if (channel.messages < limit && acceptedChannelTypes.includes(channel.type)) {
channel.getMessages({ limit: fetchingLimit }).then(messages => {
for (let msg in messages) {
channel.messages.add(messages[msg]);
}
})
}
}
}
let actions = fs.readdirSync(`${processPath}/AppData/Actions/`);
for (let fileIndex in actions) {
let fileName = actions[fileIndex];
let action = require(`${processPath}/AppData/Actions/${fileName}`);
if (action.startup) {
action.startup({
data: {
IO,
globalVars: globVars,
serverVars: serVars,
},
fs,
createGlobal: (blob) => {
if (blob.class) {
if (!bridgeGlobals[blob.class]) {
bridgeGlobals[blob.class] = {}
}
bridgeGlobals[blob.class][blob.name] = blob.value
} else {
bridgeGlobals[blob.name] = blob.value
}
},
file: (fileName) => {
if (fs.existsSync(`${data.prjSrc}`) && !fs.existsSync(`./${fileName}`) && !fs.existsSync(fileName)) {
return `${data.prjSrc}\\${fileName}`
} else if (fs.existsSync(`./${fileName}`) && !fs.existsSync(fileName)) {
return `./${fileName}`
} else {
return fileName
}
},
getGlobal: (blob) => {
try {
if (blob.class) {
return bridgeGlobals[blob.class][blob.name]
} else {
return bridgeGlobals[blob.name]
}
} catch (e) { }
},
}, client)
}
}
});
/* Used For Running Action Arrays - Universal Action Array Runner */
const runActionArray = /**
* @async
* @param {Number | Array} at
* @param {discord.Interaction | discord.Message} interaction
* @param {Object | null} actionBridge
* @param {Object | null} options
* @returns {unknown}
*/
async (at, interaction, actionBridge, options) => {
return new Promise(async (resolve) => {
let cmdActions;
let cmdName = "Inbuilt";
let cmdAt = "Inbuilt";
let cmdId;
if (typeof at == "string") {
cmdActions = data.commands[at].actions;
cmdName = data.commands[at].name;
cmdAt = at;
cmdId = data.commands[at].customId;
} else {
cmdActions = at;
}
if (options?.actionsOverwrite) {
cmdActions = options.actionsOverwrite;
}
if (options?.data?.at != undefined) {
cmdAt = options?.data?.at
}
if (options?.data?.name != undefined) {
cmdName = options?.data?.name
}
let guild;
if (options?.guild) {
guild = options.guild;
} else if (interaction?.guildID || Object.keys(interaction).includes('guild')) {
guild = interaction.guild || interaction.guildID;
} else {
guild = client.guilds.first();
}
let temporaries = {};
if (options?.temporaries) {
temporaries = options.temporaries;
}
let finalVariables = { globalActionCache: {} };
if (typeof actionBridge == 'object') {
finalVariables = actionBridge
if (!finalVariables?.globalActionCache) {
finalVariables.globalActionCache = {};
}
}
let bridge = {
temporaries,
guild,
stopActionRun: false,
variables: finalVariables,
createGlobal: (blob) => {
if (blob.class) {
if (!bridgeGlobals[blob.class]) {
bridgeGlobals[blob.class] = {}
}
bridgeGlobals[blob.class][blob.name] = blob.value
} else {
bridgeGlobals[blob.name] = blob.value
}
},
file: (fn) => {
let fileName = bridge.transf(fn.replaceAll('\\\\', '/'));
const transfPrjSrcPath = data.prjSrc.replaceAll('\\\\', '/') + '/' + fileName;
const transfCurrentPath = `./${fileName}`;
const transfFileName = fileName;
if (!fs.existsSync(transfCurrentPath) && !fs.existsSync(transfFileName)) {
return transfPrjSrcPath;
} else if (fs.existsSync(transfCurrentPath) && !fs.existsSync(transfFileName)) {
return transfCurrentPath;
} else {
return transfFileName;
}
},
getGlobal: (blob) => {
try {
if (blob.class) {
return bridgeGlobals[blob.class][blob.name]
} else {
return bridgeGlobals[blob.name]
}
} catch (e) { }
},
createTemporary: (blob) => {
if (blob.class) {
if (!bridge.temporaries[blob.class]) {
bridge.temporaries[blob.class] = {}
}
bridge.temporaries[blob.class][blob.name] = blob.value
} else {
bridge.temporaries[blob.name] = blob.value
}
},
getTemporary: (blob) => {
try {
if (blob.class) {
return bridge.temporaries[blob.class][blob.name]
} else {
return bridge.temporaries[blob.name]
}
} catch (e) { }
},
globals: {},
data: {
ranAt: cmdAt,
nodeName: cmdName,
actions: cmdActions,
globals: bridgeGlobals,
IO,
interactionTokenMap,
globalVars: globVars,
serverVars: serVars,
commandID: options?.commandID || cmdId,
interactionHandlers: specificInteractionHandlers,
invoker: {
bridge: options?.sourceBridge,
id: options?.sourceBridge?.data.commandID ? `${options?.sourceBridge?.data.commandID}` : undefined
}
},
runner: async (source) => {
await runActionArray(source, interaction, bridge.variables, { temporaries: bridge.temporaries, guild: bridge.guild, commandID: options?.commandID || cmdId, sourceBridge: bridge });
},
fs: fs,
callActions: async (blob) => {
let atAction = 0;
bridge.stopActionRun = true;
if (blob.jump) {
atAction = Number(blob.jump) - Number(1);
} else if (blob.skip) {
atAction = Number(blob.skip) + Number(bridge.atAction) + Number(1)
} else if (blob.stop) {
bridge.stopActionRun = blob.stop;
return;
}
await runActionArray(blob.actions || bridge.data.actions, interaction, bridge.variables, { startAt: atAction, guild: bridge.guild, temporaries: bridge.temporaries, commandID: options?.commandID || cmdId, sourceBridge: bridge });
},
call: async (blob, actions) => {
if (blob.type == 'continue') { bridge.stopActionRun = false; return } else if (blob.type == 'stop') {
bridge.stopActionRun = true;
} else if (blob.type == 'skip') {
await bridge.callActions({
skip: parseFloat(blob.value)
})
bridge.stopActionRun = true;
} else if (blob.type == 'jump') {
await bridge.callActions({
jump: parseFloat(blob.value)
})
bridge.stopActionRun = true;
} else if (blob.type == 'runActions') {
await bridge.runner(actions);
} else if (blob.type == 'anchorJump') {
bridge.stopActionRun = true;
await bridge.runner(bridge.getGlobal({ class: "anchors", name: bridge.transf(blob.value) }));
} else if (blob.type == 'callAnchor') {
await bridge.runner(bridge.getGlobal({ class: "anchors", name: bridge.transf(blob.value) }));
}
return;
},
getGuild: async (blob) => {
if (!blob || blob.type == 'current') {
return bridge.guild;
} else if (blob.type == 'id') {
return (client.guilds.get(bridge.transf(blob.value)) || await client.rest.guilds.get(bridge.transf(blob.value)))
} else {
return (await bridge.get({ value: blob.value, type: blob.type }));
}
},
toMember: async (user, guild) => {
if (user.guild) return user
return await bridge.guild.getMember(user.id)
},
toUser: async (member) => {
if (member.createDM) return member
return client.users.get(member.id) || await client.rest.users.get(member.id)
},
getUser: async (blob) => {
let user = {};
let member = {};
if (blob.type == 'id') {
user = client.users.get(bridge.transf(blob.value))
if (!user?.createDM) {
user = await client.rest.users.get(bridge.transf(blob.value))
}
} else
if (blob.type == 'mentioned') {
user = (interaction.message || interaction).mentions.users[0];
} else
if (blob.type == 'author') {
user = interaction.author;
} else if (blob.type == 'messageAuthor') {
user = interaction.message.author;
} else if (blob.type == 'user') {
user = interaction.data.author;
} else {
user = await bridge.get({ value: blob.value, type: blob.type });
if (!user.user) {
if (!user?.createDM) {
user = await client.rest.users.get(user.id)
}
} else {
member = user;
user = user.user;
}
}
if (!user.member || Object.keys(member).length == 0) {
try {
member = bridge.guild.members.get(user.id);
if (!member?.edit) {
member = await bridge.guild.getMember(user.id).catch((err) => { });
}
} catch (err) { }
}
user.member = member || user;
if (!user?.id) {
console.log(`${colors.Reset}${colors.BgRed}${colors.FgWhite}Invalid User. Next error(s) will probably be about it!${colors.Reset}`)
}
return user;
},
getRole: async (blob) => {
let role = {};
if (blob.type == 'id' || blob.type == 'roleID') {
role = await bridge.guild.roles.get(bridge.transf(blob.value))
} else if (blob.type == 'mentioned') {
role = (interaction.message || interaction).mentions.roles[0]
role = await bridge.guild.roles.get(role);
} else {
role = await bridge.get({ value: blob.value, type: blob.type })
}
if (!role) {
console.log(`${colors.Reset}${colors.BgRed}${colors.FgWhite}Invalid Role. Next error(s) will probably be about it!${colors.Reset}`)
}
return role;
},
getImage: async (blob) => {
if (blob.type == 'none') return;
if (blob.type == 'url') {
const undici = require('undici');
let fetchedResult = await undici.fetch(bridge.transf(blob.value));
let result = Buffer.from(await fetchedResult.arrayBuffer());
return result;
} else if (blob.type == 'file') {
let image = fs.readFileSync(bridge.file(blob.value));
return image
} else {
let image = await bridge.get({ value: blob.value, type: blob.type })
return image
}
},
getMessage: async (blob) => {
let message = {};
if (blob.type == 'none') return;
if (blob.type == 'commandMessage') {
message = (interaction.message || interaction)
} else if (blob.type == 'interactionReply') {
if (interaction.deffered) {
message = await interaction.getFollowup()
} else {
message = await interaction.getOriginal()
}
} else {
message = bridge.get({ value: blob.value, type: blob.type })
}
return message;
},
getInteraction: async (blob) => {
let interactionResult = {};
if (blob.type == 'commandInteraction') {
interactionResult = interaction;
} else {
interactionResult = bridge.get({ value: blob.value, type: blob.type });
}
return interactionResult;
},
getChannel: async (blob) => {
let channel;
if (blob.type == 'id') {
channel = client.getChannel(bridge.transf(blob.value));
if (!channel.createMessage) {
channel = await client.rest.channels.get(bridge.transf(blob.value));
}
} else
if (blob.type == 'userID') {
channel = (client.users.get(bridge.transf(blob.value)) || await client.rest.users.get(bridge.transf(blob.value)))
} else
if (blob.type == 'user') {
channel = interaction.data.author;
} else
if (blob.type == 'mentionedChannel') {
channel = client.getChannel(interaction.mentions.channels[0])
if (!channel.createMessage) {
channel = await client.rest.channels.get(interaction.mentions.channels[0]);
}
} else
if (blob.type == 'mentionedUser') {
channel = interaction.mentions.users[0];
if (!channel.createDM) {
channel = await client.rest.users.get(interaction.mentions.users[0])
}
} else
if (blob.type == 'commandAuthor') {
channel = interaction.author;
} else
if (blob.type == 'command') {
channel = interaction.channel;
} else {
channel = await bridge.get({ value: blob.value, type: blob.type })
}
try {
if (channel.createDM) {
channel = await channel.createDM()
}
} catch (err) { }
if (!channel) {
console.log(`${colors.Reset}${colors.BgRed}${colors.FgWhite}Invalid Channel. Next error(s) will probably be about it!${colors.Reset}`)
}
return channel
},
get: (blob) => {
let result;
if (blob.type == 'tempVar' || blob.type == 'temporary') {
result = bridge.variables[blob.value]
} else
if (blob.type == 'serverVar' || blob.type == 'server') {
try {
result = serVars[bridge.guild.id][blob.value];
} catch (error) { }
} else
if (blob.type == 'globVar' || blob.type == 'global') {
result = globVars[blob.value]
}
return result
},
store: (blob, value) => {
try {
if (blob.type == 'temporary' || blob.type == 'tempVar') {
bridge.variables[blob.value] = value
return;
}
if (blob.type == 'server' || blob.type == 'serverVar') {
if (!serVars[bridge.guild.id]) {
serVars[bridge.guild.id] = {}
}
serVars[bridge.guild.id][blob.value] = value
return
}
if (blob.type == 'global' || blob.type == 'globVar') {
if (!globVars) {
globVars = {}
}
globVars[blob.value] = value
return
}
} catch (err) { console.log(err) }
},
generateCustomID: () => {
customIDs++
return `${cmdName}${bridge.atAction}` + customIDs
},
transf: (txt) => {
let command = {};
if (interaction?.author) {
command.author = interaction.author;
command.author.name = interaction.author.globalName || interaction.author.username;
command.channel = interaction.channel;
command.message = interaction;
}
let text = `${txt}`
try {
let toDiscord = (variable) => {
if (typeof variable == 'string' || variable == undefined || typeof variable == 'number') {
return variable;
} else {
if (variable.roles && variable.guild) {
return `<@${variable.id}>`
} else if (variable.sendTyping != undefined || variable.messages) {
return `<#${variable.id}>`
} else if (variable.avatarURL) {
return `<@${variable.id}>`
} else if (typeof variable.hoist == "boolean") {
return variable.mention
} else {
return variable
}
}
}
const tempVars = (variable) => {
return toDiscord(bridge.variables[variable])
};
const serverVars = (variable) => {
try {
return toDiscord(serVars[bridge.guild.id][variable])
} catch (error) {
return variable
}
};
const globalVars = (variable) => {
return toDiscord(globVars[variable])
};
let formattedText = text;
formattedText = formattedText.replace(/`/g, '\\`'); // Escape backticks
const evaluatedText = eval("`" + formattedText + "`");
return evaluatedText;
} catch (err) { console.error(err); return text }
},
};
for (let action in cmdActions) {
if (!!cmdActions[action]) {
/* See If The Thing Is Meant To Keep Going! */
if (bridge.stopActionRun == false) {
let skipUntil = 0;
if (typeof options?.startAt != 'boolean' && options?.startAt != 'NaN' && typeof options?.startAt == 'number') {
skipUntil = options.startAt;
} else {
skipUntil = 0;
}
if (action >= skipUntil) {
bridge.atAction = action;
bridge.data.id = bridge.data.actions[bridge.atAction].id;
try {
/* Run The Action, Make It Happen! */
if (!cmdActions[action].file) {
cmdActions[action].run(cmdActions[action].data, interaction, client, bridge)
} else {
await require(`${processPath}/AppData/Actions/${cmdActions[action].file}`).run(
cmdActions[action].data,
interaction,
client,
bridge,
);
}
} catch (err) {
/* Alert The User Of The Error */
console.log(
`${colors.BgRed}${colors.FgBlack}${cmdName} ${colors.FgBlack + colors.BgWhite
}(@#${cmdAt})${colors.Reset + colors.BgRed + colors.FgBlack
} >>> ${cmdActions[action].name
} ${colors.FgBlack + colors.BgWhite}(@#${action})${colors.Reset + colors.BgRed + colors.FgBlack
} >>> Error: ${err}${colors.Reset}`,
);
console.log(err);
}
}
} else {
resolve();
return;
}
}
}
resolve();
});
};
function runRejectionScenario(commandIndex, target, scenarioType, bridge, options) {
let command = data.commands[commandIndex];
if (command?.rejectionScenario) {
let scenarioTypeMap = ["notWithin", "missingPermissions"];
let scenario = command.rejectionScenario[scenarioTypeMap[scenarioType]];
runActionArray(commandIndex, target, (bridge || {}),
{
...(options || {}),
actionsOverwrite: scenario
});
}
}
client.on("messageCreate", async (msg) => {
if (msg.author.id == client.application.id) return;
if (`${msg.content}`.startsWith(data.prefix)) {
let command = textCommands[msg.content.replace(data.prefix, '').split(' ')[0].toLowerCase()];
if (!command) return
let commandName = command.name;
if (command.trigger == "textCommand") {
if (`${data.prefix.toLowerCase()}${commandName.toLowerCase()}`.toLowerCase() == msg.content.split(" ")[0].toLowerCase()) {
let matchesPermissions = true;
if (command.boundary) {
if (command.boundary.worksIn == "guild") {
if (!msg.guildID) {
matchesPermissions = false;
runRejectionScenario(`${command.index}`, msg, 0);
}
}
if (command.boundary.worksIn == "dms") {
if (msg.guildID) {
matchesPermissions = false
runRejectionScenario(`${command.index}`, msg, 0);
}
}
for (let permission in command.boundary.limits) {
if (msg.member.permissions.has(command.boundary.limits[permission]) == false && matchesPermissions != false) {
matchesPermissions = false;
runRejectionScenario(`${command.index}`, msg, 1);
}
}
}
if (matchesPermissions == true) {