簡體   English   中英

循環發送消息到通道 discord.py

[英]Send a message to a channel in a loop discord.py

我希望機器人在循環中每隔一段時間向特定頻道發送一條消息。 我的代碼設置與我在on_ready function 中所做的完全一樣,但出現錯誤。 另外,我的on_ready function 在main.py中,而我的interval_message在外部 cog 作為loops.py

on_ready function (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")

間隔消息 function (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"

那么如何讓機器人循環發送消息呢?

self.client.get_channel(787469158539198484)返回None (類型為NoneType )。 弄清楚為什么會這樣。

可能是selfclient未使用該特定通道正確初始化(無論是什么......這將有助於獲得有關您正在使用的庫以及變量代表的內容的更多上下文)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM