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
|
||||
/**
|
||||
* A Slash Command was triggered
|
||||
* An Interaction was created
|
||||
*/
|
||||
interactionCreate: (interaction: Interaction) => void
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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'
|
||||
// )
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue