Search code examples
pythondiscorddiscord.pybots

How do you get a message from a channel?


channel = bot.get_channel(id_of_the_channel)
message = await channel.fetch_message(id_of_the_message)

I tried to use this code so that a bot could edit its own message, but there's an error on the line with "fetch_message".

File "main.py", line 280, in on_message message = await channel.fetch_message(messageid) AttributeError: 'NoneType' object has no attribute 'fetch_message'


Solution

  • So get_channel only returns the channel object if it's in the cache - if it's returning None then it's not. It is returning None as per the error message. To fix:

    channel = await bot.fetch_channel(id_of_the_channel)
    message = await channel.fetch_message(id_of_the_message)
    

    This should resolve that issue.