From 6c01250c39c0d58e5e4b41fe1edd7179fd8a6275 Mon Sep 17 00:00:00 2001 From: khazhyk Date: Mon, 3 Jul 2017 20:38:21 -0700 Subject: [PATCH] [commands] fix unloading incorrect cogs unload_extension would incorrectly unload cogs/listeners of other extensions if the name of one was a prefix of another. --- discord/ext/commands/bot.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/discord/ext/commands/bot.py b/discord/ext/commands/bot.py index 3a488a41b..85b8d1341 100644 --- a/discord/ext/commands/bot.py +++ b/discord/ext/commands/bot.py @@ -88,6 +88,9 @@ _mentions_transforms = { _mention_pattern = re.compile('|'.join(_mentions_transforms.keys())) +def _is_submodule(parent, child): + return parent == child or child.startswith(parent + ".") + @asyncio.coroutine def _default_help_command(ctx, *commands : str): """Shows this message.""" @@ -731,12 +734,12 @@ class BotBase(GroupMixin): # remove the cogs registered from the module for cogname, cog in self.cogs.copy().items(): - if cog.__module__.startswith(lib_name): + if _is_submodule(lib_name, cog.__module__): self.remove_cog(cogname) # first remove all the commands from the module for cmd in self.all_commands.copy().values(): - if cmd.module.startswith(lib_name): + if _is_submodule(lib_name, cmd.module): if isinstance(cmd, GroupMixin): cmd.recursively_remove_all_commands() self.remove_command(cmd.name) @@ -745,7 +748,7 @@ class BotBase(GroupMixin): for event_list in self.extra_events.copy().values(): remove = [] for index, event in enumerate(event_list): - if event.__module__.startswith(lib_name): + if _is_submodule(lib_name, event.__module__): remove.append(index) for index in reversed(remove):