简体   繁体   中英

discord.py tasks.loop() sending runtime error : no running event loop

I am trying to make my bot send a message in a particular channel hourly using the discord.ext's tasks. Here's the buggy piece of code

@tasks.loop(hours = 1)
async def remindstudy():
  await bot.wait_until_ready()
  channel = bot.get_channel(1038719402658517054)
  await channel.send("check")
remindstudy.start()

This is supposed to send the message "check" every hour but instead throws this error:

Traceback (most recent call last):
  File "main.py", line 137, in <module>
    remindstudy.start()
 File "/home/runner/main-bore-ho-raha-hoon/venv/lib/python3.8/site-packages/discord/ext/tasks/__init__.py", line 398, in start
    self._task = asyncio.create_task(self._loop(*args, **kwargs))
  File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/asyncio/tasks.py", line 381, in create_task
    loop = events.get_running_loop()
RuntimeError: no running event loop

sys:1: RuntimeWarning: coroutine 'Loop._loop' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

What is going wrong? I copied this code snippet from another old bot of mine, it is still running perfectly fine, but this bot won't run

The task.start method is expected to be called in an async function (coroutine), like in setup_hook , after the 2.0 update.

async def setup_hook():
    remindstudy.start()
bot.setup_hook = setup_hook

@tasks.loop(hours = 1)
async def remindstudy():
  await bot.wait_until_ready()
  channel = bot.get_channel(1038719402658517054)
  await channel.send("check")

See this example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM