简体   繁体   中英

Send a message to a channel in a loop discord.py

I would like the bot to send a message to a specific channel every so often in a loop. I have the code set up exactly as I did in my on_ready function but I get an error. Also my on_ready function is in main.py and my interval_message is in an external cog as 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")

interval_message function (loops.py)

@tasks.loop(seconds=60.0)
async def interval_message(self):
channel = self.client.get_channel(787469158539198484)
await channel.send("Random message")

The problem is that on_ready is fine and sends the message while interval_message returns the error:

AttributeError: "NoneType" object has no attribute "send"

So how do I make the bot send a message in a loop?

self.client.get_channel(787469158539198484) is returning None (which is of type NoneType ). Figure out why that is.

It could be that self 's client is not initialized properly with that particular channel (whatever those are... it would help to have more context about the libraries you are using and what the variables represent).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM