Skip to main content

Bridge

Concept

Let's say that you want to make a command that is both a Slash Command and a Prefixed Command. Now, you could just copy and paste the code from the first command callback to the other and make some adjustments, but that's not very efficient.

This is where the ext.bridge module comes in. It allows you to use one callback to make both a Slash Command and a Prefixed Command.

Example Usage

import discord
from discord.ext import bridge

intents = discord.Intents()
intents.message_content = True

bot = bridge.Bot(command_prefix="!", intents=intents)

@bot.bridge_command()
async def hello(ctx):
await ctx.respond("Hello!")

bot.run("TOKEN")
BobDotComused /hello
RobocordBot06/18/2024
Hello!
BobDotCom06/18/2024
!hello
@BobDotCom!hello
RobocordBot06/18/2024
Hello!

Syntax

First, instead of using discord.Bot or commands.Bot, we use bridge.Bot. bridge.Bot does inherit from commands.Bot, so you can do anything with bridge.Bot that commands.Bot can do. Then, we define a command with @bot.bridge_command(). This makes a Bridge Command, which has both a Prefixed Command counterpart and a Slash Command counterpart. Next, the callback has access to a ctx object, which is the context of the command. This context is either of BridgeApplicationContext type or BridgeExtContext. Because of that, it makes detecting how the function was called easier.

Using Bridge Commands in a Cog

Like Slash Commands and Prefixed Commands, you can use Bridge Commands in a Cog. You can do this by using the bridge_command decorator. Here's an example:

import discord
from discord.ext import bridge, commands

class Greetings(commands.Cog):
def __init__(self, bot):
self.bot = bot

@bridge.bridge_command()
async def hello(self, ctx):
await ctx.respond("Hello!")

@bridge.bridge_command()
async def bye(self, ctx):
await ctx.respond("Bye!")

def setup(bot):
bot.add_cog(Greetings(bot))

The cog will automatically split the Bridge Command into their Slash Command and Prefixed Command counterparts.

BobDotComused /hello
RobocordBot06/18/2024
Hello!
BobDotCom06/18/2024
!hello
@BobDotCom!hello
RobocordBot06/18/2024
Hello!
BobDotComused /bye
RobocordBot06/18/2024
Bye!
BobDotCom06/18/2024
!bye
@BobDotCom!bye
RobocordBot06/18/2024
Bye!

Deferring

You can defer if you want to communicate to the user that your bot is busy processing the command. This is done by using ctx.defer(). For the Slash Command implementation, ctx.defer() calls the function that gives out a "Bot is thinking" message. For the Prefixed Command implementation, ctx.defer() enables the typing indicator.

Options

Options are pretty straightforward. You just specify them like you do with prefixed commands, and you're all set!

import discord
from discord.ext import bridge

intents = discord.Intents()
intents.message_content = True

bot = bridge.Bot(command_prefix="!", intents=intents)

@bot.bridge_command()
async def sum(ctx, a: int, b: int):
await ctx.respond(a + b)

bot.run("TOKEN")
BobDotComused /sum
RobocordBot06/18/2024
4
BobDotCom06/18/2024
!sum 2 2
@BobDotCom!sum 2 2
RobocordBot06/18/2024
4