0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.