discord.ext.bridge – A module that bridges slash commands to prefixed commands¶
New in version 2.0.
This module allows using one command callback in order to make both a prefix command and a slash command. This page includes the API reference/documentation for the module, but only contains a short example. For a more detailed guide on how to use this, see our discord.ext.bridge guide.
Note
ext.bridge requires the message content intent to be enabled, as it uses the ext.commands extension.
Example usage:
import discord
from discord.ext import bridge
intents = discord.Intents.default()
intents.message_content = True
bot = bridge.Bot(command_prefix="!", intents=intents)
@bot.bridge_command()
async def hello(ctx):
await ctx.respond("Hello!")
@bot.bridge_command()
async def bye(ctx):
await ctx.respond("Bye!")
@bot.bridge_command()
async def sum(ctx, first: int, second: int):
s = first + second
await ctx.respond(f"{s}")
bot.run("TOKEN")
API Reference¶
Bots¶
- class discord.ext.bridge.Bot(command_prefix=<function when_mentioned>, help_command=<default-help-command>, **options)¶
Represents a discord bot, with support for cross-compatibility between command types.
This class is a subclass of
ext.commands.Botand as a result anything that you can do with aext.commands.Botyou can do with this bot.New in version 2.0.
- add_bridge_command()¶
Takes a
BridgeCommandand adds both a slash and traditional (prefix-based) version of the command to the bot.
- @bridge_command¶
A shortcut decorator that invokes
bridge_command()and adds it to the internal command list viaadd_bridge_command().- Returns:
A decorator that converts the provided method into an
BridgeCommand, adds both a slash and traditional (prefix-based) version of the command to the bot, and returns theBridgeCommand.- Return type:
Callable[…,
BridgeCommand]
- class discord.ext.bridge.AutoShardedBot(command_prefix=<function when_mentioned>, help_command=<default-help-command>, **options)¶
This is similar to
Botexcept that it is inherited fromext.commands.AutoShardedBotinstead.New in version 2.0.
- Bot.add_bridge_command()¶
Takes a
BridgeCommandand adds both a slash and traditional (prefix-based) version of the command to the bot.
- @Bot.bridge_command¶
A shortcut decorator that invokes
bridge_command()and adds it to the internal command list viaadd_bridge_command().- Returns:
A decorator that converts the provided method into an
BridgeCommand, adds both a slash and traditional (prefix-based) version of the command to the bot, and returns theBridgeCommand.- Return type:
Callable[…,
BridgeCommand]
Commands¶
- defadd_to
- defget_application_command
- defget_ext_command
- class discord.ext.bridge.BridgeCommand(callback, **kwargs)¶
- get_ext_command()¶
A method to get the ext.commands version of this command.
- Returns:
The respective traditional (prefix-based) version of the command.
- Return type:
- get_application_command()¶
A method to get the discord.commands version of this command.
- Returns:
The respective slash command version of the command.
- Return type:
- add_to(bot)¶
Adds the command to a bot.
- Parameters:
bot (Union[
ExtBot,ExtAutoShardedBot]) – The bot to add the command to.
- @bridge.bridge_command¶
A decorator that is used to wrap a function as a command.
- Parameters:
kwargs (Optional[Dict[str, Any]]) – Keyword arguments that are directly passed to the respective command constructors.
- class discord.ext.bridge.BridgeExtCommand(*args, **kwargs)¶
A subclass of
ext.commands.Commandthat is used to implement bridge commands.
- class discord.ext.bridge.BridgeSlashCommand(*args, **kwargs)¶
A subclass of
SlashCommandthat is used to implement bridge commands.
Context¶
- class discord.ext.bridge.BridgeContext¶
The base context class for compatibility commands. This class is an
ABC(abstract base class), which is subclassed byBridgeExtContextandBridgeApplicationContext. The methods in this class are meant to give parity between the two contexts, while still allowing for all of their functionality.When this is passed to a command, it will either be passed as
BridgeExtContext, orBridgeApplicationContext. Since they are two separate classes, it is quite simple to useisinstance()to make different functionality for each context. For example, if you want to respond to a command with the command type that it was invoked with, you can do the following:@bot.bridge_command() async def example(ctx: BridgeContext): if isinstance(ctx, BridgeExtContext): command_type = "Traditional (prefix-based) command" elif isinstance(ctx, BridgeApplicationContext): command_type = "Application command" await ctx.send(f"This command was invoked with a(n) {command_type}.")
New in version 2.0.
- await respond(*args, **kwargs)¶
This function is a coroutine.
Responds to the command with the respective response type to the current context. In
BridgeExtContext, this will bereply()while inBridgeApplicationContext, this will berespond().
- await defer(*args, **kwargs)¶
This function is a coroutine.
Defers the command with the respective approach to the current context. In
BridgeExtContext, this will betrigger_typing()while inBridgeApplicationContext, this will bedefer().Note
There is no
trigger_typingalias for this method.trigger_typingwill always provide the same functionality across contexts.
- await edit(*args, **kwargs)¶
This function is a coroutine.
Edits the original response message with the respective approach to the current context. In
BridgeExtContext, this will have a custom approach whererespond()caches the message to be edited here. InBridgeApplicationContext, this will beedit().
- class discord.ext.bridge.BridgeApplicationContext(bot, interaction)¶
The application context class for compatibility commands. This class is a subclass of
BridgeContextandApplicationContext. This class is meant to be used withBridgeCommand.New in version 2.0.
- asyncdelete
- class discord.ext.bridge.BridgeExtContext(*args, **kwargs)¶
The ext.commands context class for compatibility commands. This class is a subclass of
BridgeContextandContext. This class is meant to be used withBridgeCommand.New in version 2.0.