Learn to Code via Tutorials on Repl.it!

← Back to all posts
Edit a message - Discord.js Bot Development
almostStatic

Editing Messages | Discord.js

If you do not have a solid understanding of JavaScript or how it works and wish to learn how to do this; I suggest that you read some guides! There are some I have linked below.

Editing a sent message

There are loads of ways we can go around doing this, but let's start with one of the simplest (using a .then() block) :

The code above will send a message saying Beep and then quickly edit it to Boop!.

Remember, you can do anything with the sentMessage variable.

Using async / await

Anywhere inside an async function, you can use the operator await.

Quick Definition: await is used to wait for the promise to either be accepted or rejected. Basically, await is used to make sure something happens after the thing(s) before it have been executed.

Implementation of async / await

Alright, let's get down to some code, eh?

Here, we do const msg = await message.channel.send("Beep"). What this does is it sends a message saying "Beep" (making sure that everything else before it has been completed) and storing it as a variable msg.

Refer to the Official Discord.js Guide website for further resources

Voters
EHSANURRAHMANRA
CentralCafe
strongavenger71
Mortis7286
DiamondDev01
SkidTrack
BenClarke7
SalahObeidat
AdCharity
PixelNinja
Comments
hotnewtop
plitchorinkos

is it possible to make it so the bot sends the message after a command, like you showed, but instead of it instantley editing it, it does so after another command?
for example i say a, the bot responds b, then i say c, and he edits the "b" to be a "d". Kinda confusing, but i hope you understand.

chankelvin

@plitchorinkos Yes it is possible using Message Collectors / Reaction collectors https://discordjs.guide/popular-topics/collectors.html#await-messages

AdCharity

Seems like normal docs to me

almostStatic

@AdCharity it's mainly for beginners.

AdCharity

@static2020 the docs are pretty lit but I think the editing feature is cool. Do you know how to implement chat log reading?

almostStatic

@AdCharity Elaborate, by this do you mean a deleted messages logger like a snipe command? If so you could use the messageDelete event.

AdCharity

@static2020 Well I mean like able to read and retrieve messages from a channel. I have no real use for it; I just noticed that there is a read feature for bots on the discord developer page.

almostStatic

@AdCharity You could use <channel>.fetchMessage({ around: "MESSAGE_ID", limit: 1 }) and use .then() Careful; it returns a collection so to get the first message you'd do <collection>.first() <collection> is a placeholder for the parameter you passed into the .then() block.

Do you have Discord? Add me static#6419

AdCharity

@static2020 I'll send a friend request (I'm at school so I can't right now but will do later). As for discord bot dev I'm pretty much dead right now, but I'll be sure to look into it.

almostStatic
Reynhart

@almostStatic just for completion, on v12 it became .fetch({ options })

almostStatic

@Reynhart This guide assumes the message is already cached and ready to use.

RhyleyPlant

do you know if there is a way to edit a message after a user has sent a message?

almostStatic

@RhyleyPlant Bots can only edit messages they have sent. You would need to get the bot to send a message, assign that to a variable and edit that via the message event. (client.on('message'...)..)