Ask coding questions

← Back to all posts
Discord delete last message in channel
PikachuB2005 (10)

I want to delete all of the messages in the channel before it says something differant. Or it could just edit the last message and replace it.

Answered by 19wintersp (1142) [earned 5 cycles]
View Answer
19wintersp (1142)

You can use TextChannel.purge with an always-positive check to delete every message in a channel:

await channel.purge(limit=10000, check=lambda m: True)

assuming channel is a TextChannel, and there are less than 10000 messages to remove.

Alternatively, you can use TextChannel.last_message and Message.delete to delete the last message:

await channel.last_message.delete()

or Message.edit to edit it:

await channel.last_message.edit(content="<message>")

assuming channel is a TextChannel. Note that that message might not be yours.

PikachuB2005 (10)

@19wintersp Um do you think people will use my token 0_0

19wintersp (1142)

@BrandenBentley What do you mean? If it's in your ".env" file, you should be fine.

PikachuB2005 (10)

@19wintersp Yes, but anyone can see it. Then they can use it themselves and screw up my discord server.

PikachuB2005 (10)

@19wintersp it's literally in the code above the comments 😕

PikachuB2005 (10)

@19wintersp on my computer the repl is part of my post.

19wintersp (1142)

@BrandenBentley Only you can see the ".env" file. It's hidden to the rest of us.