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:
pa!
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.
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)
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!"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With