【发布时间】:2021-11-30 15:08:06
【问题描述】:
显然缺少一些代码,但这里是所有需要帮助的代码。
import os
import discord
import requests
import json
import asyncio
channel_id = 791884298810163200
def get_quote():
response = requests.get("https://zenquotes.io/api/random")
json_data = json.loads(response.text)
quote = json_data[0]["q"] + " -" + json_data[0]["a"]
return quote
async def sendQuote():
channel = client.get_channel(channel_id)
await channel.send(get_quote())
async def background_task():
#if not 8am, wait until 8am (ignoring the seconds place, doesn't have to be exactly 8am)
await sendQuote() #fix this
await asyncio.sleep(5) #change this to the number of seconds in a twenty four hour period when done testing
await background_task()
if __name__ == "__main__":
client.loop.create_task(background_task())
keep_alive()
client.run(my_secret)
我还没有添加等到早上 8 点部分,因为它在测试中不需要。如果我移动channel = client.get_channel(channel_id)
await channel.send(get_quote()) 进入 on_ready() 它会打印报价,所以我真的不确定 sendQuote() 出了什么问题
我的错误是:
Task exception was never retrieved future: <Task finished name='Task-1' coro=<background_task() done, defined at main.py:31> exception=AttributeError("'NoneType' object has no attribute 'send'")> Traceback (most recent call last): File "main.py", line 33, in background_task await sendQuote()
#fix this File "main.py", line 28, in sendQuote await channel.send(get_quote()) AttributeError: 'NoneType' object has no attribute 'send
【问题讨论】:
-
嗨@joseph-spielman,请edit您的问题并将上面的评论移到问题中。
-
你不应该在你的'sendQuote()' 中有这个等待调用'await bot.wait_until_ready()'。所以它可以等到机器人准备好,即执行'on_ready()'函数。
-
不是 async/await 的大用户,但我有一种强烈的感觉,该代码有问题......首先,你为什么在同一个 background_task() 方法中调用 background_task() ?你已经有效地使它递归了,为了什么?!其次,如果您需要每天运行一次代码 - 上帝保佑,不要在您的程序中设置计时器,只需使用系统任务调度程序,它在 Windows 和 Linux 上都可用,它将使您的代码简单明了.最后,如果您仍然认为您需要这种设计,我建议您在第 28 行之前从 pprint(channel) 开始进行一些调试打印
-
如果您希望每隔一段时间发送一次内容,只需在
async函数内运行while True循环,在循环内放入await asyncio.sleep(x),然后执行asyncio.get_event_loop().run_until_complete(asyncio.gather(client.start(my_secret), daily_loop(), ...))。asyncio.gather基本上需要一堆异步函数调用(notawait它们)然后使它们并发,因此您可以使用loop.run_until_complete一起运行它们。
标签: python discord python-asyncio