@open-discord-bots/framework 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/main.js +1 -1
- package/dist/api/modules/base.d.ts +27 -9
- package/dist/api/modules/base.js +78 -80
- package/dist/api/modules/builder.d.ts +0 -9
- package/dist/api/modules/checker.d.ts +26 -5
- package/dist/api/modules/checker.js +31 -31
- package/dist/api/modules/client.d.ts +66 -14
- package/dist/api/modules/client.js +146 -132
- package/dist/api/modules/component.d.ts +8 -2
- package/dist/api/modules/component.js +8 -6
- package/dist/api/modules/config.d.ts +0 -1
- package/dist/api/modules/config.js +9 -7
- package/dist/api/modules/console.d.ts +16 -4
- package/dist/api/modules/console.js +25 -25
- package/dist/api/modules/event.d.ts +4 -2
- package/dist/api/modules/event.js +8 -10
- package/dist/api/modules/fuse.d.ts +1 -1
- package/dist/api/modules/helpmenu.d.ts +2 -2
- package/dist/api/modules/helpmenu.js +4 -7
- package/dist/api/modules/language.d.ts +2 -1
- package/dist/api/modules/language.js +6 -9
- package/dist/api/modules/permission.d.ts +10 -1
- package/dist/api/modules/permission.js +17 -20
- package/dist/api/modules/plugin.d.ts +2 -1
- package/dist/api/modules/plugin.js +2 -2
- package/dist/api/modules/post.d.ts +12 -4
- package/dist/api/modules/post.js +36 -10
- package/dist/api/modules/progressbar.d.ts +16 -5
- package/dist/api/modules/progressbar.js +34 -34
- package/dist/api/modules/responder.d.ts +95 -26
- package/dist/api/modules/responder.js +213 -172
- package/dist/api/modules/session.d.ts +10 -1
- package/dist/api/modules/session.js +15 -15
- package/dist/api/modules/startscreen.d.ts +0 -1
- package/dist/api/modules/startscreen.js +3 -6
- package/dist/api/modules/statistic.d.ts +2 -1
- package/dist/api/modules/statistic.js +4 -7
- package/dist/api/modules/worker.d.ts +2 -1
- package/dist/api/modules/worker.js +3 -3
- package/package.json +1 -1
- package/src/api/main.ts +1 -1
- package/src/api/modules/base.ts +75 -77
- package/src/api/modules/builder.ts +0 -10
- package/src/api/modules/checker.ts +31 -31
- package/src/api/modules/client.ts +144 -136
- package/src/api/modules/component.ts +11 -7
- package/src/api/modules/config.ts +8 -6
- package/src/api/modules/console.ts +25 -25
- package/src/api/modules/event.ts +6 -10
- package/src/api/modules/fuse.ts +1 -1
- package/src/api/modules/helpmenu.ts +4 -7
- package/src/api/modules/language.ts +6 -9
- package/src/api/modules/permission.ts +17 -20
- package/src/api/modules/plugin.ts +2 -2
- package/src/api/modules/post.ts +31 -10
- package/src/api/modules/progressbar.ts +34 -34
- package/src/api/modules/responder.ts +232 -181
- package/src/api/modules/session.ts +14 -14
- package/src/api/modules/startscreen.ts +3 -6
- package/src/api/modules/statistic.ts +4 -7
- package/src/api/modules/worker.ts +3 -3
|
@@ -14,7 +14,7 @@ import { ODWarningConsoleMessage } from "./console.js";
|
|
|
14
14
|
*/
|
|
15
15
|
export class ODClientManager {
|
|
16
16
|
/**Alias to Open Discord debugger. */
|
|
17
|
-
|
|
17
|
+
debug;
|
|
18
18
|
/**List of required bot intents. Add intents to this list using the `onClientLoad` event. */
|
|
19
19
|
intents = [];
|
|
20
20
|
/**List of required bot privileged intents. Add intents to this list using the `onClientLoad` event. */
|
|
@@ -25,14 +25,14 @@ export class ODClientManager {
|
|
|
25
25
|
permissions = [];
|
|
26
26
|
/**The discord bot token, empty by default. */
|
|
27
27
|
set token(value) {
|
|
28
|
-
this
|
|
28
|
+
this.rawBotToken = value;
|
|
29
29
|
this.rest.setToken(value);
|
|
30
30
|
}
|
|
31
31
|
get token() {
|
|
32
|
-
return this
|
|
32
|
+
return this.rawBotToken;
|
|
33
33
|
}
|
|
34
34
|
/**The discord bot token. **DON'T USE THIS!!!** (use `ODClientManager.token` instead) */
|
|
35
|
-
|
|
35
|
+
rawBotToken = "";
|
|
36
36
|
/**The discord.js `discord.Client`. Only use it when initiated! */
|
|
37
37
|
client = new discord.Client({ intents: [] }); //temporary client
|
|
38
38
|
/**The discord.js REST client. Used for stuff that discord.js can't handle :) */
|
|
@@ -58,12 +58,12 @@ export class ODClientManager {
|
|
|
58
58
|
/**The autocomplete manager is responsible for all autocomplete events inside the bot. */
|
|
59
59
|
autocompletes;
|
|
60
60
|
constructor(debug) {
|
|
61
|
-
this
|
|
62
|
-
this.activity = new ODClientActivityManager(this
|
|
63
|
-
this.slashCommands = new ODSlashCommandManager(this
|
|
64
|
-
this.textCommands = new ODTextCommandManager(this
|
|
65
|
-
this.contextMenus = new ODContextMenuManager(this
|
|
66
|
-
this.autocompletes = new ODAutocompleteManager(this
|
|
61
|
+
this.debug = debug;
|
|
62
|
+
this.activity = new ODClientActivityManager(this.debug, this);
|
|
63
|
+
this.slashCommands = new ODSlashCommandManager(this.debug, this);
|
|
64
|
+
this.textCommands = new ODTextCommandManager(this.debug, this);
|
|
65
|
+
this.contextMenus = new ODContextMenuManager(this.debug, this);
|
|
66
|
+
this.autocompletes = new ODAutocompleteManager(this.debug, this);
|
|
67
67
|
}
|
|
68
68
|
/**Initiate the `client` variable & add the intents & partials to the bot. */
|
|
69
69
|
initClient() {
|
|
@@ -88,10 +88,10 @@ export class ODClientManager {
|
|
|
88
88
|
});
|
|
89
89
|
});
|
|
90
90
|
this.initiated = true;
|
|
91
|
-
this
|
|
92
|
-
this
|
|
93
|
-
this
|
|
94
|
-
this
|
|
91
|
+
this.debug.debug("Created client with intents: " + this.intents.join(", "));
|
|
92
|
+
this.debug.debug("Created client with privileged intents: " + this.privileges.join(", "));
|
|
93
|
+
this.debug.debug("Created client with partials: " + this.partials.join(", "));
|
|
94
|
+
this.debug.debug("Created client with permissions: " + this.permissions.join(", "));
|
|
95
95
|
}
|
|
96
96
|
/**Get all servers the bot is part of. */
|
|
97
97
|
async getGuilds() {
|
|
@@ -138,9 +138,9 @@ export class ODClientManager {
|
|
|
138
138
|
await this.readyListener();
|
|
139
139
|
resolve(true);
|
|
140
140
|
});
|
|
141
|
-
this
|
|
141
|
+
this.debug.debug("Actual discord.js client.login()");
|
|
142
142
|
await this.client.login(this.token);
|
|
143
|
-
this
|
|
143
|
+
this.debug.debug("Finished discord.js client.login()");
|
|
144
144
|
this.loggedIn = true;
|
|
145
145
|
}
|
|
146
146
|
catch (err) {
|
|
@@ -311,17 +311,38 @@ export class ODClientManager {
|
|
|
311
311
|
}
|
|
312
312
|
}
|
|
313
313
|
/**A simplified shortcut to send a DM to a user :) */
|
|
314
|
-
async sendUserDm(user,
|
|
314
|
+
async sendUserDm(user, build) {
|
|
315
315
|
if (!this.initiated)
|
|
316
316
|
throw new ODSystemError("Client isn't initiated yet!");
|
|
317
317
|
if (!this.ready)
|
|
318
318
|
throw new ODSystemError("Client isn't ready yet!");
|
|
319
319
|
try {
|
|
320
|
+
const msgFlags = [];
|
|
321
|
+
let msgData;
|
|
322
|
+
if ('message' in build) {
|
|
323
|
+
//USING BUILDERS (deprecated)
|
|
324
|
+
msgData = build.message;
|
|
325
|
+
if (build.ephemeral)
|
|
326
|
+
msgFlags.push(discord.MessageFlags.Ephemeral);
|
|
327
|
+
}
|
|
328
|
+
else {
|
|
329
|
+
//USING COMPONENTS
|
|
330
|
+
msgData = build.msg;
|
|
331
|
+
if (build.ephemeral)
|
|
332
|
+
msgFlags.push(discord.MessageFlags.Ephemeral);
|
|
333
|
+
if (build.componentsV2)
|
|
334
|
+
msgFlags.push(discord.MessageFlags.IsComponentsV2);
|
|
335
|
+
if (build.supressEmbeds)
|
|
336
|
+
msgFlags.push(discord.MessageFlags.SuppressEmbeds);
|
|
337
|
+
if (build.supressNotifications)
|
|
338
|
+
msgFlags.push(discord.MessageFlags.SuppressNotifications);
|
|
339
|
+
}
|
|
340
|
+
const finalMessage = Object.assign(msgData, { flags: msgFlags });
|
|
320
341
|
if (user instanceof discord.User) {
|
|
321
342
|
if (user.bot)
|
|
322
343
|
return { success: false, message: null };
|
|
323
344
|
const channel = await user.createDM();
|
|
324
|
-
const msg = await channel.send(
|
|
345
|
+
const msg = await channel.send(finalMessage);
|
|
325
346
|
return { success: true, message: msg };
|
|
326
347
|
}
|
|
327
348
|
else {
|
|
@@ -331,15 +352,15 @@ export class ODClientManager {
|
|
|
331
352
|
if (newUser.bot)
|
|
332
353
|
return { success: false, message: null };
|
|
333
354
|
const channel = await newUser.createDM();
|
|
334
|
-
const msg = await channel.send(
|
|
355
|
+
const msg = await channel.send(finalMessage);
|
|
335
356
|
return { success: true, message: msg };
|
|
336
357
|
}
|
|
337
358
|
}
|
|
338
359
|
catch {
|
|
339
360
|
try {
|
|
340
|
-
this
|
|
361
|
+
this.debug.console.log("Failed to send DM to user! ", "warning", [
|
|
341
362
|
{ key: "id", value: (user instanceof discord.User ? user.id : user) },
|
|
342
|
-
{ key: "message", value:
|
|
363
|
+
{ key: "message-build", value: build.id.value }
|
|
343
364
|
]);
|
|
344
365
|
}
|
|
345
366
|
catch { }
|
|
@@ -356,9 +377,9 @@ export class ODClientManager {
|
|
|
356
377
|
*/
|
|
357
378
|
export class ODClientActivityManager {
|
|
358
379
|
/**Alias to Open Discord debugger. */
|
|
359
|
-
|
|
380
|
+
debug;
|
|
360
381
|
/**Copy of discord.js client */
|
|
361
|
-
|
|
382
|
+
client;
|
|
362
383
|
/**The current status type */
|
|
363
384
|
type = false;
|
|
364
385
|
/**The current status text */
|
|
@@ -373,9 +394,9 @@ export class ODClientActivityManager {
|
|
|
373
394
|
refreshInterval = 600;
|
|
374
395
|
/**Is the status already initiated? */
|
|
375
396
|
initiated = false;
|
|
376
|
-
constructor(debug,
|
|
377
|
-
this
|
|
378
|
-
this
|
|
397
|
+
constructor(debug, client) {
|
|
398
|
+
this.debug = debug;
|
|
399
|
+
this.client = client;
|
|
379
400
|
}
|
|
380
401
|
/**Update the status. When already initiated, it can take up to 10min to see the updated status in discord. */
|
|
381
402
|
setStatus(type, text, mode, state, forceUpdate) {
|
|
@@ -384,32 +405,32 @@ export class ODClientActivityManager {
|
|
|
384
405
|
this.mode = mode;
|
|
385
406
|
this.state = state;
|
|
386
407
|
if (forceUpdate)
|
|
387
|
-
this
|
|
408
|
+
this.updateClientActivity(this.type, this.text);
|
|
388
409
|
}
|
|
389
410
|
/**When initiating the status, the bot starts updating the status using `discord.js`. Returns `true` when successfull. */
|
|
390
411
|
initStatus() {
|
|
391
|
-
if (this.initiated || !this
|
|
412
|
+
if (this.initiated || !this.client.ready)
|
|
392
413
|
return false;
|
|
393
|
-
this
|
|
414
|
+
this.updateClientActivity(this.type, this.text);
|
|
394
415
|
this.interval = setInterval(() => {
|
|
395
|
-
this
|
|
396
|
-
this
|
|
416
|
+
this.updateClientActivity(this.type, this.text);
|
|
417
|
+
this.debug.debug("Client status update cycle");
|
|
397
418
|
}, this.refreshInterval * 1000);
|
|
398
419
|
this.initiated = true;
|
|
399
|
-
this
|
|
420
|
+
this.debug.debug("Client status initiated");
|
|
400
421
|
return true;
|
|
401
422
|
}
|
|
402
423
|
/**Update the client status */
|
|
403
|
-
|
|
404
|
-
if (!this
|
|
424
|
+
updateClientActivity(type, text) {
|
|
425
|
+
if (!this.client.client.user)
|
|
405
426
|
throw new ODSystemError("Couldn't set client status: client.user == undefined");
|
|
406
427
|
if (type == false) {
|
|
407
|
-
this
|
|
428
|
+
this.client.client.user.setActivity();
|
|
408
429
|
return;
|
|
409
430
|
}
|
|
410
|
-
this
|
|
431
|
+
this.client.client.user.setPresence({
|
|
411
432
|
activities: [{
|
|
412
|
-
type: this
|
|
433
|
+
type: this.getStatusTypeEnum(type),
|
|
413
434
|
state: this.state ? this.state : undefined,
|
|
414
435
|
name: text,
|
|
415
436
|
}],
|
|
@@ -417,7 +438,7 @@ export class ODClientActivityManager {
|
|
|
417
438
|
});
|
|
418
439
|
}
|
|
419
440
|
/**Get the enum that links to the correct type */
|
|
420
|
-
|
|
441
|
+
getStatusTypeEnum(type) {
|
|
421
442
|
if (type == "playing")
|
|
422
443
|
return discord.ActivityType.Playing;
|
|
423
444
|
else if (type == "listening")
|
|
@@ -442,7 +463,7 @@ export class ODClientActivityManager {
|
|
|
442
463
|
*/
|
|
443
464
|
export class ODSlashCommandComparator {
|
|
444
465
|
/**Convert a `discord.ApplicationCommandOptionChoiceData<string>` to a universal Open Discord slash command option choice object for comparison. */
|
|
445
|
-
|
|
466
|
+
convertOptionChoice(choice) {
|
|
446
467
|
const nameLoc = choice.nameLocalizations ?? {};
|
|
447
468
|
return {
|
|
448
469
|
name: choice.name,
|
|
@@ -451,7 +472,7 @@ export class ODSlashCommandComparator {
|
|
|
451
472
|
};
|
|
452
473
|
}
|
|
453
474
|
/**Convert a `discord.ApplicationCommandOptionData` to a universal Open Discord slash command option object for comparison. */
|
|
454
|
-
|
|
475
|
+
convertBuilderOption(option) {
|
|
455
476
|
const nameLoc = option.nameLocalizations ?? {};
|
|
456
477
|
const descLoc = option.descriptionLocalizations ?? {};
|
|
457
478
|
return {
|
|
@@ -462,8 +483,8 @@ export class ODSlashCommandComparator {
|
|
|
462
483
|
descriptionLocalizations: Object.keys(descLoc).map((key) => { return { language: key, value: descLoc[key] }; }),
|
|
463
484
|
required: (option.type != discord.ApplicationCommandOptionType.SubcommandGroup && option.type != discord.ApplicationCommandOptionType.Subcommand && option.required) ? true : false,
|
|
464
485
|
autocomplete: option.autocomplete ?? false,
|
|
465
|
-
choices: (option.type == discord.ApplicationCommandOptionType.String && !option.autocomplete && option.choices) ? option.choices.map((choice) => this
|
|
466
|
-
options: ((option.type == discord.ApplicationCommandOptionType.SubcommandGroup || option.type == discord.ApplicationCommandOptionType.Subcommand) && option.options) ? option.options.map((opt) => this
|
|
486
|
+
choices: (option.type == discord.ApplicationCommandOptionType.String && !option.autocomplete && option.choices) ? option.choices.map((choice) => this.convertOptionChoice(choice)) : [],
|
|
487
|
+
options: ((option.type == discord.ApplicationCommandOptionType.SubcommandGroup || option.type == discord.ApplicationCommandOptionType.Subcommand) && option.options) ? option.options.map((opt) => this.convertBuilderOption(opt)) : [],
|
|
467
488
|
channelTypes: (option.type == discord.ApplicationCommandOptionType.Channel && option.channelTypes) ? option.channelTypes : [],
|
|
468
489
|
minValue: (option.type == discord.ApplicationCommandOptionType.Number && option.minValue) ? option.minValue : null,
|
|
469
490
|
maxValue: (option.type == discord.ApplicationCommandOptionType.Number && option.maxValue) ? option.maxValue : null,
|
|
@@ -472,7 +493,7 @@ export class ODSlashCommandComparator {
|
|
|
472
493
|
};
|
|
473
494
|
}
|
|
474
495
|
/**Convert a `discord.ApplicationCommandOption` to a universal Open Discord slash command option object for comparison. */
|
|
475
|
-
|
|
496
|
+
convertCommandOption(option) {
|
|
476
497
|
const nameLoc = option.nameLocalizations ?? {};
|
|
477
498
|
const descLoc = option.descriptionLocalizations ?? {};
|
|
478
499
|
return {
|
|
@@ -483,8 +504,8 @@ export class ODSlashCommandComparator {
|
|
|
483
504
|
descriptionLocalizations: Object.keys(descLoc).map((key) => { return { language: key, value: descLoc[key] }; }),
|
|
484
505
|
required: (option.type != discord.ApplicationCommandOptionType.SubcommandGroup && option.type != discord.ApplicationCommandOptionType.Subcommand && option.required) ? true : false,
|
|
485
506
|
autocomplete: option.autocomplete ?? false,
|
|
486
|
-
choices: (option.type == discord.ApplicationCommandOptionType.String && !option.autocomplete && option.choices) ? option.choices.map((choice) => this
|
|
487
|
-
options: ((option.type == discord.ApplicationCommandOptionType.SubcommandGroup || option.type == discord.ApplicationCommandOptionType.Subcommand) && option.options) ? option.options.map((opt) => this
|
|
507
|
+
choices: (option.type == discord.ApplicationCommandOptionType.String && !option.autocomplete && option.choices) ? option.choices.map((choice) => this.convertOptionChoice(choice)) : [],
|
|
508
|
+
options: ((option.type == discord.ApplicationCommandOptionType.SubcommandGroup || option.type == discord.ApplicationCommandOptionType.Subcommand) && option.options) ? option.options.map((opt) => this.convertBuilderOption(opt)) : [],
|
|
488
509
|
channelTypes: (option.type == discord.ApplicationCommandOptionType.Channel && option.channelTypes) ? option.channelTypes : [],
|
|
489
510
|
minValue: (option.type == discord.ApplicationCommandOptionType.Number && option.minValue) ? option.minValue : null,
|
|
490
511
|
maxValue: (option.type == discord.ApplicationCommandOptionType.Number && option.maxValue) ? option.maxValue : null,
|
|
@@ -506,7 +527,7 @@ export class ODSlashCommandComparator {
|
|
|
506
527
|
descriptionLocalizations: Object.keys(descLoc).map((key) => { return { language: key, value: descLoc[key] }; }),
|
|
507
528
|
guildId: guildId,
|
|
508
529
|
nsfw: builder.nsfw ?? false,
|
|
509
|
-
options: builder.options ? builder.options.map((opt) => this
|
|
530
|
+
options: builder.options ? builder.options.map((opt) => this.convertBuilderOption(opt)) : [],
|
|
510
531
|
defaultMemberPermissions: discord.PermissionsBitField.resolve(builder.defaultMemberPermissions ?? ["ViewChannel"]),
|
|
511
532
|
dmPermission: (builder.contexts && builder.contexts.includes(discord.InteractionContextType.BotDM)) ?? false,
|
|
512
533
|
integrationTypes: builder.integrationTypes ?? [discord.ApplicationIntegrationType.GuildInstall],
|
|
@@ -527,7 +548,7 @@ export class ODSlashCommandComparator {
|
|
|
527
548
|
descriptionLocalizations: Object.keys(descLoc).map((key) => { return { language: key, value: descLoc[key] }; }),
|
|
528
549
|
guildId: cmd.guildId,
|
|
529
550
|
nsfw: cmd.nsfw,
|
|
530
|
-
options: cmd.options ? cmd.options.map((opt) => this
|
|
551
|
+
options: cmd.options ? cmd.options.map((opt) => this.convertCommandOption(opt)) : [],
|
|
531
552
|
defaultMemberPermissions: discord.PermissionsBitField.resolve(cmd.defaultMemberPermissions ?? ["ViewChannel"]),
|
|
532
553
|
dmPermission: (cmd.contexts && cmd.contexts.includes(discord.InteractionContextType.BotDM)) ? true : false,
|
|
533
554
|
integrationTypes: cmd.integrationTypes ?? [discord.ApplicationIntegrationType.GuildInstall],
|
|
@@ -685,23 +706,20 @@ export class ODSlashCommandComparator {
|
|
|
685
706
|
* Here, you can add & remove slash commands & the bot will do the (de)registering.
|
|
686
707
|
*/
|
|
687
708
|
export class ODSlashCommandManager extends ODManager {
|
|
688
|
-
/**Alias to Open Discord debugger. */
|
|
689
|
-
#debug;
|
|
690
709
|
/**Refrerence to discord.js client. */
|
|
691
|
-
|
|
710
|
+
client;
|
|
692
711
|
/**Discord.js application commands manager. */
|
|
693
712
|
commandManager;
|
|
694
713
|
/**Collection of all interaction listeners. */
|
|
695
|
-
|
|
714
|
+
interactionListeners = [];
|
|
696
715
|
/**Set the soft limit for maximum amount of listeners. A warning will be shown when there are more listeners than this limit. */
|
|
697
716
|
listenerLimit = 100;
|
|
698
717
|
/**A utility class used to compare 2 slash commands with each other. */
|
|
699
718
|
comparator = new ODSlashCommandComparator();
|
|
700
|
-
constructor(debug,
|
|
719
|
+
constructor(debug, client) {
|
|
701
720
|
super(debug, "slash command");
|
|
702
|
-
this
|
|
703
|
-
this
|
|
704
|
-
this.commandManager = (manager.client.application) ? manager.client.application.commands : null;
|
|
721
|
+
this.client = client;
|
|
722
|
+
this.commandManager = (client.client.application) ? client.client.application.commands : null;
|
|
705
723
|
}
|
|
706
724
|
/**Get all registered & unregistered slash commands. */
|
|
707
725
|
async getAllRegisteredCommands(guildId) {
|
|
@@ -743,7 +761,7 @@ export class ODSlashCommandManager extends ODManager {
|
|
|
743
761
|
}
|
|
744
762
|
/**Create all commands that are not registered yet.*/
|
|
745
763
|
async createNewCommands(instances, progress) {
|
|
746
|
-
if (!this
|
|
764
|
+
if (!this.client.ready)
|
|
747
765
|
throw new ODSystemError("Client isn't ready yet! Unable to register slash commands!");
|
|
748
766
|
if (instances.length > 0 && progress) {
|
|
749
767
|
progress.max = instances.length;
|
|
@@ -751,7 +769,7 @@ export class ODSlashCommandManager extends ODManager {
|
|
|
751
769
|
}
|
|
752
770
|
for (const instance of instances) {
|
|
753
771
|
await this.createCmd(instance);
|
|
754
|
-
this
|
|
772
|
+
this.debug?.debug("Created new slash command", [
|
|
755
773
|
{ key: "id", value: instance.id.value },
|
|
756
774
|
{ key: "name", value: instance.name }
|
|
757
775
|
]);
|
|
@@ -761,7 +779,7 @@ export class ODSlashCommandManager extends ODManager {
|
|
|
761
779
|
}
|
|
762
780
|
/**Update all commands that are already registered. */
|
|
763
781
|
async updateExistingCommands(instances, progress) {
|
|
764
|
-
if (!this
|
|
782
|
+
if (!this.client.ready)
|
|
765
783
|
throw new ODSystemError("Client isn't ready yet! Unable to register slash commands!");
|
|
766
784
|
if (instances.length > 0 && progress) {
|
|
767
785
|
progress.max = instances.length;
|
|
@@ -769,14 +787,14 @@ export class ODSlashCommandManager extends ODManager {
|
|
|
769
787
|
}
|
|
770
788
|
for (const instance of instances) {
|
|
771
789
|
await this.createCmd(instance);
|
|
772
|
-
this
|
|
790
|
+
this.debug?.debug("Updated existing slash command", [{ key: "id", value: instance.id.value }, { key: "name", value: instance.name }]);
|
|
773
791
|
if (progress)
|
|
774
792
|
progress.increase(1);
|
|
775
793
|
}
|
|
776
794
|
}
|
|
777
795
|
/**Remove all commands that are registered but unused by Open Discord. */
|
|
778
796
|
async removeUnusedCommands(instances, guildId, progress) {
|
|
779
|
-
if (!this
|
|
797
|
+
if (!this.client.ready)
|
|
780
798
|
throw new ODSystemError("Client isn't ready yet! Unable to register slash commands!");
|
|
781
799
|
if (!this.commandManager)
|
|
782
800
|
throw new ODSystemError("Couldn't get client application to register slash commands!");
|
|
@@ -790,7 +808,7 @@ export class ODSlashCommandManager extends ODManager {
|
|
|
790
808
|
if (cmd) {
|
|
791
809
|
try {
|
|
792
810
|
await cmd.delete();
|
|
793
|
-
this
|
|
811
|
+
this.debug?.debug("Removed existing slash command", [{ key: "name", value: cmd.name }, { key: "guildId", value: guildId ?? "/" }]);
|
|
794
812
|
}
|
|
795
813
|
catch (err) {
|
|
796
814
|
process.emit("uncaughtException", err);
|
|
@@ -815,16 +833,16 @@ export class ODSlashCommandManager extends ODManager {
|
|
|
815
833
|
}
|
|
816
834
|
/**Start listening to the discord.js client `interactionCreate` event. */
|
|
817
835
|
startListeningToInteractions() {
|
|
818
|
-
this
|
|
836
|
+
this.client.client.on("interactionCreate", (interaction) => {
|
|
819
837
|
//return when not in main server or DM
|
|
820
|
-
if (!this
|
|
838
|
+
if (!this.client.mainServer || (interaction.guild && interaction.guild.id != this.client.mainServer.id))
|
|
821
839
|
return;
|
|
822
840
|
if (!interaction.isChatInputCommand())
|
|
823
841
|
return;
|
|
824
842
|
const cmd = this.getFiltered((cmd) => cmd.name == interaction.commandName)[0];
|
|
825
843
|
if (!cmd)
|
|
826
844
|
return;
|
|
827
|
-
this
|
|
845
|
+
this.interactionListeners.forEach((listener) => {
|
|
828
846
|
if (typeof listener.name == "string" && (interaction.commandName != listener.name))
|
|
829
847
|
return;
|
|
830
848
|
else if (listener.name instanceof RegExp && !listener.name.test(interaction.commandName))
|
|
@@ -835,13 +853,13 @@ export class ODSlashCommandManager extends ODManager {
|
|
|
835
853
|
});
|
|
836
854
|
}
|
|
837
855
|
onInteraction(commandName, callback) {
|
|
838
|
-
this
|
|
856
|
+
this.interactionListeners.push({
|
|
839
857
|
name: commandName,
|
|
840
858
|
callback
|
|
841
859
|
});
|
|
842
|
-
if (this
|
|
843
|
-
this
|
|
844
|
-
{ key: "listeners", value: this
|
|
860
|
+
if (this.interactionListeners.length > this.listenerLimit) {
|
|
861
|
+
this.debug?.console.log(new ODWarningConsoleMessage("Possible slash command interaction memory leak detected!", [
|
|
862
|
+
{ key: "listeners", value: this.interactionListeners.length.toString() }
|
|
845
863
|
]));
|
|
846
864
|
}
|
|
847
865
|
}
|
|
@@ -920,24 +938,22 @@ export class ODTextCommand extends ODManagerData {
|
|
|
920
938
|
* Here, you can add & remove text commands & the bot will do the (de)registering.
|
|
921
939
|
*/
|
|
922
940
|
export class ODTextCommandManager extends ODManager {
|
|
923
|
-
/**Alias to Open Discord debugger. */
|
|
924
|
-
#debug;
|
|
925
941
|
/**Copy of discord.js client. */
|
|
926
|
-
|
|
942
|
+
client;
|
|
927
943
|
/**Collection of all interaction listeners. */
|
|
928
|
-
|
|
944
|
+
interactionListeners = [];
|
|
929
945
|
/**Collection of all error listeners. */
|
|
930
|
-
|
|
946
|
+
errorListeners = [];
|
|
931
947
|
/**Set the soft limit for maximum amount of listeners. A warning will be shown when there are more listeners than this limit. */
|
|
932
948
|
listenerLimit = 100;
|
|
933
|
-
constructor(debug,
|
|
949
|
+
constructor(debug, client) {
|
|
934
950
|
super(debug, "text command");
|
|
935
|
-
this
|
|
936
|
-
this
|
|
951
|
+
this.debug = debug;
|
|
952
|
+
this.client = client;
|
|
937
953
|
}
|
|
938
954
|
/*Check if a message is a registered command. */
|
|
939
|
-
async
|
|
940
|
-
if (this
|
|
955
|
+
async checkMessage(msg) {
|
|
956
|
+
if (this.client.client.user && msg.author.id == this.client.client.user.id)
|
|
941
957
|
return false;
|
|
942
958
|
//filter commands for correct prefix
|
|
943
959
|
const validPrefixCommands = [];
|
|
@@ -950,7 +966,7 @@ export class ODTextCommandManager extends ODManager {
|
|
|
950
966
|
});
|
|
951
967
|
//return when no command with prefix
|
|
952
968
|
if (validPrefixCommands.length == 0) {
|
|
953
|
-
this
|
|
969
|
+
this.errorListeners.forEach((cb) => cb({
|
|
954
970
|
type: "unknown_prefix",
|
|
955
971
|
msg: msg
|
|
956
972
|
}));
|
|
@@ -967,7 +983,7 @@ export class ODTextCommandManager extends ODManager {
|
|
|
967
983
|
});
|
|
968
984
|
//return when no command with name
|
|
969
985
|
if (validNameCommands.length == 0) {
|
|
970
|
-
this
|
|
986
|
+
this.errorListeners.forEach((cb) => cb({
|
|
971
987
|
type: "unknown_command",
|
|
972
988
|
msg: msg
|
|
973
989
|
}));
|
|
@@ -986,11 +1002,11 @@ export class ODTextCommandManager extends ODManager {
|
|
|
986
1002
|
else if (typeof builder.allowedGuildIds != "undefined" && msg.guild && !builder.allowedGuildIds.includes(msg.guild.id))
|
|
987
1003
|
return false;
|
|
988
1004
|
//check all command options & return when incorrect
|
|
989
|
-
const options = await this
|
|
1005
|
+
const options = await this.checkOptions(command.cmd, command.newContent, msg);
|
|
990
1006
|
if (!options.valid)
|
|
991
1007
|
return false;
|
|
992
1008
|
//a command matched this message => emit event
|
|
993
|
-
this
|
|
1009
|
+
this.interactionListeners.forEach((listener) => {
|
|
994
1010
|
if (typeof listener.prefix == "string" && (command.cmd.builder.prefix != listener.prefix))
|
|
995
1011
|
return;
|
|
996
1012
|
if (typeof listener.name == "string" && (command.cmd.name.split(" ")[0] != listener.name))
|
|
@@ -1003,7 +1019,7 @@ export class ODTextCommandManager extends ODManager {
|
|
|
1003
1019
|
return true;
|
|
1004
1020
|
}
|
|
1005
1021
|
/**Check if all options of a command are correct. */
|
|
1006
|
-
async
|
|
1022
|
+
async checkOptions(cmd, newContent, msg) {
|
|
1007
1023
|
const options = cmd.builder.options;
|
|
1008
1024
|
if (!options)
|
|
1009
1025
|
return { valid: true, data: [] };
|
|
@@ -1013,7 +1029,7 @@ export class ODTextCommandManager extends ODManager {
|
|
|
1013
1029
|
const optionError = (type, option, location, value, reason) => {
|
|
1014
1030
|
//ERROR INVALID
|
|
1015
1031
|
if (type == "invalid_option" && value && reason) {
|
|
1016
|
-
this
|
|
1032
|
+
this.errorListeners.forEach((cb) => cb({
|
|
1017
1033
|
type: "invalid_option",
|
|
1018
1034
|
msg: msg,
|
|
1019
1035
|
prefix: cmd.builder.prefix,
|
|
@@ -1026,7 +1042,7 @@ export class ODTextCommandManager extends ODManager {
|
|
|
1026
1042
|
}));
|
|
1027
1043
|
}
|
|
1028
1044
|
else if (type == "missing_option") {
|
|
1029
|
-
this
|
|
1045
|
+
this.errorListeners.forEach((cb) => cb({
|
|
1030
1046
|
type: "missing_option",
|
|
1031
1047
|
msg: msg,
|
|
1032
1048
|
prefix: cmd.builder.prefix,
|
|
@@ -1322,7 +1338,7 @@ export class ODTextCommandManager extends ODManager {
|
|
|
1322
1338
|
tempContent = tempContent.substring(value.length + 1);
|
|
1323
1339
|
const userId = res[1];
|
|
1324
1340
|
try {
|
|
1325
|
-
const user = await this
|
|
1341
|
+
const user = await this.client.client.users.fetch(userId);
|
|
1326
1342
|
if (!user) {
|
|
1327
1343
|
optionError("invalid_option", option, location, value, "user_not_found");
|
|
1328
1344
|
}
|
|
@@ -1382,7 +1398,7 @@ export class ODTextCommandManager extends ODManager {
|
|
|
1382
1398
|
}
|
|
1383
1399
|
else if (type == "user") {
|
|
1384
1400
|
try {
|
|
1385
|
-
const user = await this
|
|
1401
|
+
const user = await this.client.client.users.fetch(mentionableId);
|
|
1386
1402
|
if (!user) {
|
|
1387
1403
|
optionError("invalid_option", option, location, value, "mentionable_not_found");
|
|
1388
1404
|
}
|
|
@@ -1415,15 +1431,15 @@ export class ODTextCommandManager extends ODManager {
|
|
|
1415
1431
|
}
|
|
1416
1432
|
/**Start listening to the discord.js client `messageCreate` event. */
|
|
1417
1433
|
startListeningToInteractions() {
|
|
1418
|
-
this
|
|
1434
|
+
this.client.client.on("messageCreate", (msg) => {
|
|
1419
1435
|
//return when not in main server or DM
|
|
1420
|
-
if (!this
|
|
1436
|
+
if (!this.client.mainServer || (msg.guild && msg.guild.id != this.client.mainServer.id))
|
|
1421
1437
|
return;
|
|
1422
|
-
this
|
|
1438
|
+
this.checkMessage(msg);
|
|
1423
1439
|
});
|
|
1424
1440
|
}
|
|
1425
1441
|
/**Check if optional values are only present at the end of the command. */
|
|
1426
|
-
|
|
1442
|
+
checkBuilderOptions(builder) {
|
|
1427
1443
|
let optionalVisited = false;
|
|
1428
1444
|
let valid = true;
|
|
1429
1445
|
let reason = null;
|
|
@@ -1444,20 +1460,20 @@ export class ODTextCommandManager extends ODManager {
|
|
|
1444
1460
|
return { valid, reason };
|
|
1445
1461
|
}
|
|
1446
1462
|
onInteraction(commandPrefix, commandName, callback) {
|
|
1447
|
-
this
|
|
1463
|
+
this.interactionListeners.push({
|
|
1448
1464
|
prefix: commandPrefix,
|
|
1449
1465
|
name: commandName,
|
|
1450
1466
|
callback
|
|
1451
1467
|
});
|
|
1452
|
-
if (this
|
|
1453
|
-
this
|
|
1454
|
-
{ key: "listeners", value: this
|
|
1468
|
+
if (this.interactionListeners.length > this.listenerLimit) {
|
|
1469
|
+
this.debug?.console.log(new ODWarningConsoleMessage("Possible text command interaction memory leak detected!", [
|
|
1470
|
+
{ key: "listeners", value: this.interactionListeners.length.toString() }
|
|
1455
1471
|
]));
|
|
1456
1472
|
}
|
|
1457
1473
|
}
|
|
1458
1474
|
/**Callback on error from all the registered text commands */
|
|
1459
1475
|
onError(callback) {
|
|
1460
|
-
this
|
|
1476
|
+
this.errorListeners.push(callback);
|
|
1461
1477
|
}
|
|
1462
1478
|
get(id) {
|
|
1463
1479
|
return super.get(id);
|
|
@@ -1469,7 +1485,7 @@ export class ODTextCommandManager extends ODManager {
|
|
|
1469
1485
|
return super.exists(id);
|
|
1470
1486
|
}
|
|
1471
1487
|
add(data, overwrite) {
|
|
1472
|
-
const checkResult = this
|
|
1488
|
+
const checkResult = this.checkBuilderOptions(data.builder);
|
|
1473
1489
|
if (!checkResult.valid && checkResult.reason == "required_after_optional")
|
|
1474
1490
|
throw new ODSystemError("Invalid text command '" + data.id.value + "' => optional options are only allowed at the end of a command!");
|
|
1475
1491
|
else if (!checkResult.valid && checkResult.reason == "allowspaces_not_last")
|
|
@@ -1566,23 +1582,21 @@ export class ODContextMenuComparator {
|
|
|
1566
1582
|
* Here, you can add & remove context interactions & the bot will do the (de)registering.
|
|
1567
1583
|
*/
|
|
1568
1584
|
export class ODContextMenuManager extends ODManager {
|
|
1569
|
-
/**Alias to Open Discord debugger. */
|
|
1570
|
-
#debug;
|
|
1571
1585
|
/**Refrerence to discord.js client. */
|
|
1572
|
-
|
|
1586
|
+
client;
|
|
1573
1587
|
/**Discord.js application commands manager. */
|
|
1574
1588
|
commandManager;
|
|
1575
1589
|
/**Collection of all interaction listeners. */
|
|
1576
|
-
|
|
1590
|
+
interactionListeners = [];
|
|
1577
1591
|
/**Set the soft limit for maximum amount of listeners. A warning will be shown when there are more listeners than this limit. */
|
|
1578
1592
|
listenerLimit = 100;
|
|
1579
1593
|
/**A utility class used to compare 2 context menus with each other. */
|
|
1580
1594
|
comparator = new ODContextMenuComparator();
|
|
1581
|
-
constructor(debug,
|
|
1595
|
+
constructor(debug, client) {
|
|
1582
1596
|
super(debug, "context menu");
|
|
1583
|
-
this
|
|
1584
|
-
this
|
|
1585
|
-
this.commandManager = (
|
|
1597
|
+
this.debug = debug;
|
|
1598
|
+
this.client = client;
|
|
1599
|
+
this.commandManager = (client.client.application) ? client.client.application.commands : null;
|
|
1586
1600
|
}
|
|
1587
1601
|
/**Get all registered & unregistered message context menu commands. */
|
|
1588
1602
|
async getAllRegisteredMenus(guildId) {
|
|
@@ -1624,7 +1638,7 @@ export class ODContextMenuManager extends ODManager {
|
|
|
1624
1638
|
}
|
|
1625
1639
|
/**Create all context menus that are not registered yet.*/
|
|
1626
1640
|
async createNewMenus(instances, progress) {
|
|
1627
|
-
if (!this
|
|
1641
|
+
if (!this.client.ready)
|
|
1628
1642
|
throw new ODSystemError("Client isn't ready yet! Unable to register context menus!");
|
|
1629
1643
|
if (instances.length > 0 && progress) {
|
|
1630
1644
|
progress.max = instances.length;
|
|
@@ -1632,7 +1646,7 @@ export class ODContextMenuManager extends ODManager {
|
|
|
1632
1646
|
}
|
|
1633
1647
|
for (const instance of instances) {
|
|
1634
1648
|
await this.createMenu(instance);
|
|
1635
|
-
this
|
|
1649
|
+
this.debug?.debug("Created new context menu", [
|
|
1636
1650
|
{ key: "id", value: instance.id.value },
|
|
1637
1651
|
{ key: "name", value: instance.name },
|
|
1638
1652
|
{ key: "type", value: (instance.builder.type == discord.ApplicationCommandType.Message) ? "message-context" : "user-context" }
|
|
@@ -1643,7 +1657,7 @@ export class ODContextMenuManager extends ODManager {
|
|
|
1643
1657
|
}
|
|
1644
1658
|
/**Update all context menus that are already registered. */
|
|
1645
1659
|
async updateExistingMenus(instances, progress) {
|
|
1646
|
-
if (!this
|
|
1660
|
+
if (!this.client.ready)
|
|
1647
1661
|
throw new ODSystemError("Client isn't ready yet! Unable to register context menus!");
|
|
1648
1662
|
if (instances.length > 0 && progress) {
|
|
1649
1663
|
progress.max = instances.length;
|
|
@@ -1651,7 +1665,7 @@ export class ODContextMenuManager extends ODManager {
|
|
|
1651
1665
|
}
|
|
1652
1666
|
for (const instance of instances) {
|
|
1653
1667
|
await this.createMenu(instance);
|
|
1654
|
-
this
|
|
1668
|
+
this.debug?.debug("Updated existing context menu", [
|
|
1655
1669
|
{ key: "id", value: instance.id.value },
|
|
1656
1670
|
{ key: "name", value: instance.name },
|
|
1657
1671
|
{ key: "type", value: (instance.builder.type == discord.ApplicationCommandType.Message) ? "message-context" : "user-context" }
|
|
@@ -1662,7 +1676,7 @@ export class ODContextMenuManager extends ODManager {
|
|
|
1662
1676
|
}
|
|
1663
1677
|
/**Remove all context menus that are registered but unused by Open Discord. */
|
|
1664
1678
|
async removeUnusedMenus(instances, guildId, progress) {
|
|
1665
|
-
if (!this
|
|
1679
|
+
if (!this.client.ready)
|
|
1666
1680
|
throw new ODSystemError("Client isn't ready yet! Unable to register context menus!");
|
|
1667
1681
|
if (!this.commandManager)
|
|
1668
1682
|
throw new ODSystemError("Couldn't get client application to register context menus!");
|
|
@@ -1676,7 +1690,7 @@ export class ODContextMenuManager extends ODManager {
|
|
|
1676
1690
|
if (menu) {
|
|
1677
1691
|
try {
|
|
1678
1692
|
await menu.delete();
|
|
1679
|
-
this
|
|
1693
|
+
this.debug?.debug("Removed existing context menu", [
|
|
1680
1694
|
{ key: "name", value: menu.name },
|
|
1681
1695
|
{ key: "guildId", value: guildId ?? "/" },
|
|
1682
1696
|
{ key: "type", value: (instance.type == discord.ApplicationCommandType.Message) ? "message-context" : "user-context" }
|
|
@@ -1705,16 +1719,16 @@ export class ODContextMenuManager extends ODManager {
|
|
|
1705
1719
|
}
|
|
1706
1720
|
/**Start listening to the discord.js client `interactionCreate` event. */
|
|
1707
1721
|
startListeningToInteractions() {
|
|
1708
|
-
this
|
|
1722
|
+
this.client.client.on("interactionCreate", (interaction) => {
|
|
1709
1723
|
//return when not in main server or DM
|
|
1710
|
-
if (!this
|
|
1724
|
+
if (!this.client.mainServer || (interaction.guild && interaction.guild.id != this.client.mainServer.id))
|
|
1711
1725
|
return;
|
|
1712
1726
|
if (!interaction.isContextMenuCommand())
|
|
1713
1727
|
return;
|
|
1714
1728
|
const menu = this.getFiltered((menu) => menu.name == interaction.commandName)[0];
|
|
1715
1729
|
if (!menu)
|
|
1716
1730
|
return;
|
|
1717
|
-
this
|
|
1731
|
+
this.interactionListeners.forEach((listener) => {
|
|
1718
1732
|
if (typeof listener.name == "string" && (interaction.commandName != listener.name))
|
|
1719
1733
|
return;
|
|
1720
1734
|
else if (listener.name instanceof RegExp && !listener.name.test(interaction.commandName))
|
|
@@ -1725,13 +1739,13 @@ export class ODContextMenuManager extends ODManager {
|
|
|
1725
1739
|
});
|
|
1726
1740
|
}
|
|
1727
1741
|
onInteraction(menuName, callback) {
|
|
1728
|
-
this
|
|
1742
|
+
this.interactionListeners.push({
|
|
1729
1743
|
name: menuName,
|
|
1730
1744
|
callback
|
|
1731
1745
|
});
|
|
1732
|
-
if (this
|
|
1733
|
-
this
|
|
1734
|
-
{ key: "listeners", value: this
|
|
1746
|
+
if (this.interactionListeners.length > this.listenerLimit) {
|
|
1747
|
+
this.debug?.console.log("Possible context menu interaction memory leak detected!", "warning", [
|
|
1748
|
+
{ key: "listeners", value: this.interactionListeners.length.toString() }
|
|
1735
1749
|
]);
|
|
1736
1750
|
}
|
|
1737
1751
|
}
|
|
@@ -1786,29 +1800,29 @@ export class ODContextMenu extends ODManagerData {
|
|
|
1786
1800
|
*/
|
|
1787
1801
|
export class ODAutocompleteManager {
|
|
1788
1802
|
/**Alias to Open Discord debugger. */
|
|
1789
|
-
|
|
1803
|
+
debug;
|
|
1790
1804
|
/**Refrerence to discord.js client. */
|
|
1791
|
-
|
|
1805
|
+
client;
|
|
1792
1806
|
/**Discord.js application commands manager. */
|
|
1793
1807
|
commandManager;
|
|
1794
1808
|
/**Collection of all interaction listeners. */
|
|
1795
|
-
|
|
1809
|
+
interactionListeners = [];
|
|
1796
1810
|
/**Set the soft limit for maximum amount of listeners. A warning will be shown when there are more listeners than this limit. */
|
|
1797
1811
|
listenerLimit = 100;
|
|
1798
|
-
constructor(debug,
|
|
1799
|
-
this
|
|
1800
|
-
this
|
|
1801
|
-
this.commandManager = (
|
|
1812
|
+
constructor(debug, client) {
|
|
1813
|
+
this.debug = debug;
|
|
1814
|
+
this.client = client;
|
|
1815
|
+
this.commandManager = (client.client.application) ? client.client.application.commands : null;
|
|
1802
1816
|
}
|
|
1803
1817
|
/**Start listening to the discord.js client `interactionCreate` event. */
|
|
1804
1818
|
startListeningToInteractions() {
|
|
1805
|
-
this
|
|
1819
|
+
this.client.client.on("interactionCreate", (interaction) => {
|
|
1806
1820
|
//return when not in main server or DM
|
|
1807
|
-
if (!this
|
|
1821
|
+
if (!this.client.mainServer || (interaction.guild && interaction.guild.id != this.client.mainServer.id))
|
|
1808
1822
|
return;
|
|
1809
1823
|
if (!interaction.isAutocomplete())
|
|
1810
1824
|
return;
|
|
1811
|
-
this
|
|
1825
|
+
this.interactionListeners.forEach((listener) => {
|
|
1812
1826
|
if (typeof listener.cmdName == "string" && (interaction.commandName != listener.cmdName))
|
|
1813
1827
|
return;
|
|
1814
1828
|
else if (listener.cmdName instanceof RegExp && !listener.cmdName.test(interaction.commandName))
|
|
@@ -1824,12 +1838,12 @@ export class ODAutocompleteManager {
|
|
|
1824
1838
|
}
|
|
1825
1839
|
/**Callback on interaction from one or multiple autocompletes. */
|
|
1826
1840
|
onInteraction(cmdName, optName, callback) {
|
|
1827
|
-
this
|
|
1841
|
+
this.interactionListeners.push({
|
|
1828
1842
|
cmdName, optName, callback
|
|
1829
1843
|
});
|
|
1830
|
-
if (this
|
|
1831
|
-
this
|
|
1832
|
-
{ key: "listeners", value: this
|
|
1844
|
+
if (this.interactionListeners.length > this.listenerLimit) {
|
|
1845
|
+
this.debug.console.log("Possible autocomplete interaction memory leak detected!", "warning", [
|
|
1846
|
+
{ key: "listeners", value: this.interactionListeners.length.toString() }
|
|
1833
1847
|
]);
|
|
1834
1848
|
}
|
|
1835
1849
|
}
|