Skip to content

Instantly share code, notes, and snippets.

@favna
Last active January 24, 2022 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save favna/518404664bd8f668b79ac3a1a3163223 to your computer and use it in GitHub Desktop.
Save favna/518404664bd8f668b79ac3a1a3163223 to your computer and use it in GitHub Desktop.
Subcommands for Sapphire Slash Command Proof of Concept code examples
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