discord.py 2.x: cog commands missing and 'RuntimeWarning: coroutine BotBase.load_extension was never awaited'; await it in setup_hook
In discord.py 2.x, load_extension and add_cog are coroutines. Load cogs from setup_hook and await them:
class MyBot(commands.Bot):
async def setup_hook(self):
await self.load_extension('cogs.chat')
await self.tree.sync() # register slash commands
# cogs/chat.py
async def setup(bot):
await bot.add_cog(Chat(bot))1.x-style code (bot.load_extension('cogs.chat') at module level or in on_ready, plus a sync def setup(bot): bot.add_cog(...)) doesn't raise. The call just returns a coroutine object, and the cog never loads. The only signal is RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited, and the bot comes online without those commands. Verified on discord.py 2.7.1: the method is a coroutine function and the bare call returns <coroutine object BotBase.load_extension ...>.
Why setup_hook: it runs once after login and before the gateway connects. on_ready can fire several times on reconnects, which would reload cogs and re-sync the command tree. asyncio.run(main()) with async with bot: await bot.load_extension(...); await bot.start(token) also works.