@@ -34,7 +34,7 @@ export class SlashCommand {
3434 this . applicationID = data . application_id
3535 this . name = data . name
3636 this . description = data . description
37- this . options = data . options
37+ this . options = data . options ?? [ ]
3838 }
3939
4040 async delete ( ) : Promise < void > {
@@ -158,6 +158,8 @@ export type SlashCommandHandlerCallback = (interaction: Interaction) => any
158158export interface SlashCommandHandler {
159159 name : string
160160 guild ?: string
161+ parent ?: string
162+ group ?: string
161163 handler : SlashCommandHandlerCallback
162164}
163165
@@ -182,38 +184,45 @@ export class SlashClient {
182184 }
183185
184186 this . client . on ( 'interactionCreate' , ( interaction ) =>
185- this . process ( interaction )
187+ this . _process ( interaction )
186188 )
187189 }
188190
189191 /** Adds a new Slash Command Handler */
190- handle (
191- name : string ,
192- handler : SlashCommandHandlerCallback ,
193- guild ?: string
194- ) : SlashClient {
195- this . handlers . push ( {
196- name,
197- guild,
198- handler
199- } )
192+ handle ( handler : SlashCommandHandler ) : SlashClient {
193+ this . handlers . push ( handler )
200194 return this
201195 }
202196
197+ private _getCommand ( i : Interaction ) : SlashCommandHandler | undefined {
198+ return this . handlers . find ( ( e ) => {
199+ const hasGroupOrParent = e . group !== undefined || e . parent !== undefined
200+ const groupMatched =
201+ e . group !== undefined && e . parent !== undefined
202+ ? i . options
203+ . find ( ( o ) => o . name === e . group )
204+ ?. options ?. find ( ( o ) => o . name === e . name ) !== undefined
205+ : true
206+ const subMatched =
207+ e . group === undefined && e . parent !== undefined
208+ ? i . options . find ( ( o ) => o . name === e . name ) !== undefined
209+ : true
210+ const nameMatched1 = e . name === i . name
211+ const parentMatched = hasGroupOrParent ? e . parent === i . name : true
212+ const nameMatched = hasGroupOrParent ? parentMatched : nameMatched1
213+
214+ const matched = groupMatched && subMatched && nameMatched
215+ return matched
216+ } )
217+ }
218+
203219 /** Process an incoming Slash Command (interaction) */
204- private process ( interaction : Interaction ) : void {
220+ private _process ( interaction : Interaction ) : void {
205221 if ( ! this . enabled ) return
206222
207223 if ( interaction . type !== InteractionType . APPLICATION_COMMAND ) return
208224
209- let cmd
210-
211- if ( interaction . guild !== undefined )
212- cmd =
213- this . handlers . find (
214- ( e ) => e . guild !== undefined && e . name === interaction . name
215- ) ?? this . handlers . find ( ( e ) => e . name === interaction . name )
216- else cmd = this . handlers . find ( ( e ) => e . name === interaction . name )
225+ const cmd = this . _getCommand ( interaction )
217226
218227 if ( cmd === undefined ) return
219228
0 commit comments