[Core] Fix loading of cogs on initial startup (#971)

This commit is contained in:
Will 2017-09-09 15:27:05 -04:00 committed by GitHub
parent 5ae6fe2dda
commit cf77eb2e47
2 changed files with 24 additions and 12 deletions

View File

@ -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)

View File

@ -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),