From e0154ceec1262758b21a0a8a8a4382364117e27b Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 16 Dec 2020 05:05:26 -0800 Subject: [PATCH] feat(slash): add support for sub and group handling (this drove me crazy) --- src/gateway/handlers/index.ts | 2 +- src/models/client.ts | 25 ++++++++++++++ src/models/slashClient.ts | 36 ++++++++++++++------ src/test/music.ts | 63 +++++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 11 deletions(-) diff --git a/src/gateway/handlers/index.ts b/src/gateway/handlers/index.ts index af1b69d..a85d8fc 100644 --- a/src/gateway/handlers/index.ts +++ b/src/gateway/handlers/index.ts @@ -330,7 +330,7 @@ export interface ClientEvents extends EventTypes { */ webhooksUpdate: (guild: Guild, channel: GuildTextChannel) => void /** - * A Slash Command was triggered + * An Interaction was created */ interactionCreate: (interaction: Interaction) => void } diff --git a/src/models/client.ts b/src/models/client.ts index 953d272..a807fb9 100644 --- a/src/models/client.ts +++ b/src/models/client.ts @@ -95,6 +95,7 @@ export class Client extends EventEmitter { name: string guild?: string parent?: string + group?: string 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() { return function (client: Client, prop: string) { if (client._decoratedSlashModules === undefined) diff --git a/src/models/slashClient.ts b/src/models/slashClient.ts index 85b356b..5115424 100644 --- a/src/models/slashClient.ts +++ b/src/models/slashClient.ts @@ -159,6 +159,7 @@ export interface SlashCommandHandler { name: string guild?: string parent?: string + group?: string handler: SlashCommandHandlerCallback } @@ -183,7 +184,7 @@ export class SlashClient { } this.client.on('interactionCreate', (interaction) => - this.process(interaction) + this._process(interaction) ) } @@ -201,20 +202,35 @@ export class SlashClient { 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) */ - private process(interaction: Interaction): void { + private _process(interaction: Interaction): void { if (!this.enabled) return if (interaction.type !== InteractionType.APPLICATION_COMMAND) return - let cmd - - 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) + const cmd = this._getCommand(interaction) if (cmd === undefined) return diff --git a/src/test/music.ts b/src/test/music.ts index 018315c..3801237 100644 --- a/src/test/music.ts +++ b/src/test/music.ts @@ -3,6 +3,8 @@ import { event, Intents, command, + subslash, + groupslash, CommandContext, Extension, Collection @@ -12,6 +14,9 @@ import { Manager, Player } 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 = [ { @@ -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() ready(): void { console.log(`Logged in as ${this.user?.tag}!`) 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' + // ) } }