Skip to content

Instantly share code, notes, and snippets.

@favna
Last active April 10, 2022 20:45
Show Gist options
  • Save favna/5b614d9e78d483cfcb48f19cc2cab887 to your computer and use it in GitHub Desktop.
Save favna/5b614d9e78d483cfcb48f19cc2cab887 to your computer and use it in GitHub Desktop.
[REVISION 2] Sapphire Subcommands v3 for framework v3
import type { Args, ChatInputCommand, MessageCommand } from '@sapphire/framework';
import {
ChatInputSubcommandGroupMappings,
ChatInputSubcommandMappings,
MessageSubcommandGroupMappings,
MessageSubcommandMappings,
SubCommand,
SubcommandMappingsArray
} from '@sapphire/plugin-subcommands';
import type { CommandInteraction, Message } from 'discord.js';
// Note: SubcommandMappingsArray is equal to:
/*
type SubcommandMappingsArray = (
| ChatInputSubcommandGroupMappings
| ChatInputSubcommandMappings
| MessageSubcommandGroupMappings
| MessageSubcommandMappings
)[];
*/
export class CommandWithSubcommands extends SubCommand {
/**
* Alternatively to passing the mapping in the options you can also create
* a class field named exactly "subcommandMappings"
*/
public readonly subcommandMappings: SubcommandMappingsArray = [
// Usages is /config mod-roles add @role
// command group subcommand option
new ChatInputSubcommandGroupMappings('mod-roles', [
/**
* Adds a new mod role
*
* Usage is /config mod-roles add @role
* command group subcommand option
*/
{ name: 'add', to: 'chatInputAddModRoles' },
/**
* Removes a mod role
*
* Usage is /config mod-roles remove @role
* command group subcommand option
*/
{ name: 'remove', to: this.chatInputRemoveModRoles.bind(this) }, // Alternative way of binding methods
/**
* Shows all mod roles
* Note: No default needed here because Chat Input
*
* Usage is /config mod-roles show
* command group subcommand
*/
{ name: 'show', to: 'chatInputShowModRoles' }
]),
// no hyphen because message run
new MessageSubcommandGroupMappings('adminroles', [
/**
* Adds a new admin role
*
* Usage is !config adminroles add @role
* command group subcommand arg
*/
{ name: 'add', to: this.messageAddAdminRoles.bind(this) }, // Alternative way of binding methods
/**
* Removes an admin role
*
* Usage is !config adminroles remove @role
* command group subcommand arg
*/
{ name: 'remove', to: 'messageRemoveAdminRoles' },
/**
* Shows all admin roles
* Default so if the command string is `!config adminroles` then this will trigger
*
* Usage is !config adminroles show
* command group subcommand
*/
{ name: 'show', to: 'messageShowAdminRoles', default: true }
]),
new MessageSubcommandMappings([
/**
* Sets the language
*
* Usage is !config language en-US
* command subcommand arg
*/
{ name: 'language', to: 'messageSetLanguage' },
{ name: 'lang', to: 'messageSetLanguage' },
/**
* Shows all config
*
* Usage is !config show
* command subcommand
*/
{ name: 'show', to: 'messageShowConfig', default: true }
]),
new ChatInputSubcommandMappings([
/**
* Sets the language of the bot
*
* Usage is /config language en-US
* command subcommand option
*/
{ name: 'language', to: 'chatInputSetLanguage' },
/**
* Show all config
*
* Usage is /config show
* command subcommand
*/
{ name: 'show', to: 'chatInputShowConfig' }
])
];
public constructor(context: SubCommand.Context, options: SubCommand.Options) {
super(context, {
...options,
name: 'config',
aliases: ['conf'],
description: 'Configures various aspects of the bot'
});
}
public chatInputAddModRoles(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public chatInputRemoveModRoles(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public chatInputShowModRoles(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public chatInputSetLanguage(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public chatInputShowConfig(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public messageAddAdminRoles(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
public messageRemoveAdminRoles(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
public messageShowAdminRoles(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
public messageSetLanguage(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
public messageShowConfig(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
}
import type { Args, ChatInputCommand, MessageCommand } from '@sapphire/framework';
import {
ChatInputSubcommandGroupMappings,
ChatInputSubcommandMappings,
MessageSubcommandGroupMappings,
MessageSubcommandMappings,
SubCommand
} from '@sapphire/plugin-subcommands';
import type { CommandInteraction, Message } from 'discord.js';
export class CommandWithSubcommands extends SubCommand {
public constructor(context: SubCommand.Context, options: SubCommand.Options) {
super(context, {
...options,
name: 'config',
aliases: ['conf'],
description: 'Configures various aspects of the bot',
subCommands: [
// Usages is /config mod-roles add @role
// command group subcommand option
new ChatInputSubcommandGroupMappings('mod-roles', [
/**
* Adds a new mod role
*
* Usage is /config mod-roles add @role
* command group subcommand option
*/
{ name: 'add', to: 'chatInputAddModRoles' },
/**
* Removes a mod role
*
* Usage is /config mod-roles remove @role
* command group subcommand option
*/
{ name: 'remove', to: 'chatInputRemoveModRoles' },
/**
* Shows all mod roles
* Note: No default needed here because Chat Input
*
* Usage is /config mod-roles show
* command group subcommand
*/
{ name: 'show', to: 'chatInputShowModRoles' }
]),
// no hyphen because message run
new MessageSubcommandGroupMappings('adminroles', [
/**
* Adds a new admin role
*
* Usage is !config adminroles add @role
* command group subcommand arg
*/
{ name: 'add', to: 'messageAddAdminRoles' },
/**
* Removes an admin role
*
* Usage is !config adminroles remove @role
* command group subcommand arg
*/
{ name: 'remove', to: 'messageRemoveAdminRoles' },
/**
* Shows all admin roles
* Default so if the command string is `!config adminroles` then this will trigger
*
* Usage is !config adminroles show
* command group subcommand
*/
{ name: 'show', to: 'messageShowAdminRoles', default: true }
]),
new MessageSubcommandMappings([
/**
* Sets the language
*
* Usage is !config language en-US
* command subcommand arg
*/
{ name: 'language', to: 'messageSetLanguage' },
{ name: 'lang', to: 'messageSetLanguage' },
/**
* Shows all config
*
* Usage is !config show
* command subcommand
*/
{ name: 'show', to: 'messageShowConfig', default: true }
]),
new ChatInputSubcommandMappings([
/**
* Sets the language of the bot
*
* Usage is /config language en-US
* command subcommand option
*/
{ name: 'language', to: 'chatInputSetLanguage' },
/**
* Show all config
*
* Usage is /config show
* command subcommand
*/
{ name: 'show', to: 'chatInputShowConfig' }
])
]
});
}
public chatInputAddModRoles(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public chatInputRemoveModRoles(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public chatInputShowModRoles(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public chatInputSetLanguage(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public chatInputShowConfig(interaction: CommandInteraction, _context: ChatInputCommand.RunContext) {
return interaction.reply('Hello World!');
}
public messageAddAdminRoles(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
public messageRemoveAdminRoles(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
public messageShowAdminRoles(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
public messageSetLanguage(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
public messageShowConfig(message: Message, _args: Args, _context: MessageCommand.RunContext) {
return message.reply('Hello World!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment