feat(slash): add support for sub and group handling (this drove me crazy)
This commit is contained in:
		
							parent
							
								
									999fe88aa4
								
							
						
					
					
						commit
						e0154ceec1
					
				
					 4 changed files with 115 additions and 11 deletions
				
			
		|  | @ -330,7 +330,7 @@ export interface ClientEvents extends EventTypes { | ||||||
|    */ |    */ | ||||||
|   webhooksUpdate: (guild: Guild, channel: GuildTextChannel) => void |   webhooksUpdate: (guild: Guild, channel: GuildTextChannel) => void | ||||||
|   /** |   /** | ||||||
|    * A Slash Command was triggered |    * An Interaction was created | ||||||
|    */ |    */ | ||||||
|   interactionCreate: (interaction: Interaction) => void |   interactionCreate: (interaction: Interaction) => void | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -95,6 +95,7 @@ export class Client extends EventEmitter { | ||||||
|     name: string |     name: string | ||||||
|     guild?: string |     guild?: string | ||||||
|     parent?: string |     parent?: string | ||||||
|  |     group?: string | ||||||
|     handler: (interaction: Interaction) => any |     handler: (interaction: Interaction) => any | ||||||
|   }> |   }> | ||||||
| 
 | 
 | ||||||
|  | @ -255,6 +256,30 @@ export function subslash(parent: string, name?: string, guild?: string) { | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | export function groupslash( | ||||||
|  |   parent: string, | ||||||
|  |   group: string, | ||||||
|  |   name?: string, | ||||||
|  |   guild?: string | ||||||
|  | ) { | ||||||
|  |   return function (client: Client | SlashModule, prop: string) { | ||||||
|  |     if (client._decoratedSlash === undefined) client._decoratedSlash = [] | ||||||
|  |     const item = (client as { [name: string]: any })[prop] | ||||||
|  |     if (typeof item !== 'function') { | ||||||
|  |       item.parent = parent | ||||||
|  |       item.group = group | ||||||
|  |       client._decoratedSlash.push(item) | ||||||
|  |     } else | ||||||
|  |       client._decoratedSlash.push({ | ||||||
|  |         group, | ||||||
|  |         parent, | ||||||
|  |         name: name ?? prop, | ||||||
|  |         guild, | ||||||
|  |         handler: item | ||||||
|  |       }) | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
| export function slashModule() { | export function slashModule() { | ||||||
|   return function (client: Client, prop: string) { |   return function (client: Client, prop: string) { | ||||||
|     if (client._decoratedSlashModules === undefined) |     if (client._decoratedSlashModules === undefined) | ||||||
|  |  | ||||||
|  | @ -159,6 +159,7 @@ export interface SlashCommandHandler { | ||||||
|   name: string |   name: string | ||||||
|   guild?: string |   guild?: string | ||||||
|   parent?: string |   parent?: string | ||||||
|  |   group?: string | ||||||
|   handler: SlashCommandHandlerCallback |   handler: SlashCommandHandlerCallback | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -183,7 +184,7 @@ export class SlashClient { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     this.client.on('interactionCreate', (interaction) => |     this.client.on('interactionCreate', (interaction) => | ||||||
|       this.process(interaction) |       this._process(interaction) | ||||||
|     ) |     ) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | @ -201,20 +202,35 @@ export class SlashClient { | ||||||
|     return this |     return this | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   private _getCommand(i: Interaction): SlashCommandHandler | undefined { | ||||||
|  |     return this.handlers.find((e) => { | ||||||
|  |       const hasGroupOrParent = e.group !== undefined || e.parent !== undefined | ||||||
|  |       const groupMatched = | ||||||
|  |         e.group !== undefined && e.parent !== undefined | ||||||
|  |           ? i.options | ||||||
|  |               .find((o) => o.name === e.group) | ||||||
|  |               ?.options?.find((o) => o.name === e.name) !== undefined | ||||||
|  |           : true | ||||||
|  |       const subMatched = | ||||||
|  |         e.group === undefined && e.parent !== undefined | ||||||
|  |           ? i.options.find((o) => o.name === e.name) !== undefined | ||||||
|  |           : true | ||||||
|  |       const nameMatched1 = e.name === i.name | ||||||
|  |       const parentMatched = hasGroupOrParent ? e.parent === i.name : true | ||||||
|  |       const nameMatched = hasGroupOrParent ? parentMatched : nameMatched1 | ||||||
|  | 
 | ||||||
|  |       const matched = groupMatched && subMatched && nameMatched | ||||||
|  |       return matched | ||||||
|  |     }) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   /** Process an incoming Slash Command (interaction) */ |   /** Process an incoming Slash Command (interaction) */ | ||||||
|   private process(interaction: Interaction): void { |   private _process(interaction: Interaction): void { | ||||||
|     if (!this.enabled) return |     if (!this.enabled) return | ||||||
| 
 | 
 | ||||||
|     if (interaction.type !== InteractionType.APPLICATION_COMMAND) return |     if (interaction.type !== InteractionType.APPLICATION_COMMAND) return | ||||||
| 
 | 
 | ||||||
|     let cmd |     const cmd = this._getCommand(interaction) | ||||||
| 
 |  | ||||||
|     if (interaction.guild !== undefined) |  | ||||||
|       cmd = |  | ||||||
|         this.handlers.find( |  | ||||||
|           (e) => e.guild !== undefined && e.name === interaction.name |  | ||||||
|         ) ?? this.handlers.find((e) => e.name === interaction.name) |  | ||||||
|     else cmd = this.handlers.find((e) => e.name === interaction.name) |  | ||||||
| 
 | 
 | ||||||
|     if (cmd === undefined) return |     if (cmd === undefined) return | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -3,6 +3,8 @@ import { | ||||||
|   event, |   event, | ||||||
|   Intents, |   Intents, | ||||||
|   command, |   command, | ||||||
|  |   subslash, | ||||||
|  |   groupslash, | ||||||
|   CommandContext, |   CommandContext, | ||||||
|   Extension, |   Extension, | ||||||
|   Collection |   Collection | ||||||
|  | @ -12,6 +14,9 @@ import { | ||||||
|   Manager, |   Manager, | ||||||
|   Player |   Player | ||||||
| } from 'https://raw.githubusercontent.com/Lavaclient/lavadeno/master/mod.ts' | } from 'https://raw.githubusercontent.com/Lavaclient/lavadeno/master/mod.ts' | ||||||
|  | import { Interaction } from '../structures/slash.ts' | ||||||
|  | import { slash } from '../models/client.ts' | ||||||
|  | // import { SlashCommandOptionType } from '../types/slash.ts'
 | ||||||
| 
 | 
 | ||||||
| export const nodes = [ | export const nodes = [ | ||||||
|   { |   { | ||||||
|  | @ -54,10 +59,68 @@ class MyClient extends CommandClient { | ||||||
|     }) |     }) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   @subslash('cmd', 'sub-cmd-no-grp') | ||||||
|  |   subCmdNoGrp(d: Interaction): void { | ||||||
|  |     d.respond({ content: 'sub-cmd-no-group worked' }) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @groupslash('cmd', 'sub-cmd-group', 'sub-cmd') | ||||||
|  |   subCmdGrp(d: Interaction): void { | ||||||
|  |     d.respond({ content: 'sub-cmd-group worked' }) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   @slash() | ||||||
|  |   run(d: Interaction): void { | ||||||
|  |     console.log(d.name) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   @event() |   @event() | ||||||
|   ready(): void { |   ready(): void { | ||||||
|     console.log(`Logged in as ${this.user?.tag}!`) |     console.log(`Logged in as ${this.user?.tag}!`) | ||||||
|     this.manager.init(this.user?.id as string) |     this.manager.init(this.user?.id as string) | ||||||
|  |     // client.slash.commands.create(
 | ||||||
|  |     //   {
 | ||||||
|  |     //     name: 'cmd',
 | ||||||
|  |     //     description: 'Parent command',
 | ||||||
|  |     //     options: [
 | ||||||
|  |     //       {
 | ||||||
|  |     //         name: 'sub-cmd-group',
 | ||||||
|  |     //         type: SlashCommandOptionType.SUB_COMMAND_GROUP,
 | ||||||
|  |     //         description: 'Sub Cmd Group',
 | ||||||
|  |     //         options: [
 | ||||||
|  |     //           {
 | ||||||
|  |     //             name: 'sub-cmd',
 | ||||||
|  |     //             type: SlashCommandOptionType.SUB_COMMAND,
 | ||||||
|  |     //             description: 'Sub Cmd'
 | ||||||
|  |     //           }
 | ||||||
|  |     //         ]
 | ||||||
|  |     //       },
 | ||||||
|  |     //       {
 | ||||||
|  |     //         name: 'sub-cmd-no-grp',
 | ||||||
|  |     //         type: SlashCommandOptionType.SUB_COMMAND,
 | ||||||
|  |     //         description: 'Sub Cmd'
 | ||||||
|  |     //       },
 | ||||||
|  |     //       {
 | ||||||
|  |     //         name: 'sub-cmd-grp-2',
 | ||||||
|  |     //         type: SlashCommandOptionType.SUB_COMMAND_GROUP,
 | ||||||
|  |     //         description: 'Sub Cmd Group 2',
 | ||||||
|  |     //         options: [
 | ||||||
|  |     //           {
 | ||||||
|  |     //             name: 'sub-cmd-1',
 | ||||||
|  |     //             type: SlashCommandOptionType.SUB_COMMAND,
 | ||||||
|  |     //             description: 'Sub Cmd 1'
 | ||||||
|  |     //           },
 | ||||||
|  |     //           {
 | ||||||
|  |     //             name: 'sub-cmd-2',
 | ||||||
|  |     //             type: SlashCommandOptionType.SUB_COMMAND,
 | ||||||
|  |     //             description: 'Sub Cmd 2'
 | ||||||
|  |     //           }
 | ||||||
|  |     //         ]
 | ||||||
|  |     //       }
 | ||||||
|  |     //     ]
 | ||||||
|  |     //   },
 | ||||||
|  |     //   '783319033205751809'
 | ||||||
|  |     // )
 | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue