Home > database >  Error when trying to run discord.py with asynchronous functions on cog_loader
Error when trying to run discord.py with asynchronous functions on cog_loader

Time:09-07

I am trying to build my first discord.py bot on a docker container. But when running it the traceback gave me the following error on the cogs_loader: enter image description here

I used the following articles to try to fix the error, but I failed when trying refactor my code with their solution:

Bot base.load_extentstion was never awaited error message discord.py

"RuntimeWarning: coroutine 'BotBase.load_extension' was never awaited" after updating discord.py

This is my "client" file:

intents = discord.Intents.default()
bot = Bot(owner_id=env.owner_id, command_prefix='.', intents=intents)

async def load_extensions():
    await bot.load_extension('bot.cogs.commands')
    await bot.load_extension('bot.cogs.owner')


async def main():
    await load_extensions()
    await bot.start(env.TOKEN)

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

This is my __init__.py file for the cogs (setup):

intents = discord.Intents.default()
bot = Bot(owner_id=env.owner_id, command_prefix='.', intents=intents)


async def setup(bot):
    """
    Init bot's cogs
    """
    await bot.add_cog(Commands(bot))
    await bot.add_cog(Owner(bot))

This is the traceback when running __main__.py:

Traceback (most recent call last):
  File "D:/GitHub/Mamey_bot/bot/__main__.py", line 83, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "D:/GitHub/Mamey_bot/bot/__main__.py", line 62, in main
    await load_extensions()
  File "D:/GitHub/Mamey_bot/bot/__main__.py", line 57, in load_extensions
    await bot.load_extension('bot.cogs.commands')
TypeError: object NoneType can't be used in 'await' expression

If I rewrite main() in __main__.py like in the first article:

async def main():
    async with bot:
        await load_extensions()
        await bot.start(env.TOKEN)

I would get a different traceback:

Traceback (most recent call last):
  File "D:/GitHub/Mamey_bot/bot/__main__.py", line 84, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
    return future.result()
  File "D:/GitHub/Mamey_bot/bot/__main__.py", line 62, in main
    async with bot:
AttributeError: __aexit__

How can I fix this?

If I don't use asynchronous functions when running the bot in the container, the bot will get online, but the commands wont work.

CodePudding user response:

The issue was that my Dockerfile was installing the wrong discord.py version.

Inside the container

In the docker container RUN python3 -m pip install -U discord.py was installing Discord.py 2.0.1, and that's why the traceback was giving me "load_extension was never awaited" since I was using a deprecated syntax of 1.7.3.

After using the proper command in the Dockerfile: RUN python3 -m pip install discord.py==1.7.3 the issue disappears and the bot and its commands works perfectly.

On my real PC and the refactored code

My real PC/OS (outside the docker container) had the inverse issue: It had installed discord.py 1.7.3 and the refactored code was using discord.py 2.0.1 syntax. After running in the CMD py -3 -m pip install -U py-cord the refactored code that I shared in the original question works perfectly.

tl;dr: Need to be more careful on what versions I'm importing.

  • Related