App Commands (Slash Commands)
Relevant source files
The following files were used as context for generating this wiki page:
App Commands represent the integration of Discord's native application command system, including Slash Commands, User Context Menus, and Message Context Menus, into the Red Discord Bot framework. Red implements a specialized command tree logic to support its modular nature, allowing commands to be enabled or disabled dynamically without immediate global synchronization.
Sources: redbot/core/tree.py:38-44, redbot/core/core_commands.py:649-651
Architecture and Command Tree
Red uses the RedTree class, which inherits from discord.app_commands.CommandTree, to manage application commands. Unlike standard implementations, RedTree does not immediately add commands to the active Discord tree. It maintains internal storage for disabled commands to support Red's modularity and per-guild configurations.
Command Storage Components
The RedTree manages four primary internal dictionaries to track command states:
| Dictionary | Description |
|---|---|
_global_commands | Active global slash commands (inherited). |
_context_menus | Active context menu commands (inherited). |
_disabled_global_commands | Commands registered but currently marked as disabled. |
_disabled_context_menus | Context menus registered but currently marked as disabled. |
Sources: redbot/core/tree.py:46-51
Registration Logic
When a cog or module adds a command via add_command, the tree checks if the command should be force-enabled or if it belongs to a specific guild. If it is a standard global command, it is placed in a "disabled" state by default until an administrator enables it.
Sources: redbot/core/tree.py:53-99
Command Management
Bot owners manage application commands through the [p]slash command group. This system allows for granular control over which commands are exposed to Discord's interface.
Management Commands
| Command | Action |
|---|---|
[p]slash enable | Marks a specific command as enabled in the internal tree. |
[p]slash disable | Marks a specific command as disabled in the internal tree. |
[p]slash enablecog | Enables all application commands provided by a specific cog. |
[p]slash disablecog | Disables all application commands provided by a specific cog. |
[p]slash list | Displays the current state (Enabled/Disabled/Forced) of all registered commands. |
[p]slash sync | Synchronizes the internal enabled state with Discord's API. |
Sources: redbot/core/core_commands.py:653-888
Force-Enabled Commands
Some commands are marked by cog authors as required for functionality using extras={"red_force_enable": True}. These commands cannot be disabled via management commands and are always added to the active tree during the synchronization process.
Sources: redbot/core/core_commands.py:726-733, redbot/core/tree.py:168-175
Execution and Error Handling
Application command execution follows a specialized flow that integrates with Red's internal permission and logging systems.
Global Interaction Checks
The tree performs global checks before executing any interaction:
- Bot Check: Prevents other bots from triggering interactions.
- Ignored Locations: Checks if the guild or channel is on the bot's ignore list.
- Allowlist/Blocklist: Validates that the user is permitted to use the bot.
Sources: redbot/core/tree.py:270-291
Error Handling Flow
The on_error handler in RedTree translates interaction errors into user-friendly messages or log entries.
Sources: redbot/core/tree.py:207-268
Hybrid Commands
Red supports HybridCommand and HybridGroup, which allow a single function to act as both a traditional prefix command and a slash command.
Configuration Features
- Auto-Sync: Hybrid commands are automatically prepared for the app command tree if
with_app_commandis True. - Group Behavior: In Red,
HybridGroupdefaultsinvoke_without_commandtoFalseunless a fallback is provided, ensuring consistent behavior between prefix and slash interactions.
Sources: redbot/core/commands/commands.py:1124-1188
Custom Commands Integration
The CustomCommands cog allows users to create text-based responses. While primarily prefix-based, Red's event system handles messages that do not match standard commands to check for custom command triggers.
Cooldown Logic
Custom commands support three levels of cooldowns that apply to their invocation:
- Guild: Rate limit applied to the entire server.
- Channel: Rate limit applied to specific text channels.
- Member: Rate limit applied to individual users.
Sources: redbot/cogs/customcom/customcom.py:381-404, redbot/cogs/customcom/customcom.py:539-558
Summary
App Commands in Red provide a modular bridge to Discord's modern UI features. By intercepting the standard command registration process, Red ensures that bot owners retain control over their command surface area while supporting complex features like hybrid execution and automated permission checks. Developers must use [p]slash sync to commit changes to Discord after modifying the enabled status of commands.
Sources: redbot/core/core_commands.py:880-908, redbot/core/tree.py:127-142