11import debug from "debug" ;
2- import { Collection } from "discord.js" ;
2+ import { Collection , Snowflake } from "discord.js" ;
33import { existsSync } from "node:fs" ;
44import { lstat , readdir } from "node:fs/promises" ;
55import { basename , resolve } from "node:path" ;
@@ -93,6 +93,9 @@ export default class DiscordModuleLoader {
9393 if ( this . guilds . has ( guild . id ) )
9494 throw new Error ( `Cannot add ${ guild . id } more than once.` ) ;
9595
96+ this . guilds . set ( guild . id , guild ) ;
97+ returnGuilds . push ( [ guild . id , guild ] ) ;
98+
9699 if ( existsSync ( resolve ( dir , folder , "events" ) ) )
97100 this . addToColl (
98101 guild . events ,
@@ -111,8 +114,6 @@ export default class DiscordModuleLoader {
111114 await this . loadModules ( resolve ( dir , folder , "modules" ) , guild . id )
112115 ) ;
113116
114- this . guilds . set ( guild . id , guild ) ;
115- returnGuilds . push ( [ guild . id , guild ] ) ;
116117 log ( "Loaded guild module for guild: %s" , guild . id ) ;
117118 }
118119 return returnGuilds ;
@@ -188,7 +189,11 @@ export default class DiscordModuleLoader {
188189 if ( ! ( event instanceof DiscordEvent ) )
189190 throw new Error ( `Event ${ file } is not an Event` ) ;
190191
191- if ( guildId ) event . guildId = guildId ;
192+ if ( guildId ) {
193+ this . guilds . get ( guildId ) ! . events . set ( event . event , event ) ;
194+
195+ event . guildId = guildId ;
196+ }
192197
193198 this . client . on ( event . event , ( ...args ) => {
194199 if (
@@ -206,7 +211,7 @@ export default class DiscordModuleLoader {
206211 return returnEvents ;
207212 }
208213
209- async loadCommands ( dir = "commands" , globalCommands : true | string = true ) {
214+ async loadCommands ( dir = "commands" , guildId ?: Snowflake ) {
210215 dir = resolve ( dir ) ;
211216 if ( ! existsSync ( dir ) ) return [ ] ;
212217
@@ -225,8 +230,12 @@ export default class DiscordModuleLoader {
225230 if ( this . commands . has ( command . name . toLowerCase ( ) ) )
226231 throw new Error ( `Cannot add ${ command . name } more than once.` ) ;
227232
228- if ( globalCommands !== true ) {
229- command . guildId = globalCommands ;
233+ if ( guildId ) {
234+ this . guilds
235+ . get ( guildId ) !
236+ . commands . set ( command . name . toLowerCase ( ) , command ) ;
237+
238+ command . guildId = guildId ;
230239 }
231240
232241 this . commands . set ( command . name . toLowerCase ( ) , command ) ;
@@ -338,6 +347,18 @@ export default class DiscordModuleLoader {
338347 }
339348
340349 private async handleInteraction ( interaction : Interaction < CacheType > ) {
350+ if ( interaction . isAutocomplete ( ) ) {
351+ const command = this . commands . get ( interaction . commandName . toLowerCase ( ) ) ;
352+
353+ if ( ! command ) return await interaction . respond ( [ ] ) ;
354+
355+ try {
356+ await command . execute ( interaction ) ;
357+ } catch ( err ) {
358+ console . error ( err ) ;
359+ }
360+ }
361+
341362 if ( interaction . isCommand ( ) || interaction . isContextMenu ( ) ) {
342363 const command = this . commands . get ( interaction . commandName . toLowerCase ( ) ) ;
343364 if ( ! command )
0 commit comments