How do you get a message from a channel?

Question:

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’

Asked By: Yozy Opto

||

Answers:

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.

Answered By: ESloman
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.