ClawX69
ClawX69

Reputation: 71

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

PROBLEM

I dont know where I am wrong but the below code seems to throw me NoneType error even though I have used this thing in my other cogs too.

CODE

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

ERROR

'NoneType' object has no attribute 'send'

I am sure there is no mistake in channel id and also I am new to tasks so if this problem is arising due to it how do I solve it?

Upvotes: 1

Views: 5598

Answers (1)

Razzer
Razzer

Reputation: 884

NoneType means in that case that either the channel id is invalid or the channel is not in the bot's cache because he's starting currently or whatever.

But in most cases, is the channel just not in the bot's cache.

You should add await self.client.wait_until_ready() at the start of the task, to wait until the bot's cache is ready.

Like this:

@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")

Sources

Upvotes: 2

Related Questions