Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-tires-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'harmonix': patch
---

Add subcommands and subcommand groups handling
74 changes: 61 additions & 13 deletions packages/harmonix/src/discord/builders/slash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import {
ChannelType,
InteractionContextType,
PermissionFlagsBits,
SlashCommandBuilder
SlashCommandBuilder,
SlashCommandSubcommandBuilder
} from 'discord.js'

import { toArray } from '../../utils/helpers'

import type { HarmonixSlashCommand } from '../../types/module'
import type {
HarmonixSlashCommand,
HarmonixSlashCommandWithOptions,
HarmonixSlashCommandWithSubs
} from '../../types/module'
import type {
AttachmentOption,
BooleanOption,
Expand Down Expand Up @@ -44,12 +49,55 @@ export const buildSlashCommand = (command: HarmonixSlashCommand) => {
builder.setContexts(contexts)
}

addOptions(builder, command.options)
if ('subcommands' in command && command.subcommands) {
for (const [name, subOrGroup] of Object.entries(command.subcommands)) {
if ('subcommands' in subOrGroup) {
builder.addSubcommandGroup((groupBuilder) => {
groupBuilder
.setName(subOrGroup.name ?? name)
.setDescription(subOrGroup.description)

for (const [subName, sub] of Object.entries(subOrGroup.subcommands)) {
groupBuilder.addSubcommand((subBuilder) => {
subBuilder
.setName(sub.name ?? subName)
.setDescription(sub.description)

if (sub.options) {
addOptions(subBuilder, sub.options)
}

return subBuilder
})
}

return groupBuilder
})
} else {
builder.addSubcommand((subBuilder) => {
subBuilder
.setName(subOrGroup.name ?? name)
.setDescription(subOrGroup.description)

if (subOrGroup.options) {
addOptions(subBuilder, subOrGroup.options)
}

return subBuilder
})
}
}
} else if ('options' in command && command.options) {
addOptions(builder, command.options)
}

return builder.toJSON()
}

const addOptions = (builder: SlashCommandBuilder, options: SlashOptionMap) => {
const addOptions = (
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
options: SlashOptionMap
) => {
for (const [name, option] of Object.entries(options)) {
switch (option.type) {
case 'String': {
Expand Down Expand Up @@ -93,7 +141,7 @@ const addOptions = (builder: SlashCommandBuilder, options: SlashOptionMap) => {
}

const addStringOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: StringOption
) => {
Expand All @@ -112,7 +160,7 @@ const addStringOption = (
}

const addIntegerOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: IntegerOption
) => {
Expand All @@ -131,7 +179,7 @@ const addIntegerOption = (
}

const addNumberOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: NumberOption
) => {
Expand All @@ -150,7 +198,7 @@ const addNumberOption = (
}

const addBooleanOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: BooleanOption
) => {
Expand All @@ -164,7 +212,7 @@ const addBooleanOption = (
}

const addChannelOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: ChannelOption
) => {
Expand All @@ -186,7 +234,7 @@ const addChannelOption = (
}

const addUserOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: UserOption
) => {
Expand All @@ -200,7 +248,7 @@ const addUserOption = (
}

const addRoleOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: RoleOption
) => {
Expand All @@ -214,7 +262,7 @@ const addRoleOption = (
}

const addMentionableOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: MentionableOption
) => {
Expand All @@ -228,7 +276,7 @@ const addMentionableOption = (
}

const addAttachmentOption = (
builder: SlashCommandBuilder,
builder: SlashCommandBuilder | SlashCommandSubcommandBuilder,
name: string,
option: AttachmentOption
) => {
Expand Down
65 changes: 57 additions & 8 deletions packages/harmonix/src/discord/handlers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,12 @@ export const handleCommandInteraction = async (
switch (command.commandType) {
case CommandType.Slash: {
if (!interaction.isChatInputCommand()) break

const options = parseSlashOptions(interaction, command.options)

await runPipeline(interaction, command.middleware, async (i) =>
command.handler(i as any, options)
)
await handleSlashCommand(interaction, command)
break
}

case CommandType.UserContextMenu: {
if (!interaction.isUserContextMenuCommand()) break

await runPipeline(interaction, command.middleware, async (i) =>
command.handler(i as any)
)
Expand All @@ -45,7 +39,6 @@ export const handleCommandInteraction = async (

case CommandType.MessageContextMenu: {
if (!interaction.isMessageContextMenuCommand()) break

await runPipeline(interaction, command.middleware, async (i) =>
command.handler(i as any)
)
Expand All @@ -54,6 +47,62 @@ export const handleCommandInteraction = async (
}
}

export const handleSlashCommand = async (
interaction: ChatInputCommandInteraction,
command: AnyCommand
) => {
if ('subcommands' in command) {
const target = resolveSubcomand(interaction, command)

if (!target) return
const { sub, options } = target

await runPipeline(interaction, command.middleware, async (i) =>
sub.handler(i as any, options)
)

return
}

if ('options' in command && 'handler' in command) {
const options = parseSlashOptions(interaction, command.options ?? {})

await runPipeline(interaction, command.middleware, async (i) =>
command.handler(i as any, options)
)
}
}

const resolveSubcomand = (
interaction: ChatInputCommandInteraction,
command: Extract<AnyCommand, { subcommands: any }>
) => {
const groupName = interaction.options.getSubcommandGroup(false)
const subName = interaction.options.getSubcommand(false)

if (!subName) return null

if (groupName) {
const group = command.subcommands[groupName]

if (!group || !('subcommands' in group)) return null
const sub = group.subcommands[subName]

if (!sub) return null

const options = parseSlashOptions(interaction, sub.options ?? {})

return { sub, options }
}

const sub = command.subcommands[subName]

if (!sub || 'subcommands' in sub) return null
const options = parseSlashOptions(interaction, sub.options ?? {})

return { sub, options }
}

const optionResolvers: Record<
string,
(i: ChatInputCommandInteraction, name: string) => unknown
Expand Down
2 changes: 2 additions & 0 deletions packages/harmonix/src/runtime/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Commands
export {
defineSlashCommand,
defineSlashSubcommand,
defineSlashSubcommandGroup,
defineUserContextMenuCommand,
defineMessageContextMenuCommand
} from './internal/command'
Expand Down
69 changes: 60 additions & 9 deletions packages/harmonix/src/runtime/internal/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,87 @@ import { CommandType, ModuleType } from '../../types/module'

import type {
HarmonixMessageContextMenuCommand,
HarmonixSlashCommand,
HarmonixSlashCommandWithOptions,
HarmonixSlashCommandWithSubs,
HarmonixSlashSubcommand,
HarmonixSlashSubcommandGroup,
HarmonixUserContextMenuCommand
} from '../../types/module'
import type {
HarmonixContextMenuConfig,
HarmonixMessageContextMenuCommandHandler,
HarmonixSlashCommandConfig,
HarmonixSlashCommandConfigBase,
HarmonixSlashCommandConfigWithOptions,
HarmonixSlashCommandConfigWithSubs,
HarmonixSlashCommandHandler,
HarmonixSlashSubcommandConfig,
HarmonixSlashSubcommandGroupConfig,
HarmonixUserContextMenuCommandHandler
} from '../../types/runtime/command'
import type { SlashOptionMap } from '../../types/runtime/options'

export const defineSlashCommand = <Options extends SlashOptionMap>(
config: HarmonixSlashCommandConfig<Options>,
export function defineSlashCommand<Options extends SlashOptionMap>(
config: HarmonixSlashCommandConfigWithOptions<Options>,
handler: HarmonixSlashCommandHandler<Options>
) => {
): HarmonixSlashCommandWithOptions<Options>

export function defineSlashCommand<
Subs extends Record<
string,
HarmonixSlashSubcommand | HarmonixSlashSubcommandGroup<any>
>
>(
config: HarmonixSlashCommandConfigWithSubs<Subs>
): HarmonixSlashCommandWithSubs<Subs>

export function defineSlashCommand(
config: HarmonixSlashCommandConfigBase,
handler: HarmonixSlashCommandHandler<any>
): HarmonixSlashCommandWithOptions<any>

export function defineSlashCommand(
config: any,
handler?: (...args: any[]) => any
): any {
return {
type: ModuleType.Command,
commandType: CommandType.Slash,
name: config.name,
category: config.category,
description: config.description,
contexts: config.contexts,
memberPermissions: config.memberPermissions,
nsfw: config.nsfw,
options: config.options ?? {},
autocomplete: config.autocomplete,
middleware: config.middleware ?? [],
commandType: CommandType.Slash,
...(config.subcommands
? { subcommands: config.subcommands }
: { options: config.options, autocomplete: config.autocomplete, handler })
}
}

export const defineSlashSubcommand = <Options extends SlashOptionMap>(
config: HarmonixSlashSubcommandConfig<Options>,
handler: HarmonixSlashCommandHandler<Options>
): HarmonixSlashSubcommand<Options> => {
return {
name: config.name,
description: config.description,
options: config.options,
autocomplete: config.autocomplete,
handler
} as HarmonixSlashCommand
}
}

export function defineSlashSubcommandGroup<
Subs extends Record<string, HarmonixSlashSubcommand>
>(
config: HarmonixSlashSubcommandGroupConfig<Subs>
): HarmonixSlashSubcommandGroup<Subs> {
return {
name: config.name,
description: config.description,
subcommands: config.subcommands
}
}

export function defineUserContextMenuCommand(
Expand Down
1 change: 0 additions & 1 deletion packages/harmonix/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type { HarmonixPlugin } from './runtime/plugin'
export type {
ModuleType,
HarmonixCommand,
HarmonixSlashCommand,
HarmonixUserContextMenuCommand,
HarmonixMessageContextMenuCommand,
HarmonixEvent,
Expand Down
Loading