[V3 Core] Add optional async setup function (#1314)

This commit is contained in:
Will 2018-02-18 21:12:58 -05:00 committed by GitHub
parent 86b5932c8f
commit b871241eac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 5 deletions

View File

@ -172,7 +172,7 @@ class RedBase(BotBase, RpcMethodMixin):
while pkg_name in curr_pkgs:
curr_pkgs.remove(pkg_name)
def load_extension(self, spec: ModuleSpec):
async def load_extension(self, spec: ModuleSpec):
name = spec.name.split('.')[-1]
if name in self.extensions:
return
@ -182,7 +182,11 @@ class RedBase(BotBase, RpcMethodMixin):
del lib
raise discord.ClientException('extension does not have a setup function')
lib.setup(self)
if asyncio.iscoroutinefunction(lib.setup):
await lib.setup(self)
else:
lib.setup(self)
self.extensions[name] = lib
def unload_extension(self, name):

View File

@ -226,7 +226,7 @@ class Core:
return
try:
ctx.bot.load_extension(spec)
await ctx.bot.load_extension(spec)
except Exception as e:
log.exception("Package loading failed", exc_info=e)
await ctx.send(_("Failed to load package. Check your console or "
@ -261,7 +261,7 @@ class Core:
try:
self.cleanup_and_refresh_modules(spec.name)
ctx.bot.load_extension(spec)
await ctx.bot.load_extension(spec)
except Exception as e:
log.exception("Package reloading failed", exc_info=e)
await ctx.send(_("Failed to reload package. Check your console or "

View File

@ -52,7 +52,7 @@ def init_events(bot, cli_flags):
for package in packages:
try:
spec = await bot.cog_mgr.find_cog(package)
bot.load_extension(spec)
await bot.load_extension(spec)
except Exception as e:
log.exception("Failed to load package {}".format(package),
exc_info=e)