discord.py: sys:1: RuntimeWarning: coroutine ‘Loop._loop’ was never awaited

huangapple go评论63阅读模式
英文:

discord.py: sys:1: RuntimeWarning: coroutine 'Loop._loop' was never awaited

问题

Here's the translated traceback and code portion:

追溯最新调用最后):
  文件 "bot.py"55<module>
    send_cal.start()
  文件 "python3.8/site-packages/discord/ext/tasks/__init__.py"398在 start
    self._task = asyncio.create_task(self._loop(*args, **kwargs))
  文件 "/usr/local/Cellar/python@3.8/3.8.16/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py"381在 create_task
    loop = events.get_running_loop()
RuntimeError: 没有正在运行的事件循环
sys:1: RuntimeWarning: 协程 'Loop._loop' 从未被等待

我使用以下代码为我的机器人获得了上述回溯

@tasks.loop(seconds=20.0)
async def send_cal():
    message_channel = bot.get_channel(target_channel_id)
    print(f"Got channel {message_channel}")
    df = await someApi()

    df = df.drop(['stuff'], axis=1)
    csv = df.to_csv(header=False, index=False)
    res = csv.replace(',', ' ----> ')
    if (len(res)) >= 2000:
        await message.channel.send('result over 2000 chars')
    await message.channel.send(res)

@send_cal.before_loop
async def before():
    await client.wait_until_ready()
    print("Finished waiting")

send_cal.start()

client.run('mytoken')

Please note that code translations may not include comments or inline documentation.

英文:
Traceback (most recent call last):
  File &quot;bot.py&quot;, line 55, in &lt;module&gt;
    send_cal.start()
  File &quot;python3.8/site-packages/discord/ext/tasks/__init__.py&quot;, line 398, in start
    self._task = asyncio.create_task(self._loop(*args, **kwargs))
  File &quot;/usr/local/Cellar/python@3.8/3.8.16/Frameworks/Python.framework/Versions/3.8/lib/python3.8/asyncio/tasks.py&quot;, line 381, in create_task
    loop = events.get_running_loop()
RuntimeError: no running event loop
sys:1: RuntimeWarning: coroutine &#39;Loop._loop&#39; was never awaited

I get the above traceback with this code for my bot

@tasks.loop(seconds=20.0)
async def send_cal():
    message_channel = bot.get_channel(target_channel_id)
    print(f&quot;Got channel {message_channel}&quot;)
    df = await someApi()

    df = df.drop([&#39;stuff&#39;], axis=1)
    csv = df.to_csv(header=False, index=False)
    res = csv.replace(&#39;,&#39;, &#39; ----&gt; &#39;)
    if (len(res)) &gt;= 2000:
        await message.channel.send(&#39;result over 2000 chars&#39;)
    await message.channel.send(res)

@send_cal.before_loop
async def before():
    await client.wait_until_ready()
    print(&quot;Finished waiting&quot;)

send_cal.start()

client.run(&#39;mytoken&#39;)

I can't figure out why send_cal would never be awaited. I see in examples the same setup, where .start() kicks off the job and i assume asyncio is awaiting it under the hood, however i haven't been able to figure out why this is happening otherwise

edit: I am really just copying the starter guide here https://discordpy.readthedocs.io/en/latest/ext/tasks/index.html

答案1

得分: 1

搞清楚了——需要在“on_ready”例程中添加send_cal.start()

英文:

figured it out -- needed to add send_cal.start() to the on_ready routine

huangapple
  • 本文由 发表于 2023年5月22日 09:56:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定