SlashCommandsProvider
This file defines the SlashCommandsProvider
class, which manages the registration and unregistration of Discord slash commands and context menu commands for the @sodacore/core
Discord integration. It interacts with the Discord REST API to deploy commands globally or to specific guilds, and can also remove them as needed.
Features
- Global Command Registration: Registers all slash and context menu commands globally for the Discord application.
- Guild Command Registration: Registers all slash and context menu commands for a specific guild.
- Global Command Unregistration: Removes all global slash and context menu commands.
- Guild Command Unregistration: Removes all slash and context menu commands from a specific guild.
- Automatic Command Discovery: Discovers all Discord controllers and their commands using metadata and the registry.
- Dependency Injection: Uses
@Provide
and@Inject
decorators for integration with the Sodacore DI system.
Usage
typescript
import SlashCommandsProvider from './provider/slash-commands';
const slashCommands = new SlashCommandsProvider();
// Register commands globally
await slashCommands.registerGlobal();
// Register commands for a specific guild
await slashCommands.registerGuild('guild-id');
// Unregister global commands
await slashCommands.unregisterGlobal();
// Unregister commands from a specific guild
await slashCommands.unregisterGuild('guild-id');
API
Methods
async registerGlobal(): Promise<number>
Registers all discovered slash and context menu commands globally for the application.
- Returns: The number of commands registered.
async registerGuild(guildId?: string): Promise<number>
Registers all discovered slash and context menu commands for a specific guild.
- guildId: The guild ID to register commands for (uses config value if not provided).
- Returns: The number of commands registered.
async unregisterGlobal(): Promise<boolean>
Removes all global slash and context menu commands from the application.
- Returns:
true
if successful,false
otherwise.
async unregisterGuild(guildId?: string): Promise<boolean>
Removes all slash and context menu commands from a specific guild.
- guildId: The guild ID to unregister commands from (uses config value if not provided).
- Returns:
true
if successful,false
otherwise.
Private Methods
private getCommands(): any[]
Discovers all Discord controllers and collects their slash and context menu commands as JSON objects for registration.
How It Works
- Discovery: Finds all modules registered as Discord controllers using metadata and the registry.
- Command Extraction: Extracts slash commands and context menu commands from controllers using their builders and method metadata.
- REST API Interaction: Uses the Discord REST API to register or unregister commands globally or for specific guilds.
- Error Handling: Throws errors for missing configuration and logs failures during command removal.
Notes
- Requires a valid Discord bot token and client ID in the configuration.
- Uses
discord.js
v14+ REST API for command management. - Designed to be used as a provider within the Sodacore dependency injection system.
- Handles both slash commands and context menu commands (message/user).