【发布时间】:2021-04-04 20:56:42
【问题描述】:
我希望机器人在循环中每隔一段时间向特定频道发送一条消息。我的代码设置与我在on_ready 函数中所做的完全一样,但出现错误。此外,我的 on_ready 函数位于 main.py 中,而我的 interval_message 位于外部 cog 中,为 loops.py
on_ready 函数(main.py):
@client.event
async def on_ready():
channel = client.get_channel(787469158539198484)
await channel.send(f"{client.user.mention} online")
print("Bot is online")
interval_message 函数(loops.py)
@tasks.loop(seconds=60.0)
async def interval_message(self):
channel = self.client.get_channel(787469158539198484)
await channel.send("Random message")
问题是on_ready 可以正常发送消息,而interval_message 返回错误:
AttributeError: "NoneType" object has no attribute "send"
那么如何让机器人循环发送消息呢?
【问题讨论】:
标签: python discord.py