is it possible to have different commands for different prefixes in discord.py?

Viewed 143

Is it possible to have different commands for each prefix? For example, one command for ! and another command for ? For example, someone does !foo the bot responds with "foo", but another person does ?foo the bot responds with "bar"?

1 Answers

You can make a bot with two prefixes and create a foo command. Then check what prefix was used to invoke a command using ctx.prefix.

from discord.ext import commands

bot = commands.Bot(command_prefix=("!", "?"))


@bot.command()
async def foo(ctx):
    await ctx.send("foo" if ctx.prefix == "!" else "bar")

bot.run("TOKEN")

Or you can do it manually using on_message event:

@bot.event
async def on_message(message):
    if message.content.lower() == "!foo":
        await message.channel.send("foo")
    elif message.content.lower() == "?foo":
        await message.channel.send("bar")
    await bot.process_commands(message)
Related