From fce2ef553474007dc413fd599454274fce7f6416 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 13 Aug 2017 20:52:12 -0400 Subject: [PATCH] [commands] Raise when an invalid prefix is given. Fixes #712 --- discord/ext/commands/bot.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index 55c973015..5b872f88c 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -785,20 +785,33 @@ class BotBase(GroupMixin): message: :class:`discord.Message` The message context to get the prefix of. + Raises + -------- + :exc:`.ClientException` + The prefix was invalid. This could be if the prefix + function returned None, the prefix list returned no + elements that aren't None, or the prefix string is + empty. + Returns -------- Union[List[str], str] A list of prefixes or a single prefix that the bot is listening for. """ - prefix = self.command_prefix + prefix = ret = self.command_prefix if callable(prefix): ret = prefix(self, message) if asyncio.iscoroutine(ret): ret = yield from ret - return ret - else: - return prefix + + if isinstance(ret, (list, tuple)): + ret = [p for p in ret if p] + + if not ret: + raise ClientException('invalid prefix (could be an empty string, empty list, or None)') + + return ret @asyncio.coroutine def get_context(self, message, *, cls=Context): @@ -840,7 +853,7 @@ class BotBase(GroupMixin): prefix = yield from self.get_prefix(message) invoked_prefix = prefix - if not isinstance(prefix, (tuple, list)): + if isinstance(prefix, str): if not view.skip_string(prefix): return ctx else: