Last active
January 24, 2022 19:49
-
-
Save favna/518404664bd8f668b79ac3a1a3163223 to your computer and use it in GitHub Desktop.
Subcommands for Sapphire Slash Command Proof of Concept code examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ApplyOptions } from '@sapphire/decorators'; | |
import { ApplicationCommandRegistry, ChatInputCommand } from '@sapphire/framework'; | |
import { SubCommandGroupHandler } from '@sapphire/plugin-subcommands'; | |
import type { CommandInteraction } from 'discord.js'; | |
@ApplyOptions<SubCommandPluginCommand.Options>({ | |
description: | |
"A Chat Input Command with SubCommand group and nested Subcommands using a required public property that's an instance of SubCommandGroupHandler" | |
}) | |
export class SlashCommand extends SubCommandPluginCommand { | |
public roles = new SubCommandGroupHandler([ | |
{ name: 'list', to: this.listRolesSubcommand.bind(this) }, | |
{ name: 'add', to: this.addRoleSubcommand.bind(this) }, | |
['remove', (...args: Parameters<ChatInputCommand['chatInputRun']>) => this.removeRoleSubcommand(...args)] // Alternative way of accessing the method | |
]); | |
public listRolesSubcommand(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) { | |
return interaction.reply({ content: 'Hello World!' }); | |
} | |
public addRoleSubcommand(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) { | |
return interaction.reply({ content: 'Hello World!' }); | |
} | |
public removeRoleSubcommand(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) { | |
return interaction.reply({ content: 'Hello World!' }); | |
} | |
public override registerApplicationCommands(registry: ApplicationCommandRegistry) { | |
registry.registerChatInputCommand( | |
(builder) => | |
builder // | |
.setName(this.name) | |
.setDescription(this.description) | |
.addSubcommandGroup((input) => | |
input // | |
.setName('roles') | |
.setDescription('Roles group') | |
.addSubcommand((input) => | |
input // | |
.setName('list') | |
.setDescription('List roles of a user') | |
.addUserOption((input) => | |
input // | |
.setName('user') | |
.setDescription('The user whose roles you want to view.') | |
.setRequired(true) | |
) | |
) | |
.addSubcommand((input) => | |
input // | |
.setName('add') | |
.setDescription('Add a role to a user') | |
.addUserOption((input) => | |
input // | |
.setName('user') | |
.setDescription('The user to whom you want to add a role.') | |
.setRequired(true) | |
) | |
.addRoleOption((option) => | |
option // | |
.setName('role') | |
.setDescription('The role to add') | |
.setRequired(true) | |
) | |
) | |
.addSubcommand((input) => | |
input // | |
.setName('remove') | |
.setDescription('Remove a role from a user') | |
.addUserOption((input) => | |
input // | |
.setName('user') | |
.setDescription('The user from whom you want to remove a role.') | |
.setRequired(true) | |
) | |
.addRoleOption((option) => | |
option // | |
.setName('role') | |
.setDescription('The role to remove') | |
.setRequired(true) | |
) | |
) | |
), | |
{ guildIds: [], idHints: [] } | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment