From cf77eb2e47263db8b9b9331ea1ef7914388a3dfa Mon Sep 17 00:00:00 2001 From: Will Date: Sat, 9 Sep 2017 15:27:05 -0400 Subject: [PATCH] [Core] Fix loading of cogs on initial startup (#971) --- redbot/core/core_commands.py | 33 ++++++++++++++++++++++----------- redbot/core/events.py | 3 ++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/redbot/core/core_commands.py b/redbot/core/core_commands.py index e21013e1d..32c4ea989 100644 --- a/redbot/core/core_commands.py +++ b/redbot/core/core_commands.py @@ -16,6 +16,8 @@ from redbot.core import i18n import redbot.cogs # Don't remove this line or core cogs won't load +__all__ = ["find_spec", "Core"] + log = logging.getLogger("red") OWNER_DISCLAIMER = ("⚠ **Only** the person who is hosting Red should be " @@ -23,6 +25,21 @@ OWNER_DISCLAIMER = ("⚠ **Only** the person who is hosting Red should be " "owner can access any data that is present on the host " "system.** ⚠") + +async def find_spec(bot, cog_name: str): + try: + spec = await bot.cog_mgr.find_cog(cog_name) + except RuntimeError: + real_name = ".{}".format(cog_name) + try: + mod = importlib.import_module(real_name, package='redbot.cogs') + except ImportError: + spec = None + else: + spec = mod.__spec__ + return spec + + _ = i18n.CogI18n("Core", __file__) @@ -33,17 +50,11 @@ class Core: @checks.is_owner() async def load(self, ctx, *, cog_name: str): """Loads a package""" - try: - spec = await ctx.bot.cog_mgr.find_cog(cog_name) - except RuntimeError: - real_name = ".{}".format(cog_name) - try: - mod = importlib.import_module(real_name, package='redbot.cogs') - except ImportError as e: - await ctx.send(_("No module by that name was found in any" - " cog path.")) - return - spec = mod.__spec__ + spec = await find_spec(ctx.bot, cog_name) + if spec is None: + await ctx.send(_("No module by that name was found in any" + " cog path.")) + return try: ctx.bot.load_extension(spec) diff --git a/redbot/core/events.py b/redbot/core/events.py index 1128f12b5..5ff09bdf2 100644 --- a/redbot/core/events.py +++ b/redbot/core/events.py @@ -7,6 +7,7 @@ from .sentry_setup import should_log from discord.ext import commands from .utils.chat_formatting import inline +from .core_commands import find_spec log = logging.getLogger("red") sentry_log = logging.getLogger("red.sentry") @@ -40,7 +41,7 @@ def init_events(bot, cli_flags): for package in packages: try: - spec = await bot.cog_mgr.find_cog(package) + spec = await find_spec(bot, package) bot.load_extension(spec) except Exception as e: log.exception("Failed to load package {}".format(package),