send the previous message to a discord channel

Im not sure if u meant to find the content of the message itself or the previous message before it but you can read more on txt_channel.history here:

What history does is enabling you to see the message history of a text channel.
So if you want to find the contents of a specific message using it’s id you can do: (of course you need to fetch the text channel, you can look at here)

async for msg in txt_ch.history(limit=[how many messages back you want the loop to go]):
    if msg.id == ID_OF_MSG: # you found the message
       print("the message with the id has been found")

now this code ^^ is pointing you to the message you gave with the current id but if you want to get the message previous to it you just add this:

async for msg in txt_ch.history(limit=[how many messages back you want the loop to go]):
    found_message = False
    if msg.id == ID_OF_MSG: # you found the message
       found_message =True
    elif found_message:
       found_message = False
       print("previous message")

Of course you can change the last line on the print to something like:

print(msg.content)

to see it’s content, or you can send it back to the channel, it is up to you.

side not: I tried to test it and it doesn’t seem that the ids of the messages are one after another so you can’t do id-1 to get the previous message id.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top