dolfies / discord.py-self

A fork of the popular discord.py for user accounts.
https://discordpy-self.rtfd.io/en/latest/
MIT License
688 stars 160 forks source link

discord.ext.commands.errors.ExtensionFailed: Extension 'errorhandler' raised an error: RuntimeError: asyncio.run() cannot be called from a running event loop #574

Closed kqee closed 1 year ago

kqee commented 1 year ago

Summary

a runtime error whenever I try to load an extension(cogs)

Reproduction Steps

  1. create a new file where a cog will reside.
  2. create a class, inheriting commands.Cog, write constructor(__init__)
  3. in the main file, load the cog with Bot().load_extension(...) method.

Code

# in the main file
from discord.ext import commands

from discord.ext.commands import Context

from dotenv import load_dotenv
from os import getenv

load_dotenv()
PREFIX="->"
TOKEN=getenv("TOKEN")

bot = commands.Bot("->", self_bot=True)

@bot.event
async def on_ready():
    print(f"logged in as {bot.user}")
    await bot.load_extension("errorhandler") # the file where a cog resides

# ...other methods

bot.run(TOKEN)
# in "errorhandler.py", where the cog resides

class ErrorHandler(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
    # ...methods...

async def setup(bot):
    await bot.add_cog(ErrorHandler(bot))

Expected Results

Bot().load_extension(...) should load the specified file and do what it is supposed to do. Which is to handle errors in my case.

Actual Results

I was greeted by the error below:

Traceback (most recent call last):
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\ext\commands\bot.py", line 777, in _load_from_module_spec
    spec.loader.exec_module(lib)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\codin\code\selfbot\errorhandler.py", line 4, in <module>
    from main import bot_reply
  File "C:\Users\codin\code\selfbot\main.py", line 55, in <module>
    bot.run(TOKEN)
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\client.py", line 938, in run
    asyncio.run(runner())
  File "C:\Users\codin\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 186, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\client.py", line 507, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\codin\code\selfbot\main.py", line 20, in on_ready
    await bot.load_extension("errorhandler")
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\ext\commands\bot.py", line 855, in load_extension
    await self._load_from_module_spec(spec, name)
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\ext\commands\bot.py", line 780, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'errorhandler' raised an error: RuntimeError: asyncio.run() cannot be called from a running event loop
2023-09-10 01:17:31 ERROR    discord.client Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\ext\commands\bot.py", line 777, in _load_from_module_spec
    spec.loader.exec_module(lib)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\codin\code\selfbot\errorhandler.py", line 4, in <module>
    from main import bot_reply
  File "C:\Users\codin\code\selfbot\main.py", line 55, in <module>
    bot.run(TOKEN)
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\client.py", line 938, in run
    asyncio.run(runner())
  File "C:\Users\codin\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 186, in run
    raise RuntimeError(
RuntimeError: asyncio.run() cannot be called from a running event loop

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\client.py", line 507, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\codin\code\selfbot\main.py", line 20, in on_ready
    await bot.load_extension("errorhandler")
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\ext\commands\bot.py", line 855, in load_extension
    await self._load_from_module_spec(spec, name)
  File "C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\ext\commands\bot.py", line 780, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'errorhandler' raised an error: RuntimeError: asyncio.run() cannot be called from a running event loop
C:\Users\codin\code\selfbot\.venv\Lib\site-packages\discord\client.py:512: RuntimeWarning: coroutine 'Client.run.<locals>.runner' was never awaited
  await self.on_error(event_name, *args, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

System Information

Checklist

Additional Information

No response

kqee commented 1 year ago

I have found a workaround::

  1. install nest_asyncio(with pip)
  2. call nest_asyncio.apply() in the main file
# ...other imports
import nest_asyncio

nest_asyncio.apply()
# ...symbols ans whatnot

bot = commands.Bot(...)

@bot.event
async def on_ready():
     await bot.load_extension("cog")

load_extension() no longer raises an error.

kqee commented 1 year ago

apparently there was some errors in my code involving imports...i have fixed it now.