[commands] Change prefix callback signature and add when_mentioned.
The utility allows for easy addition of "when the bot is mentioned" as the prefix. The change of signature was to facilitate this.
This commit is contained in:
parent
af94179be5
commit
52eb0e3adb
@ -10,7 +10,7 @@ An extension module to facilitate creation of bot commands.
|
|||||||
:license: MIT, see LICENSE for more details.
|
:license: MIT, see LICENSE for more details.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .bot import Bot
|
from .bot import Bot, when_mentioned
|
||||||
from .context import Context
|
from .context import Context
|
||||||
from .core import *
|
from .core import *
|
||||||
from .errors import *
|
from .errors import *
|
||||||
|
@ -33,6 +33,11 @@ from .view import StringView
|
|||||||
from .context import Context
|
from .context import Context
|
||||||
from .errors import CommandNotFound
|
from .errors import CommandNotFound
|
||||||
|
|
||||||
|
def when_mentioned(bot, msg):
|
||||||
|
"""A callable that implements a command prefix equivalent
|
||||||
|
to being mentioned, e.g. ``@bot ``."""
|
||||||
|
return '{0.user.mention} '.format(bot)
|
||||||
|
|
||||||
class Bot(GroupMixin, discord.Client):
|
class Bot(GroupMixin, discord.Client):
|
||||||
"""Represents a discord bot.
|
"""Represents a discord bot.
|
||||||
|
|
||||||
@ -48,9 +53,10 @@ class Bot(GroupMixin, discord.Client):
|
|||||||
command_prefix
|
command_prefix
|
||||||
The command prefix is what the message content must contain initially
|
The command prefix is what the message content must contain initially
|
||||||
to have a command invoked. This prefix could either be a string to
|
to have a command invoked. This prefix could either be a string to
|
||||||
indicate what the prefix should be, or a callable that takes in a
|
indicate what the prefix should be, or a callable that takes in the bot
|
||||||
:class:`discord.Message` as its first parameter and returns the prefix.
|
as its first parameter and :class:`discord.Message` as its second
|
||||||
This is to facilitate "dynamic" command prefixes.
|
parameter and returns the prefix. This is to facilitate "dynamic"
|
||||||
|
command prefixes.
|
||||||
|
|
||||||
The command prefix could also be a list or a tuple indicating that
|
The command prefix could also be a list or a tuple indicating that
|
||||||
multiple checks for the prefix should be used and the first one to
|
multiple checks for the prefix should be used and the first one to
|
||||||
@ -71,7 +77,7 @@ class Bot(GroupMixin, discord.Client):
|
|||||||
def _get_prefix(self, message):
|
def _get_prefix(self, message):
|
||||||
prefix = self.command_prefix
|
prefix = self.command_prefix
|
||||||
if callable(prefix):
|
if callable(prefix):
|
||||||
return prefix(message)
|
return prefix(self, message)
|
||||||
else:
|
else:
|
||||||
return prefix
|
return prefix
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user