我为我的机器人做了一个循环,但它不会运行

2024-04-25 07:02:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用discord.py创建一个discord bot,我正在为我的借用命令使用一个事件循环,但是当我运行它时,它会给我一堆错误代码。错误代码表示循环已在运行,但我只是修改了代码

注意:这是discord.py包的问题,而不是python本身。

代码:

@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())

错误代码:

  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

Tags: runinpyclientloopeventasynciolib