@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
|
@@ -5,8 +5,10 @@ import { ODId, ODManager, ODManagerData, ODNoGeneric, ODSystemError, ODValidId }
|
|
|
5
5
|
import * as discord from "discord.js"
|
|
6
6
|
import {REST} from "@discordjs/rest"
|
|
7
7
|
import { ODWarningConsoleMessage, ODDebugger } from "./console.js"
|
|
8
|
-
import { ODMessageBuildResult
|
|
8
|
+
import { ODMessageBuildResult } from "./builder.js"
|
|
9
9
|
import { ODManualProgressBar } from "./progressbar.js"
|
|
10
|
+
import { ODResponderSendResult } from "./responder.js"
|
|
11
|
+
import { ODMessageComponentBuildResult } from "./component.js"
|
|
10
12
|
|
|
11
13
|
/**## ODClientIntents `type`
|
|
12
14
|
* A list of intents required when inviting the bot.
|
|
@@ -34,7 +36,7 @@ export type ODClientPermissions = ("CreateInstantInvite"|"KickMembers"|"BanMembe
|
|
|
34
36
|
*/
|
|
35
37
|
export class ODClientManager<SlashIdList extends ODSlashCommandManagerIdConstraint = ODSlashCommandManagerIdConstraint,TextIdList extends ODTextCommandManagerIdConstraint = ODTextCommandManagerIdConstraint,ContextMenuIdList extends ODContextMenuManagerIdConstraint = ODContextMenuManagerIdConstraint> {
|
|
36
38
|
/**Alias to Open Discord debugger. */
|
|
37
|
-
|
|
39
|
+
protected debug: ODDebugger
|
|
38
40
|
|
|
39
41
|
/**List of required bot intents. Add intents to this list using the `onClientLoad` event. */
|
|
40
42
|
intents: ODClientIntents[] = []
|
|
@@ -46,14 +48,14 @@ export class ODClientManager<SlashIdList extends ODSlashCommandManagerIdConstrai
|
|
|
46
48
|
permissions: ODClientPermissions[] = []
|
|
47
49
|
/**The discord bot token, empty by default. */
|
|
48
50
|
set token(value:string){
|
|
49
|
-
this
|
|
51
|
+
this.rawBotToken = value
|
|
50
52
|
this.rest.setToken(value)
|
|
51
53
|
}
|
|
52
54
|
get token(){
|
|
53
|
-
return this
|
|
55
|
+
return this.rawBotToken
|
|
54
56
|
}
|
|
55
57
|
/**The discord bot token. **DON'T USE THIS!!!** (use `ODClientManager.token` instead) */
|
|
56
|
-
|
|
58
|
+
private rawBotToken: string = ""
|
|
57
59
|
|
|
58
60
|
/**The discord.js `discord.Client`. Only use it when initiated! */
|
|
59
61
|
client: discord.Client<true> = new discord.Client({intents:[]}) //temporary client
|
|
@@ -82,12 +84,12 @@ export class ODClientManager<SlashIdList extends ODSlashCommandManagerIdConstrai
|
|
|
82
84
|
autocompletes: ODAutocompleteManager
|
|
83
85
|
|
|
84
86
|
constructor(debug:ODDebugger){
|
|
85
|
-
this
|
|
86
|
-
this.activity = new ODClientActivityManager(this
|
|
87
|
-
this.slashCommands = new ODSlashCommandManager(this
|
|
88
|
-
this.textCommands = new ODTextCommandManager(this
|
|
89
|
-
this.contextMenus = new ODContextMenuManager(this
|
|
90
|
-
this.autocompletes = new ODAutocompleteManager(this
|
|
87
|
+
this.debug = debug
|
|
88
|
+
this.activity = new ODClientActivityManager(this.debug,this)
|
|
89
|
+
this.slashCommands = new ODSlashCommandManager(this.debug,this)
|
|
90
|
+
this.textCommands = new ODTextCommandManager(this.debug,this)
|
|
91
|
+
this.contextMenus = new ODContextMenuManager(this.debug,this)
|
|
92
|
+
this.autocompletes = new ODAutocompleteManager(this.debug,this)
|
|
91
93
|
}
|
|
92
94
|
|
|
93
95
|
/**Initiate the `client` variable & add the intents & partials to the bot. */
|
|
@@ -114,10 +116,10 @@ export class ODClientManager<SlashIdList extends ODSlashCommandManagerIdConstrai
|
|
|
114
116
|
|
|
115
117
|
this.initiated = true
|
|
116
118
|
|
|
117
|
-
this
|
|
118
|
-
this
|
|
119
|
-
this
|
|
120
|
-
this
|
|
119
|
+
this.debug.debug("Created client with intents: "+this.intents.join(", "))
|
|
120
|
+
this.debug.debug("Created client with privileged intents: "+this.privileges.join(", "))
|
|
121
|
+
this.debug.debug("Created client with partials: "+this.partials.join(", "))
|
|
122
|
+
this.debug.debug("Created client with permissions: "+this.permissions.join(", "))
|
|
121
123
|
}
|
|
122
124
|
/**Get all servers the bot is part of. */
|
|
123
125
|
async getGuilds(): Promise<discord.Guild[]> {
|
|
@@ -161,9 +163,9 @@ export class ODClientManager<SlashIdList extends ODSlashCommandManagerIdConstrai
|
|
|
161
163
|
resolve(true)
|
|
162
164
|
})
|
|
163
165
|
|
|
164
|
-
this
|
|
166
|
+
this.debug.debug("Actual discord.js client.login()")
|
|
165
167
|
await this.client.login(this.token)
|
|
166
|
-
this
|
|
168
|
+
this.debug.debug("Finished discord.js client.login()")
|
|
167
169
|
this.loggedIn = true
|
|
168
170
|
}catch(err:any){
|
|
169
171
|
if (softErrors) return resolve(false)
|
|
@@ -301,29 +303,45 @@ export class ODClientManager<SlashIdList extends ODSlashCommandManagerIdConstrai
|
|
|
301
303
|
}
|
|
302
304
|
}
|
|
303
305
|
/**A simplified shortcut to send a DM to a user :) */
|
|
304
|
-
async sendUserDm(user:string|discord.User,
|
|
306
|
+
async sendUserDm(user:string|discord.User, build:ODMessageBuildResult|ODMessageComponentBuildResult): Promise<ODResponderSendResult<false>> {
|
|
305
307
|
if (!this.initiated) throw new ODSystemError("Client isn't initiated yet!")
|
|
306
308
|
if (!this.ready) throw new ODSystemError("Client isn't ready yet!")
|
|
307
309
|
|
|
308
310
|
try{
|
|
311
|
+
const msgFlags: number[] = []
|
|
312
|
+
let msgData: discord.MessageCreateOptions
|
|
313
|
+
if ('message' in build){
|
|
314
|
+
//USING BUILDERS (deprecated)
|
|
315
|
+
msgData = build.message
|
|
316
|
+
if (build.ephemeral) msgFlags.push(discord.MessageFlags.Ephemeral)
|
|
317
|
+
}else{
|
|
318
|
+
//USING COMPONENTS
|
|
319
|
+
msgData = build.msg
|
|
320
|
+
if (build.ephemeral) msgFlags.push(discord.MessageFlags.Ephemeral)
|
|
321
|
+
if (build.componentsV2) msgFlags.push(discord.MessageFlags.IsComponentsV2)
|
|
322
|
+
if (build.supressEmbeds) msgFlags.push(discord.MessageFlags.SuppressEmbeds)
|
|
323
|
+
if (build.supressNotifications) msgFlags.push(discord.MessageFlags.SuppressNotifications)
|
|
324
|
+
}
|
|
325
|
+
const finalMessage = Object.assign(msgData,{flags:msgFlags})
|
|
326
|
+
|
|
309
327
|
if (user instanceof discord.User){
|
|
310
328
|
if (user.bot) return {success:false,message:null}
|
|
311
329
|
const channel = await user.createDM()
|
|
312
|
-
const msg = await channel.send(
|
|
330
|
+
const msg = await channel.send(finalMessage)
|
|
313
331
|
return {success:true,message:msg}
|
|
314
332
|
}else{
|
|
315
333
|
const newUser = await this.fetchUser(user)
|
|
316
334
|
if (!newUser) throw new Error()
|
|
317
335
|
if (newUser.bot) return {success:false,message:null}
|
|
318
336
|
const channel = await newUser.createDM()
|
|
319
|
-
const msg = await channel.send(
|
|
337
|
+
const msg = await channel.send(finalMessage)
|
|
320
338
|
return {success:true,message:msg}
|
|
321
339
|
}
|
|
322
340
|
}catch{
|
|
323
341
|
try{
|
|
324
|
-
this
|
|
342
|
+
this.debug.console.log("Failed to send DM to user! ","warning",[
|
|
325
343
|
{key:"id",value:(user instanceof discord.User ? user.id : user)},
|
|
326
|
-
{key:"message",value:
|
|
344
|
+
{key:"message-build",value:build.id.value}
|
|
327
345
|
])
|
|
328
346
|
}catch{}
|
|
329
347
|
return {success:false,message:null}
|
|
@@ -350,10 +368,10 @@ export type ODClientActivityMode = ("online"|"invisible"|"idle"|"dnd")
|
|
|
350
368
|
*/
|
|
351
369
|
export class ODClientActivityManager {
|
|
352
370
|
/**Alias to Open Discord debugger. */
|
|
353
|
-
|
|
371
|
+
protected debug: ODDebugger
|
|
354
372
|
|
|
355
373
|
/**Copy of discord.js client */
|
|
356
|
-
|
|
374
|
+
protected client: ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>
|
|
357
375
|
/**The current status type */
|
|
358
376
|
type: ODClientActivityType = false
|
|
359
377
|
/**The current status text */
|
|
@@ -370,9 +388,9 @@ export class ODClientActivityManager {
|
|
|
370
388
|
/**Is the status already initiated? */
|
|
371
389
|
initiated: boolean = false
|
|
372
390
|
|
|
373
|
-
constructor(debug:ODDebugger,
|
|
374
|
-
this
|
|
375
|
-
this
|
|
391
|
+
constructor(debug:ODDebugger, client:ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>){
|
|
392
|
+
this.debug = debug
|
|
393
|
+
this.client = client
|
|
376
394
|
}
|
|
377
395
|
|
|
378
396
|
/**Update the status. When already initiated, it can take up to 10min to see the updated status in discord. */
|
|
@@ -381,32 +399,32 @@ export class ODClientActivityManager {
|
|
|
381
399
|
this.text = text
|
|
382
400
|
this.mode = mode
|
|
383
401
|
this.state = state
|
|
384
|
-
if (forceUpdate) this
|
|
402
|
+
if (forceUpdate) this.updateClientActivity(this.type,this.text)
|
|
385
403
|
}
|
|
386
404
|
|
|
387
405
|
/**When initiating the status, the bot starts updating the status using `discord.js`. Returns `true` when successfull. */
|
|
388
406
|
initStatus(): boolean {
|
|
389
|
-
if (this.initiated || !this
|
|
390
|
-
this
|
|
407
|
+
if (this.initiated || !this.client.ready) return false
|
|
408
|
+
this.updateClientActivity(this.type,this.text)
|
|
391
409
|
this.interval = setInterval(() => {
|
|
392
|
-
this
|
|
393
|
-
this
|
|
410
|
+
this.updateClientActivity(this.type,this.text)
|
|
411
|
+
this.debug.debug("Client status update cycle")
|
|
394
412
|
},this.refreshInterval*1000)
|
|
395
413
|
this.initiated = true
|
|
396
|
-
this
|
|
414
|
+
this.debug.debug("Client status initiated")
|
|
397
415
|
return true
|
|
398
416
|
}
|
|
399
417
|
|
|
400
418
|
/**Update the client status */
|
|
401
|
-
|
|
402
|
-
if (!this
|
|
419
|
+
private updateClientActivity(type:ODClientActivityType,text:string){
|
|
420
|
+
if (!this.client.client.user) throw new ODSystemError("Couldn't set client status: client.user == undefined")
|
|
403
421
|
if (type == false){
|
|
404
|
-
this
|
|
422
|
+
this.client.client.user.setActivity()
|
|
405
423
|
return
|
|
406
424
|
}
|
|
407
|
-
this
|
|
425
|
+
this.client.client.user.setPresence({
|
|
408
426
|
activities:[{
|
|
409
|
-
type:this
|
|
427
|
+
type:this.getStatusTypeEnum(type),
|
|
410
428
|
state:this.state ? this.state : undefined,
|
|
411
429
|
name:text,
|
|
412
430
|
}],
|
|
@@ -414,7 +432,7 @@ export class ODClientActivityManager {
|
|
|
414
432
|
})
|
|
415
433
|
}
|
|
416
434
|
/**Get the enum that links to the correct type */
|
|
417
|
-
|
|
435
|
+
private getStatusTypeEnum(type:Exclude<ODClientActivityType,false>){
|
|
418
436
|
if (type == "playing") return discord.ActivityType.Playing
|
|
419
437
|
else if (type == "listening") return discord.ActivityType.Listening
|
|
420
438
|
else if (type == "watching") return discord.ActivityType.Watching
|
|
@@ -538,7 +556,7 @@ export interface ODSlashCommandBuilder extends discord.ChatInputApplicationComma
|
|
|
538
556
|
*/
|
|
539
557
|
export class ODSlashCommandComparator {
|
|
540
558
|
/**Convert a `discord.ApplicationCommandOptionChoiceData<string>` to a universal Open Discord slash command option choice object for comparison. */
|
|
541
|
-
|
|
559
|
+
protected convertOptionChoice(choice:discord.ApplicationCommandOptionChoiceData<string>): ODSlashCommandUniversalOptionChoice {
|
|
542
560
|
const nameLoc = choice.nameLocalizations ?? {}
|
|
543
561
|
return {
|
|
544
562
|
name:choice.name,
|
|
@@ -547,7 +565,7 @@ export class ODSlashCommandComparator {
|
|
|
547
565
|
}
|
|
548
566
|
}
|
|
549
567
|
/**Convert a `discord.ApplicationCommandOptionData` to a universal Open Discord slash command option object for comparison. */
|
|
550
|
-
|
|
568
|
+
protected convertBuilderOption(option:discord.ApplicationCommandOptionData): ODSlashCommandUniversalOption {
|
|
551
569
|
const nameLoc = option.nameLocalizations ?? {}
|
|
552
570
|
const descLoc = option.descriptionLocalizations ?? {}
|
|
553
571
|
return {
|
|
@@ -559,8 +577,8 @@ export class ODSlashCommandComparator {
|
|
|
559
577
|
required:(option.type != discord.ApplicationCommandOptionType.SubcommandGroup && option.type != discord.ApplicationCommandOptionType.Subcommand && option.required) ? true : false,
|
|
560
578
|
|
|
561
579
|
autocomplete:option.autocomplete ?? false,
|
|
562
|
-
choices:(option.type == discord.ApplicationCommandOptionType.String && !option.autocomplete && option.choices) ? option.choices.map((choice) => this
|
|
563
|
-
options:((option.type == discord.ApplicationCommandOptionType.SubcommandGroup || option.type == discord.ApplicationCommandOptionType.Subcommand) && option.options) ? option.options.map((opt) => this
|
|
580
|
+
choices:(option.type == discord.ApplicationCommandOptionType.String && !option.autocomplete && option.choices) ? option.choices.map((choice) => this.convertOptionChoice(choice)) : [],
|
|
581
|
+
options:((option.type == discord.ApplicationCommandOptionType.SubcommandGroup || option.type == discord.ApplicationCommandOptionType.Subcommand) && option.options) ? option.options.map((opt) => this.convertBuilderOption(opt)) : [],
|
|
564
582
|
channelTypes:(option.type == discord.ApplicationCommandOptionType.Channel && option.channelTypes) ? option.channelTypes : [],
|
|
565
583
|
minValue:(option.type == discord.ApplicationCommandOptionType.Number && option.minValue) ? option.minValue : null,
|
|
566
584
|
maxValue:(option.type == discord.ApplicationCommandOptionType.Number && option.maxValue) ? option.maxValue : null,
|
|
@@ -569,7 +587,7 @@ export class ODSlashCommandComparator {
|
|
|
569
587
|
}
|
|
570
588
|
}
|
|
571
589
|
/**Convert a `discord.ApplicationCommandOption` to a universal Open Discord slash command option object for comparison. */
|
|
572
|
-
|
|
590
|
+
protected convertCommandOption(option:discord.ApplicationCommandOption): ODSlashCommandUniversalOption {
|
|
573
591
|
const nameLoc = option.nameLocalizations ?? {}
|
|
574
592
|
const descLoc = option.descriptionLocalizations ?? {}
|
|
575
593
|
|
|
@@ -582,8 +600,8 @@ export class ODSlashCommandComparator {
|
|
|
582
600
|
required:(option.type != discord.ApplicationCommandOptionType.SubcommandGroup && option.type != discord.ApplicationCommandOptionType.Subcommand && option.required) ? true : false,
|
|
583
601
|
|
|
584
602
|
autocomplete:option.autocomplete ?? false,
|
|
585
|
-
choices:(option.type == discord.ApplicationCommandOptionType.String && !option.autocomplete && option.choices) ? option.choices.map((choice) => this
|
|
586
|
-
options:((option.type == discord.ApplicationCommandOptionType.SubcommandGroup || option.type == discord.ApplicationCommandOptionType.Subcommand) && option.options) ? option.options.map((opt) => this
|
|
603
|
+
choices:(option.type == discord.ApplicationCommandOptionType.String && !option.autocomplete && option.choices) ? option.choices.map((choice) => this.convertOptionChoice(choice)) : [],
|
|
604
|
+
options:((option.type == discord.ApplicationCommandOptionType.SubcommandGroup || option.type == discord.ApplicationCommandOptionType.Subcommand) && option.options) ? option.options.map((opt) => this.convertBuilderOption(opt)) : [],
|
|
587
605
|
channelTypes:(option.type == discord.ApplicationCommandOptionType.Channel && option.channelTypes) ? option.channelTypes : [],
|
|
588
606
|
minValue:(option.type == discord.ApplicationCommandOptionType.Number && option.minValue) ? option.minValue : null,
|
|
589
607
|
maxValue:(option.type == discord.ApplicationCommandOptionType.Number && option.maxValue) ? option.maxValue : null,
|
|
@@ -604,7 +622,7 @@ export class ODSlashCommandComparator {
|
|
|
604
622
|
descriptionLocalizations:(Object.keys(descLoc) as discord.Locale[]).map((key) => {return {language:key as `${discord.Locale}`,value:descLoc[key] as string}}),
|
|
605
623
|
guildId:guildId,
|
|
606
624
|
nsfw:builder.nsfw ?? false,
|
|
607
|
-
options:builder.options ? builder.options.map((opt) => this
|
|
625
|
+
options:builder.options ? builder.options.map((opt) => this.convertBuilderOption(opt)) : [],
|
|
608
626
|
defaultMemberPermissions:discord.PermissionsBitField.resolve(builder.defaultMemberPermissions ?? ["ViewChannel"]),
|
|
609
627
|
dmPermission:(builder.contexts && builder.contexts.includes(discord.InteractionContextType.BotDM)) ?? false,
|
|
610
628
|
integrationTypes:builder.integrationTypes ?? [discord.ApplicationIntegrationType.GuildInstall],
|
|
@@ -624,7 +642,7 @@ export class ODSlashCommandComparator {
|
|
|
624
642
|
descriptionLocalizations:(Object.keys(descLoc) as discord.Locale[]).map((key) => {return {language:key as `${discord.Locale}`,value:descLoc[key] as string}}),
|
|
625
643
|
guildId:cmd.guildId,
|
|
626
644
|
nsfw:cmd.nsfw,
|
|
627
|
-
options:cmd.options ? cmd.options.map((opt) => this
|
|
645
|
+
options:cmd.options ? cmd.options.map((opt) => this.convertCommandOption(opt)) : [],
|
|
628
646
|
defaultMemberPermissions:discord.PermissionsBitField.resolve(cmd.defaultMemberPermissions ?? ["ViewChannel"]),
|
|
629
647
|
dmPermission:(cmd.contexts && cmd.contexts.includes(discord.InteractionContextType.BotDM)) ? true : false,
|
|
630
648
|
integrationTypes:cmd.integrationTypes ?? [discord.ApplicationIntegrationType.GuildInstall],
|
|
@@ -790,25 +808,21 @@ export type ODSlashCommandManagerIdConstraint = Record<string,ODSlashCommand>
|
|
|
790
808
|
* Here, you can add & remove slash commands & the bot will do the (de)registering.
|
|
791
809
|
*/
|
|
792
810
|
export class ODSlashCommandManager<IdList extends ODSlashCommandManagerIdConstraint = ODSlashCommandManagerIdConstraint> extends ODManager<ODSlashCommand> {
|
|
793
|
-
/**Alias to Open Discord debugger. */
|
|
794
|
-
#debug: ODDebugger
|
|
795
|
-
|
|
796
811
|
/**Refrerence to discord.js client. */
|
|
797
|
-
|
|
812
|
+
protected client: ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>
|
|
798
813
|
/**Discord.js application commands manager. */
|
|
799
814
|
commandManager: discord.ApplicationCommandManager|null
|
|
800
815
|
/**Collection of all interaction listeners. */
|
|
801
|
-
|
|
816
|
+
protected interactionListeners: {name:string|RegExp, callback:ODSlashCommandInteractionCallback}[] = []
|
|
802
817
|
/**Set the soft limit for maximum amount of listeners. A warning will be shown when there are more listeners than this limit. */
|
|
803
818
|
listenerLimit: number = 100
|
|
804
819
|
/**A utility class used to compare 2 slash commands with each other. */
|
|
805
820
|
comparator: ODSlashCommandComparator = new ODSlashCommandComparator()
|
|
806
821
|
|
|
807
|
-
constructor(debug:ODDebugger,
|
|
822
|
+
constructor(debug:ODDebugger, client:ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>){
|
|
808
823
|
super(debug,"slash command")
|
|
809
|
-
this
|
|
810
|
-
this
|
|
811
|
-
this.commandManager = (manager.client.application) ? manager.client.application.commands : null
|
|
824
|
+
this.client = client
|
|
825
|
+
this.commandManager = (client.client.application) ? client.client.application.commands : null
|
|
812
826
|
}
|
|
813
827
|
|
|
814
828
|
/**Get all registered & unregistered slash commands. */
|
|
@@ -853,7 +867,7 @@ export class ODSlashCommandManager<IdList extends ODSlashCommandManagerIdConstra
|
|
|
853
867
|
}
|
|
854
868
|
/**Create all commands that are not registered yet.*/
|
|
855
869
|
async createNewCommands(instances:ODSlashCommand[],progress?:ODManualProgressBar){
|
|
856
|
-
if (!this
|
|
870
|
+
if (!this.client.ready) throw new ODSystemError("Client isn't ready yet! Unable to register slash commands!")
|
|
857
871
|
if (instances.length > 0 && progress){
|
|
858
872
|
progress.max = instances.length
|
|
859
873
|
progress.start()
|
|
@@ -861,7 +875,7 @@ export class ODSlashCommandManager<IdList extends ODSlashCommandManagerIdConstra
|
|
|
861
875
|
|
|
862
876
|
for (const instance of instances){
|
|
863
877
|
await this.createCmd(instance)
|
|
864
|
-
this
|
|
878
|
+
this.debug?.debug("Created new slash command",[
|
|
865
879
|
{key:"id",value:instance.id.value},
|
|
866
880
|
{key:"name",value:instance.name}
|
|
867
881
|
])
|
|
@@ -870,7 +884,7 @@ export class ODSlashCommandManager<IdList extends ODSlashCommandManagerIdConstra
|
|
|
870
884
|
}
|
|
871
885
|
/**Update all commands that are already registered. */
|
|
872
886
|
async updateExistingCommands(instances:ODSlashCommand[],progress?:ODManualProgressBar){
|
|
873
|
-
if (!this
|
|
887
|
+
if (!this.client.ready) throw new ODSystemError("Client isn't ready yet! Unable to register slash commands!")
|
|
874
888
|
if (instances.length > 0 && progress){
|
|
875
889
|
progress.max = instances.length
|
|
876
890
|
progress.start()
|
|
@@ -878,13 +892,13 @@ export class ODSlashCommandManager<IdList extends ODSlashCommandManagerIdConstra
|
|
|
878
892
|
|
|
879
893
|
for (const instance of instances){
|
|
880
894
|
await this.createCmd(instance)
|
|
881
|
-
this
|
|
895
|
+
this.debug?.debug("Updated existing slash command",[{key:"id",value:instance.id.value},{key:"name",value:instance.name}])
|
|
882
896
|
if (progress) progress.increase(1)
|
|
883
897
|
}
|
|
884
898
|
}
|
|
885
899
|
/**Remove all commands that are registered but unused by Open Discord. */
|
|
886
900
|
async removeUnusedCommands(instances:ODSlashCommandUniversalCommand[],guildId?:string,progress?:ODManualProgressBar){
|
|
887
|
-
if (!this
|
|
901
|
+
if (!this.client.ready) throw new ODSystemError("Client isn't ready yet! Unable to register slash commands!")
|
|
888
902
|
if (!this.commandManager) throw new ODSystemError("Couldn't get client application to register slash commands!")
|
|
889
903
|
if (instances.length > 0 && progress){
|
|
890
904
|
progress.max = instances.length
|
|
@@ -898,7 +912,7 @@ export class ODSlashCommandManager<IdList extends ODSlashCommandManagerIdConstra
|
|
|
898
912
|
if (cmd){
|
|
899
913
|
try {
|
|
900
914
|
await cmd.delete()
|
|
901
|
-
this
|
|
915
|
+
this.debug?.debug("Removed existing slash command",[{key:"name",value:cmd.name},{key:"guildId",value:guildId ?? "/"}])
|
|
902
916
|
}catch(err){
|
|
903
917
|
process.emit("uncaughtException",err)
|
|
904
918
|
throw new ODSystemError("Failed to delete slash command '/"+cmd.name+"'!")
|
|
@@ -919,15 +933,15 @@ export class ODSlashCommandManager<IdList extends ODSlashCommandManagerIdConstra
|
|
|
919
933
|
}
|
|
920
934
|
/**Start listening to the discord.js client `interactionCreate` event. */
|
|
921
935
|
startListeningToInteractions(){
|
|
922
|
-
this
|
|
936
|
+
this.client.client.on("interactionCreate",(interaction) => {
|
|
923
937
|
//return when not in main server or DM
|
|
924
|
-
if (!this
|
|
938
|
+
if (!this.client.mainServer || (interaction.guild && interaction.guild.id != this.client.mainServer.id)) return
|
|
925
939
|
|
|
926
940
|
if (!interaction.isChatInputCommand()) return
|
|
927
941
|
const cmd = this.getFiltered((cmd) => cmd.name == interaction.commandName)[0]
|
|
928
942
|
if (!cmd) return
|
|
929
943
|
|
|
930
|
-
this
|
|
944
|
+
this.interactionListeners.forEach((listener) => {
|
|
931
945
|
if (typeof listener.name == "string" && (interaction.commandName != listener.name)) return
|
|
932
946
|
else if (listener.name instanceof RegExp && !listener.name.test(interaction.commandName)) return
|
|
933
947
|
|
|
@@ -940,14 +954,14 @@ export class ODSlashCommandManager<IdList extends ODSlashCommandManagerIdConstra
|
|
|
940
954
|
onInteraction(commandName:keyof ODNoGeneric<IdList>, callback:ODSlashCommandInteractionCallback): void
|
|
941
955
|
onInteraction(commandName:string|RegExp, callback:ODSlashCommandInteractionCallback): void
|
|
942
956
|
onInteraction(commandName:string|RegExp, callback:ODSlashCommandInteractionCallback){
|
|
943
|
-
this
|
|
957
|
+
this.interactionListeners.push({
|
|
944
958
|
name:commandName,
|
|
945
959
|
callback
|
|
946
960
|
})
|
|
947
961
|
|
|
948
|
-
if (this
|
|
949
|
-
this
|
|
950
|
-
{key:"listeners",value:this
|
|
962
|
+
if (this.interactionListeners.length > this.listenerLimit){
|
|
963
|
+
this.debug?.console.log(new ODWarningConsoleMessage("Possible slash command interaction memory leak detected!",[
|
|
964
|
+
{key:"listeners",value:this.interactionListeners.length.toString()}
|
|
951
965
|
]))
|
|
952
966
|
}
|
|
953
967
|
}
|
|
@@ -1322,26 +1336,24 @@ export type ODTextCommandManagerIdConstraint = Record<string,ODTextCommand>
|
|
|
1322
1336
|
* Here, you can add & remove text commands & the bot will do the (de)registering.
|
|
1323
1337
|
*/
|
|
1324
1338
|
export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstraint = ODTextCommandManagerIdConstraint> extends ODManager<ODTextCommand> {
|
|
1325
|
-
/**Alias to Open Discord debugger. */
|
|
1326
|
-
#debug: ODDebugger
|
|
1327
1339
|
/**Copy of discord.js client. */
|
|
1328
|
-
|
|
1340
|
+
protected client: ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>
|
|
1329
1341
|
/**Collection of all interaction listeners. */
|
|
1330
|
-
|
|
1342
|
+
protected interactionListeners: {prefix:string, name:string|RegExp, callback:ODTextCommandInteractionCallback}[] = []
|
|
1331
1343
|
/**Collection of all error listeners. */
|
|
1332
|
-
|
|
1344
|
+
protected errorListeners: ODTextCommandErrorCallback[] = []
|
|
1333
1345
|
/**Set the soft limit for maximum amount of listeners. A warning will be shown when there are more listeners than this limit. */
|
|
1334
1346
|
listenerLimit: number = 100
|
|
1335
1347
|
|
|
1336
|
-
constructor(debug:ODDebugger,
|
|
1348
|
+
constructor(debug:ODDebugger, client:ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>){
|
|
1337
1349
|
super(debug,"text command")
|
|
1338
|
-
this
|
|
1339
|
-
this
|
|
1350
|
+
this.debug = debug
|
|
1351
|
+
this.client = client
|
|
1340
1352
|
}
|
|
1341
1353
|
|
|
1342
1354
|
/*Check if a message is a registered command. */
|
|
1343
|
-
async
|
|
1344
|
-
if (this
|
|
1355
|
+
private async checkMessage(msg:discord.Message){
|
|
1356
|
+
if (this.client.client.user && msg.author.id == this.client.client.user.id) return false
|
|
1345
1357
|
|
|
1346
1358
|
//filter commands for correct prefix
|
|
1347
1359
|
const validPrefixCommands: {cmd:ODTextCommand,newContent:string}[] = []
|
|
@@ -1354,7 +1366,7 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1354
1366
|
|
|
1355
1367
|
//return when no command with prefix
|
|
1356
1368
|
if (validPrefixCommands.length == 0){
|
|
1357
|
-
this
|
|
1369
|
+
this.errorListeners.forEach((cb) => cb({
|
|
1358
1370
|
type:"unknown_prefix",
|
|
1359
1371
|
msg:msg
|
|
1360
1372
|
}))
|
|
@@ -1372,7 +1384,7 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1372
1384
|
|
|
1373
1385
|
//return when no command with name
|
|
1374
1386
|
if (validNameCommands.length == 0){
|
|
1375
|
-
this
|
|
1387
|
+
this.errorListeners.forEach((cb) => cb({
|
|
1376
1388
|
type:"unknown_command",
|
|
1377
1389
|
msg:msg
|
|
1378
1390
|
}))
|
|
@@ -1390,11 +1402,11 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1390
1402
|
else if (typeof builder.allowedGuildIds != "undefined" && msg.guild && !builder.allowedGuildIds.includes(msg.guild.id)) return false
|
|
1391
1403
|
|
|
1392
1404
|
//check all command options & return when incorrect
|
|
1393
|
-
const options = await this
|
|
1405
|
+
const options = await this.checkOptions(command.cmd,command.newContent,msg)
|
|
1394
1406
|
if (!options.valid) return false
|
|
1395
1407
|
|
|
1396
1408
|
//a command matched this message => emit event
|
|
1397
|
-
this
|
|
1409
|
+
this.interactionListeners.forEach((listener) => {
|
|
1398
1410
|
if (typeof listener.prefix == "string" && (command.cmd.builder.prefix != listener.prefix)) return
|
|
1399
1411
|
if (typeof listener.name == "string" && (command.cmd.name.split(" ")[0] != listener.name)) return
|
|
1400
1412
|
else if (listener.name instanceof RegExp && !listener.name.test(command.cmd.name.split(" ")[0])) return
|
|
@@ -1405,7 +1417,7 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1405
1417
|
return true
|
|
1406
1418
|
}
|
|
1407
1419
|
/**Check if all options of a command are correct. */
|
|
1408
|
-
async
|
|
1420
|
+
private async checkOptions(cmd:ODTextCommand, newContent:string, msg:discord.Message){
|
|
1409
1421
|
const options = cmd.builder.options
|
|
1410
1422
|
if (!options) return {valid:true,data:[]}
|
|
1411
1423
|
|
|
@@ -1416,7 +1428,7 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1416
1428
|
const optionError = (type:"invalid_option"|"missing_option", option:ODTextCommandBuilderOption, location:number, value?:string, reason?:ODTextCommandErrorInvalidOptionReason) => {
|
|
1417
1429
|
//ERROR INVALID
|
|
1418
1430
|
if (type == "invalid_option" && value && reason){
|
|
1419
|
-
this
|
|
1431
|
+
this.errorListeners.forEach((cb) => cb({
|
|
1420
1432
|
type:"invalid_option",
|
|
1421
1433
|
msg:msg,
|
|
1422
1434
|
prefix:cmd.builder.prefix,
|
|
@@ -1428,7 +1440,7 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1428
1440
|
reason
|
|
1429
1441
|
}))
|
|
1430
1442
|
}else if (type == "missing_option"){
|
|
1431
|
-
this
|
|
1443
|
+
this.errorListeners.forEach((cb) => cb({
|
|
1432
1444
|
type:"missing_option",
|
|
1433
1445
|
msg:msg,
|
|
1434
1446
|
prefix:cmd.builder.prefix,
|
|
@@ -1704,7 +1716,7 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1704
1716
|
const userId = res[1]
|
|
1705
1717
|
|
|
1706
1718
|
try{
|
|
1707
|
-
const user = await this
|
|
1719
|
+
const user = await this.client.client.users.fetch(userId)
|
|
1708
1720
|
if (!user){
|
|
1709
1721
|
optionError("invalid_option",option,location,value,"user_not_found")
|
|
1710
1722
|
}else{
|
|
@@ -1755,7 +1767,7 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1755
1767
|
}
|
|
1756
1768
|
}else if (type == "user"){
|
|
1757
1769
|
try{
|
|
1758
|
-
const user = await this
|
|
1770
|
+
const user = await this.client.client.users.fetch(mentionableId)
|
|
1759
1771
|
if (!user){
|
|
1760
1772
|
optionError("invalid_option",option,location,value,"mentionable_not_found")
|
|
1761
1773
|
}else{
|
|
@@ -1783,14 +1795,14 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1783
1795
|
}
|
|
1784
1796
|
/**Start listening to the discord.js client `messageCreate` event. */
|
|
1785
1797
|
startListeningToInteractions(){
|
|
1786
|
-
this
|
|
1798
|
+
this.client.client.on("messageCreate",(msg) => {
|
|
1787
1799
|
//return when not in main server or DM
|
|
1788
|
-
if (!this
|
|
1789
|
-
this
|
|
1800
|
+
if (!this.client.mainServer || (msg.guild && msg.guild.id != this.client.mainServer.id)) return
|
|
1801
|
+
this.checkMessage(msg)
|
|
1790
1802
|
})
|
|
1791
1803
|
}
|
|
1792
1804
|
/**Check if optional values are only present at the end of the command. */
|
|
1793
|
-
|
|
1805
|
+
private checkBuilderOptions(builder:ODTextCommandBuilder): {valid:boolean,reason:"required_after_optional"|"allowspaces_not_last"|null} {
|
|
1794
1806
|
let optionalVisited = false
|
|
1795
1807
|
let valid = true
|
|
1796
1808
|
let reason: "required_after_optional"|"allowspaces_not_last"|null = null
|
|
@@ -1814,21 +1826,21 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1814
1826
|
onInteraction(commandPrefix:string,commandName:keyof ODNoGeneric<IdList>, callback:ODTextCommandInteractionCallback): void
|
|
1815
1827
|
onInteraction(commandPrefix:string,commandName:string|RegExp, callback:ODTextCommandInteractionCallback): void
|
|
1816
1828
|
onInteraction(commandPrefix:string,commandName:string|RegExp, callback:ODTextCommandInteractionCallback){
|
|
1817
|
-
this
|
|
1829
|
+
this.interactionListeners.push({
|
|
1818
1830
|
prefix:commandPrefix,
|
|
1819
1831
|
name:commandName,
|
|
1820
1832
|
callback
|
|
1821
1833
|
})
|
|
1822
1834
|
|
|
1823
|
-
if (this
|
|
1824
|
-
this
|
|
1825
|
-
{key:"listeners",value:this
|
|
1835
|
+
if (this.interactionListeners.length > this.listenerLimit){
|
|
1836
|
+
this.debug?.console.log(new ODWarningConsoleMessage("Possible text command interaction memory leak detected!",[
|
|
1837
|
+
{key:"listeners",value:this.interactionListeners.length.toString()}
|
|
1826
1838
|
]))
|
|
1827
1839
|
}
|
|
1828
1840
|
}
|
|
1829
1841
|
/**Callback on error from all the registered text commands */
|
|
1830
1842
|
onError(callback:ODTextCommandErrorCallback){
|
|
1831
|
-
this
|
|
1843
|
+
this.errorListeners.push(callback)
|
|
1832
1844
|
}
|
|
1833
1845
|
|
|
1834
1846
|
get<TextCommandId extends keyof ODNoGeneric<IdList>>(id:TextCommandId): IdList[TextCommandId]
|
|
@@ -1853,7 +1865,7 @@ export class ODTextCommandManager<IdList extends ODTextCommandManagerIdConstrain
|
|
|
1853
1865
|
}
|
|
1854
1866
|
|
|
1855
1867
|
add(data:ODTextCommand, overwrite?:boolean): boolean {
|
|
1856
|
-
const checkResult = this
|
|
1868
|
+
const checkResult = this.checkBuilderOptions(data.builder)
|
|
1857
1869
|
if (!checkResult.valid && checkResult.reason == "required_after_optional") throw new ODSystemError("Invalid text command '"+data.id.value+"' => optional options are only allowed at the end of a command!")
|
|
1858
1870
|
else if (!checkResult.valid && checkResult.reason == "allowspaces_not_last") throw new ODSystemError("Invalid text command '"+data.id.value+"' => string option with 'allowSpaces' is only allowed at the end of a command!")
|
|
1859
1871
|
else return super.add(data,overwrite)
|
|
@@ -2034,25 +2046,22 @@ export type ODContextMenuManagerIdConstraint = Record<string,ODContextMenu>
|
|
|
2034
2046
|
* Here, you can add & remove context interactions & the bot will do the (de)registering.
|
|
2035
2047
|
*/
|
|
2036
2048
|
export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstraint = ODContextMenuManagerIdConstraint> extends ODManager<ODContextMenu> {
|
|
2037
|
-
/**Alias to Open Discord debugger. */
|
|
2038
|
-
#debug: ODDebugger
|
|
2039
|
-
|
|
2040
2049
|
/**Refrerence to discord.js client. */
|
|
2041
|
-
|
|
2050
|
+
protected client: ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>
|
|
2042
2051
|
/**Discord.js application commands manager. */
|
|
2043
2052
|
commandManager: discord.ApplicationCommandManager|null
|
|
2044
2053
|
/**Collection of all interaction listeners. */
|
|
2045
|
-
|
|
2054
|
+
protected interactionListeners: {name:string|RegExp, callback:ODContextMenuInteractionCallback}[] = []
|
|
2046
2055
|
/**Set the soft limit for maximum amount of listeners. A warning will be shown when there are more listeners than this limit. */
|
|
2047
2056
|
listenerLimit: number = 100
|
|
2048
2057
|
/**A utility class used to compare 2 context menus with each other. */
|
|
2049
2058
|
comparator: ODContextMenuComparator = new ODContextMenuComparator()
|
|
2050
2059
|
|
|
2051
|
-
constructor(debug:ODDebugger,
|
|
2060
|
+
constructor(debug:ODDebugger, client:ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>){
|
|
2052
2061
|
super(debug,"context menu")
|
|
2053
|
-
this
|
|
2054
|
-
this
|
|
2055
|
-
this.commandManager = (
|
|
2062
|
+
this.debug = debug
|
|
2063
|
+
this.client = client
|
|
2064
|
+
this.commandManager = (client.client.application) ? client.client.application.commands : null
|
|
2056
2065
|
}
|
|
2057
2066
|
|
|
2058
2067
|
/**Get all registered & unregistered message context menu commands. */
|
|
@@ -2097,7 +2106,7 @@ export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstrain
|
|
|
2097
2106
|
}
|
|
2098
2107
|
/**Create all context menus that are not registered yet.*/
|
|
2099
2108
|
async createNewMenus(instances:ODContextMenu[],progress?:ODManualProgressBar){
|
|
2100
|
-
if (!this
|
|
2109
|
+
if (!this.client.ready) throw new ODSystemError("Client isn't ready yet! Unable to register context menus!")
|
|
2101
2110
|
if (instances.length > 0 && progress){
|
|
2102
2111
|
progress.max = instances.length
|
|
2103
2112
|
progress.start()
|
|
@@ -2105,7 +2114,7 @@ export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstrain
|
|
|
2105
2114
|
|
|
2106
2115
|
for (const instance of instances){
|
|
2107
2116
|
await this.createMenu(instance)
|
|
2108
|
-
this
|
|
2117
|
+
this.debug?.debug("Created new context menu",[
|
|
2109
2118
|
{key:"id",value:instance.id.value},
|
|
2110
2119
|
{key:"name",value:instance.name},
|
|
2111
2120
|
{key:"type",value:(instance.builder.type == discord.ApplicationCommandType.Message) ? "message-context" : "user-context"}
|
|
@@ -2115,7 +2124,7 @@ export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstrain
|
|
|
2115
2124
|
}
|
|
2116
2125
|
/**Update all context menus that are already registered. */
|
|
2117
2126
|
async updateExistingMenus(instances:ODContextMenu[],progress?:ODManualProgressBar){
|
|
2118
|
-
if (!this
|
|
2127
|
+
if (!this.client.ready) throw new ODSystemError("Client isn't ready yet! Unable to register context menus!")
|
|
2119
2128
|
if (instances.length > 0 && progress){
|
|
2120
2129
|
progress.max = instances.length
|
|
2121
2130
|
progress.start()
|
|
@@ -2123,7 +2132,7 @@ export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstrain
|
|
|
2123
2132
|
|
|
2124
2133
|
for (const instance of instances){
|
|
2125
2134
|
await this.createMenu(instance)
|
|
2126
|
-
this
|
|
2135
|
+
this.debug?.debug("Updated existing context menu",[
|
|
2127
2136
|
{key:"id",value:instance.id.value},
|
|
2128
2137
|
{key:"name",value:instance.name},
|
|
2129
2138
|
{key:"type",value:(instance.builder.type == discord.ApplicationCommandType.Message) ? "message-context" : "user-context"}
|
|
@@ -2133,7 +2142,7 @@ export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstrain
|
|
|
2133
2142
|
}
|
|
2134
2143
|
/**Remove all context menus that are registered but unused by Open Discord. */
|
|
2135
2144
|
async removeUnusedMenus(instances:ODContextMenuUniversalMenu[],guildId?:string,progress?:ODManualProgressBar){
|
|
2136
|
-
if (!this
|
|
2145
|
+
if (!this.client.ready) throw new ODSystemError("Client isn't ready yet! Unable to register context menus!")
|
|
2137
2146
|
if (!this.commandManager) throw new ODSystemError("Couldn't get client application to register context menus!")
|
|
2138
2147
|
if (instances.length > 0 && progress){
|
|
2139
2148
|
progress.max = instances.length
|
|
@@ -2147,7 +2156,7 @@ export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstrain
|
|
|
2147
2156
|
if (menu){
|
|
2148
2157
|
try {
|
|
2149
2158
|
await menu.delete()
|
|
2150
|
-
this
|
|
2159
|
+
this.debug?.debug("Removed existing context menu",[
|
|
2151
2160
|
{key:"name",value:menu.name},
|
|
2152
2161
|
{key:"guildId",value:guildId ?? "/"},
|
|
2153
2162
|
{key:"type",value:(instance.type == discord.ApplicationCommandType.Message) ? "message-context" : "user-context"}
|
|
@@ -2172,15 +2181,15 @@ export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstrain
|
|
|
2172
2181
|
}
|
|
2173
2182
|
/**Start listening to the discord.js client `interactionCreate` event. */
|
|
2174
2183
|
startListeningToInteractions(){
|
|
2175
|
-
this
|
|
2184
|
+
this.client.client.on("interactionCreate",(interaction) => {
|
|
2176
2185
|
//return when not in main server or DM
|
|
2177
|
-
if (!this
|
|
2186
|
+
if (!this.client.mainServer || (interaction.guild && interaction.guild.id != this.client.mainServer.id)) return
|
|
2178
2187
|
|
|
2179
2188
|
if (!interaction.isContextMenuCommand()) return
|
|
2180
2189
|
const menu = this.getFiltered((menu) => menu.name == interaction.commandName)[0]
|
|
2181
2190
|
if (!menu) return
|
|
2182
2191
|
|
|
2183
|
-
this
|
|
2192
|
+
this.interactionListeners.forEach((listener) => {
|
|
2184
2193
|
if (typeof listener.name == "string" && (interaction.commandName != listener.name)) return
|
|
2185
2194
|
else if (listener.name instanceof RegExp && !listener.name.test(interaction.commandName)) return
|
|
2186
2195
|
|
|
@@ -2194,14 +2203,14 @@ export class ODContextMenuManager<IdList extends ODContextMenuManagerIdConstrain
|
|
|
2194
2203
|
onInteraction(menuName:string|RegExp, callback:ODContextMenuInteractionCallback): void
|
|
2195
2204
|
|
|
2196
2205
|
onInteraction(menuName:string|RegExp, callback:ODContextMenuInteractionCallback){
|
|
2197
|
-
this
|
|
2206
|
+
this.interactionListeners.push({
|
|
2198
2207
|
name:menuName,
|
|
2199
2208
|
callback
|
|
2200
2209
|
})
|
|
2201
2210
|
|
|
2202
|
-
if (this
|
|
2203
|
-
this
|
|
2204
|
-
{key:"listeners",value:this
|
|
2211
|
+
if (this.interactionListeners.length > this.listenerLimit){
|
|
2212
|
+
this.debug?.console.log("Possible context menu interaction memory leak detected!","warning",[
|
|
2213
|
+
{key:"listeners",value:this.interactionListeners.length.toString()}
|
|
2205
2214
|
])
|
|
2206
2215
|
}
|
|
2207
2216
|
}
|
|
@@ -2282,31 +2291,30 @@ export type ODAutocompleteInteractionCallback = (interaction:discord.Autocomplet
|
|
|
2282
2291
|
*/
|
|
2283
2292
|
export class ODAutocompleteManager {
|
|
2284
2293
|
/**Alias to Open Discord debugger. */
|
|
2285
|
-
|
|
2286
|
-
|
|
2294
|
+
protected debug: ODDebugger
|
|
2287
2295
|
/**Refrerence to discord.js client. */
|
|
2288
|
-
|
|
2296
|
+
protected client: ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>
|
|
2289
2297
|
/**Discord.js application commands manager. */
|
|
2290
2298
|
commandManager: discord.ApplicationCommandManager|null
|
|
2291
2299
|
/**Collection of all interaction listeners. */
|
|
2292
|
-
|
|
2300
|
+
protected interactionListeners: {cmdName:string|RegExp, optName:string|RegExp, callback:ODAutocompleteInteractionCallback}[] = []
|
|
2293
2301
|
/**Set the soft limit for maximum amount of listeners. A warning will be shown when there are more listeners than this limit. */
|
|
2294
2302
|
listenerLimit: number = 100
|
|
2295
2303
|
|
|
2296
|
-
constructor(debug:ODDebugger,
|
|
2297
|
-
this
|
|
2298
|
-
this
|
|
2299
|
-
this.commandManager = (
|
|
2304
|
+
constructor(debug:ODDebugger, client:ODClientManager<ODSlashCommandManagerIdConstraint,ODTextCommandManagerIdConstraint,ODContextMenuManagerIdConstraint>){
|
|
2305
|
+
this.debug = debug
|
|
2306
|
+
this.client = client
|
|
2307
|
+
this.commandManager = (client.client.application) ? client.client.application.commands : null
|
|
2300
2308
|
}
|
|
2301
2309
|
|
|
2302
2310
|
/**Start listening to the discord.js client `interactionCreate` event. */
|
|
2303
2311
|
startListeningToInteractions(){
|
|
2304
|
-
this
|
|
2312
|
+
this.client.client.on("interactionCreate",(interaction) => {
|
|
2305
2313
|
//return when not in main server or DM
|
|
2306
|
-
if (!this
|
|
2314
|
+
if (!this.client.mainServer || (interaction.guild && interaction.guild.id != this.client.mainServer.id)) return
|
|
2307
2315
|
|
|
2308
2316
|
if (!interaction.isAutocomplete()) return
|
|
2309
|
-
this
|
|
2317
|
+
this.interactionListeners.forEach((listener) => {
|
|
2310
2318
|
|
|
2311
2319
|
if (typeof listener.cmdName == "string" && (interaction.commandName != listener.cmdName)) return
|
|
2312
2320
|
else if (listener.cmdName instanceof RegExp && !listener.cmdName.test(interaction.commandName)) return
|
|
@@ -2320,13 +2328,13 @@ export class ODAutocompleteManager {
|
|
|
2320
2328
|
}
|
|
2321
2329
|
/**Callback on interaction from one or multiple autocompletes. */
|
|
2322
2330
|
onInteraction(cmdName:string|RegExp,optName:string|RegExp,callback:ODAutocompleteInteractionCallback){
|
|
2323
|
-
this
|
|
2331
|
+
this.interactionListeners.push({
|
|
2324
2332
|
cmdName,optName,callback
|
|
2325
2333
|
})
|
|
2326
2334
|
|
|
2327
|
-
if (this
|
|
2328
|
-
this
|
|
2329
|
-
{key:"listeners",value:this
|
|
2335
|
+
if (this.interactionListeners.length > this.listenerLimit){
|
|
2336
|
+
this.debug.console.log("Possible autocomplete interaction memory leak detected!","warning",[
|
|
2337
|
+
{key:"listeners",value:this.interactionListeners.length.toString()}
|
|
2330
2338
|
])
|
|
2331
2339
|
}
|
|
2332
2340
|
}
|