如何让我的机器人在 discord.py 的特定频道中发送消息

How to make my bot send message in a specific channel in discord.py

问题

我不知道我哪里错了,但下面的代码似乎抛出了 NoneType 错误,即使我也在我的其他齿轮中使用了这个东西。

代码

@tasks.loop(seconds=10.0)
async def remind(self):
    remind_channel = self.bot.get_channel(#id of channel)    
    await remind_channel.send("Passed")

错误

'NoneType' object has no attribute 'send'

我确定频道 ID 没有错误,而且我是新手,所以如果出现此问题,我该如何解决?

NoneType 表示在这种情况下,频道 ID 无效或频道不在机器人的缓存中,因为他当前正在启动或其他原因。

但在大多数情况下,频道只是不在机器人的缓存中。

您应该在任务开始时添加 await self.client.wait_until_ready(),以等待机器人的缓存准备就绪。

像这样:

@tasks.loop(seconds=10.0)
async def remind(self):
    await self.client.wait_until_ready()
    remind_channel = self.bot.get_channel(#id of channel)    
    await remind_channel.send("Passed")

来源