Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different prefix for each Cog?

Is it possible to set a prefix for each cog?

For example the Cog with admin commands has the prefix pa! and the cog for some fun commands pf!.

Without using on_message.

Edit: I guess I have to go more in detail:

  • My cog for serverstats has a command called 'stats', this command should get triggered when I'm using pa!
  • My cog for fun has a 'stats' command too. This should be triggered when I'm using pf!

My only thought up to now was to set the default prefix to p! and then call the command astats. But I want to have a better way.

like image 358
maaanuuuuu Avatar asked Sep 16 '25 03:09

maaanuuuuu


2 Answers

To expand on Maxsc's answer, you can define a cog_check method that will be called before every command from the cog:

class FunCommands(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.prefix = "pf!"
    async def cog_check(self, ctx):
        return ctx.prefix == self.prefix

In your main code, you can use a callable that consumes the prefixes from the cogs to determine which prefixes to pay attention to. This would get passed as your command_prefix

def get_prefixes(bot, message):
    cog_prefixes = (cog.prefix for cog in bot.cogs.values() if hasattr(cog, 'prefix'))
    default_prefixes = ("!")
    return (*cog_prefixes, *default_prefixes)
like image 154
Patrick Haugh Avatar answered Sep 18 '25 18:09

Patrick Haugh


You can check the prefix used in a command using ctx.prefix. So you could run a check like this:

@commands.command()
async def sample_command(self, ctx):
    if ctx.prefix != "pa!":
        return

And previously enabling those prefixes bot = commands.Bot(command_prefix=("pa!", "pf!"))

like image 35
Maxsc Avatar answered Sep 18 '25 17:09

Maxsc