0

I am making a discord bot using discord.py and I am using an event loop for my borrow command, but when I run it it gives me a bunch of error codes. The error code said that the loop is already running, but I just bot the code.

Note: this is something wrong with discord.py package, not python itself.

code:

@tasks.loop(hours=24.0)
async def debt_increase():
    for user in coins.keys():
        setdebt(user, getdebt(user)+(0.06*getdebt(user)))

async def run_bot():
    await client.run(Token)

debt_increase.start()
asyncio.get_event_loop().run_until_complete(run_bot())

Error code:

  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 631, in run
    loop.run_forever()
  File "/usr/lib/python3.8/asyncio/base_events.py", line 560, in run_forever
    self._check_running()
  File "/usr/lib/python3.8/asyncio/base_events.py", line 552, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 92, in _cleanup_loop
    _cancel_tasks(loop)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 77, in _cancel_tasks
    loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
  File "/usr/lib/python3.8/asyncio/base_events.py", line 592, in run_until_complete
    self._check_running()
  File "/usr/lib/python3.8/asyncio/base_events.py", line 552, in _check_running
    raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 801, in <module>
    asyncio.get_event_loop().run_until_complete(run_bot())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "main.py", line 798, in run_bot
    await client.run(Token)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 637, in run
    _cleanup_loop(loop)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 97, in _cleanup_loop
    loop.close()
  File "/usr/lib/python3.8/asyncio/unix_events.py", line 58, in close
    super().close()
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 89, in close
    raise RuntimeError("Cannot close a running event loop")
RuntimeError: Cannot close a running event loop
Geeson
  • 17
  • 9

1 Answers1

2

client.run is not a coroutine, it's a function. The machinery for using the event loop is already contained inside it. You should just call client.run(Token) when you want to run your bot.

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96