MoBadraan
MoBadraan

Reputation: 137

AttributeError: 'NoneType' object has no attribute 'send' in on_message event

I want that when I write something in the channel 927235996158922802 the bot will repeat it in (channelO),
but I got the error: 'NoneType' object has no attribute 'send'.

@bot.event
async def on_message(message):
    channelO = bot.get_channel(926945495413301290)
    if message.channel.id == 927235996158922802:
        await bot.get_channel(channelO).send(message)

Upvotes: 0

Views: 1649

Answers (1)

RiveN
RiveN

Reputation: 2653

Nonetype error occurs when your bot doesn't have the channel in its internal cache. That's why you should use await bot.fetch_channel instead which is an API call. message stores other data (e.g. server, author, channel, etc.) you have to get message.content.

@bot.event
async def on_message(message):
    if message.author == bot.user: # To make sure your bot will ignore messages sent by itself
        return

    if message.channel.id == 927235996158922802:
        channelO = await bot.fetch_channel(926945495413301290)
        await channelO.send(message.content)

You have to enable intents.messages!

Upvotes: 1

Related Questions