Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(discord): support subcommands #271

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Changes from 4 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
27 changes: 21 additions & 6 deletions adapters/discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,19 +345,34 @@
options: encodeCommandOptions(cmd),
})

const decodeArgv = (data: Discord.InteractionData.ApplicationCommand, command: Universal.Command) => {
const result = { name: data.name, arguments: [], options: {} } as Universal.Argv
const decodeArgv = (
data: Discord.InteractionData.ApplicationCommand | Discord.InteractionData.ApplicationCommand.Option,
command: Universal.Command,
) => {
const result = { name: command.name, arguments: [], options: {} } as Universal.Argv
const options = data.options
if (!options) return result
const dataChild = options[0]
if (dataChild && (
dataChild.type === Discord.ApplicationCommand.OptionType.SUB_COMMAND ||
dataChild.type === Discord.ApplicationCommand.OptionType.SUB_COMMAND_GROUP
)) {
shigma marked this conversation as resolved.
Show resolved Hide resolved
const commandChild = command.children.find(cmd => cmd.name.endsWith('.' + dataChild.name))
return commandChild ? decodeArgv(dataChild, commandChild) : result
}
for (const argument of command.arguments) {
const value = data.options?.find(opt => opt.name === argument.name)?.value
const name = argument.name.toLowerCase()
const value = options.find(opt => opt.name === name)?.value
if (value !== undefined) result.arguments.push(value)
}
for (const option of command.options) {
const value = data.options?.find(opt => opt.name === option.name)?.value
const name = option.name.toLowerCase()
const value = options.find(opt => opt.name === name)?.value
if (value !== undefined) result.options[option.name] = value
}
return result
}

Check warning on line 375 in adapters/discord/src/utils.ts

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 4 spaces but found 6

Check failure on line 375 in adapters/discord/src/utils.ts

View workflow job for this annotation

GitHub Actions / lint

'||' should be placed at the beginning of the line
export function encodeCommandOptions(cmd: Universal.Command): Discord.ApplicationCommand.Option[] {
const result: Discord.ApplicationCommand.Option[] = []
if (cmd.children.length) {
Expand All @@ -367,8 +382,8 @@
? Discord.ApplicationCommand.OptionType.SUB_COMMAND_GROUP
: Discord.ApplicationCommand.OptionType.SUB_COMMAND,
options: encodeCommandOptions(child),
description: cmd.description[''] || child.name,
description_localizations: pick(cmd.description, Discord.Locale),
description: child.description[''] || child.name,
description_localizations: pick(child.description, Discord.Locale),
})))
} else {
for (const arg of cmd.arguments) {
Expand Down
Loading