How can i edit the embed of a message?

Im trying to make a /ratrace command which returns a embed with the racers,and i want to actually edit them so that they move and stuff,i have tried searching ways to editing an embed but i havent found any. (also startrace() function is gonna be for editing the message.)
No description
No description
76 Replies
d.js toolkit
d.js toolkit6mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Crλb
CrλbOP6mo ago
No description
No description
003002001
0030020016mo ago
First you have to get message object after sending message so use msg.send(...).then((SentMessage) => ...) After that you have to receive your embed to edit it, <Message>.embeds returns Array of embeds in message. In this example SentMessage.embeds[0] is what should you use. After receiving embed you can edit Embed's description property directly Embed.description = "Edited Description" At the end you must use <Message>.edit function with embeds option to edit your message. Try it out yourself and feel free to ask if you get any errors, also sorry if my english is bad.
Crλb
CrλbOP6mo ago
ok yeah thanks also your english is fine :D also not really related to the question, but, what is the message object called on the discord.js docs?
d.js docs
d.js docs6mo ago
:class: Message @14.15.3 Represents a message on Discord.
Crλb
CrλbOP6mo ago
oh ok thanks sorry for asking a bunch of very complicated and annoying questions but ... * do i have to replace the msg.reply() with msg.send({embeds:[...]}).then(...) or do i have to add it after msg.reply ? * what do i put inside of the three dots in msg.send and .then(SentMessage) => ) ? * where do i put <Message>.embeds and <Message>.edit ?
003002001
0030020016mo ago
Answer for first question is: it doesn't matter, if you want to make your bot mention user then use reply.
Crλb
CrλbOP6mo ago
ok yeah i will use reply since i want a parameter for the message to be ephemeral or not (which i know how to do )
003002001
0030020016mo ago
For other questions, let me show you how to place codes correctly.
/*send embed*/.then((SentMessage) => {
// get embed

// edit embed

// and edit your message
});
/*send embed*/.then((SentMessage) => {
// get embed

// edit embed

// and edit your message
});
That is how its gonna look like Try again and let me know!
Crλb
CrλbOP6mo ago
ok do i replace <Message> with msg ?
003002001
0030020016mo ago
Yeah replace it with your message variable, in example that i sent you message variable is SentMessage
Crλb
CrλbOP6mo ago
ok
Crλb
CrλbOP6mo ago
what do i put inside of SentMessage.edit() ? this is my current code btw
No description
d.js docs
d.js docs6mo ago
:guide: Popular Topics: Embeds - Resending and editing read more
003002001
0030020016mo ago
Nice, but you have 2 missing parts. 1. Don't forget msg.reply() is a function, its not a property 2. You called <Message>.edit function but you have to put your edited embed into embeds option: <Message>.edit({embeds: <MessageEmbed>}) Actually edit and reply functions are nearly same, think it like sending a message
Crλb
CrλbOP6mo ago
ok assuming MessageEmbed is the embed i made with the EmbedBuilder class right?
TÆMBØ
TÆMBØ6mo ago
MessageEmbed is the v13 name for EmbedBuilder, but yes, correct. Note that the embeds option takes an array of embeds and not a single one, irrespective of how many embeds you intend to send
Crλb
CrλbOP6mo ago
i know
Crλb
CrλbOP6mo ago
i think i fixed it
No description
TÆMBØ
TÆMBØ6mo ago
You're still missing the payload you want to reply with
d.js docs
d.js docs6mo ago
:method: Message#reply() @14.15.3 Send an inline reply to this message.
// Reply to a message
message.reply('This is a reply!')
.then(() => console.log(`Replied to message "${message.content}"`))
.catch(console.error);
// Reply to a message
message.reply('This is a reply!')
.then(() => console.log(`Replied to message "${message.content}"`))
.catch(console.error);
Crλb
CrλbOP6mo ago
i need to use the method .catch() ?
TÆMBØ
TÆMBØ6mo ago
No, I'm talking about the lack of any parameters you're passing to reply()
Crλb
CrλbOP6mo ago
ooohh so what parameters does .reply use?
TÆMBØ
TÆMBØ6mo ago
:IRT_This:
Crλb
CrλbOP6mo ago
ok yeah lemme do that the parameter inside of .reply will be sent as a normal message not as a part of the embed then?
TÆMBØ
TÆMBØ6mo ago
If you simply pass a string to it, yes If you pass an embeds option with embeds to it, then it replies with those embeds
003002001
0030020016mo ago
@TÆMBØ Docs says embed parts are immutable but as far as i can remember we can just define embed into a variable and edit its own property then resend it. Am i wrong?
TÆMBØ
TÆMBØ6mo ago
Well, Embed#description is a getter, not a property, which you can't overwrite
d.js docs
d.js docs6mo ago
Structures from the API cannot be edited directly. To do so, you can create a new structure (a builder) using the .from() method
const newEmbed = EmbedBuilder.from(embed).setTitle("title")
const newRow = ActionRowBuilder.from(row).addComponents(component)
const newEmbed = EmbedBuilder.from(embed).setTitle("title")
const newRow = ActionRowBuilder.from(row).addComponents(component)
TÆMBØ
TÆMBØ6mo ago
You're going to get a TypeError if you try to redefine that getter (or any other one) as something else like a string for an embed's description Which is why the guide as well as this tag suggests creating a new builder from it
003002001
0030020016mo ago
So const newEmbed = EmbedBuilder.from(getembed).setDescription("Edited Description") gonna solve the problem for this example
TÆMBØ
TÆMBØ6mo ago
You could call Embed#toJSON() which returns a raw JSON object of the embed which, sure, you can then edit using the same pattern
003002001
0030020016mo ago
Yeah that was my bad, I thought we can just edit it directly but I guess it was how Embeds works for older versions of discord.js
Crλb
CrλbOP6mo ago
so i just use this one or i should do what @TÆMBØ said
TÆMBØ
TÆMBØ6mo ago
You can use that snippet Sabri sent, that's what's best practice
Crλb
CrλbOP6mo ago
ok yeah lets see if it works
Crλb
CrλbOP6mo ago
ok no errors...
No description
Crλb
CrλbOP6mo ago
it sent the message but the bot also crashed
No description
No description
003002001
0030020016mo ago
Can you send a screenshot of your code again?
Crλb
CrλbOP6mo ago
yeah sure, i put newEmbed in embeds because it was gonna get unused
No description
003002001
0030020016mo ago
As we already mentioned, you must send your embed to edit it
Crλb
CrλbOP6mo ago
oh yeah the rat race embed im stupid
003002001
0030020016mo ago
Just focus on msg.reply function
Crλb
CrλbOP6mo ago
ok do we even send a embed since when i sent it is just the "this is a message " string
003002001
0030020016mo ago
Look at your msg.reply function in the first screenshot And compare it with your current msg.reply function
Crλb
CrλbOP6mo ago
No description
No description
Crλb
CrλbOP6mo ago
huh
003002001
0030020016mo ago
No i mean msg.reply({ embeds: ... })
Crλb
CrλbOP6mo ago
ioh wait i gotta put the embed in msg.reply? instead of the string? is it like really obvious and im not seeing it ...
003002001
0030020016mo ago
You just have to send your embed with using embeds option.
<Message>.reply({ embeds: [ ... place your embeds here ...] })
<Message>.reply({ embeds: [ ... place your embeds here ...] })
Thats how you send embeds
Crλb
CrλbOP6mo ago
a you can kinda notice im slow at picking up on stuff
003002001
0030020016mo ago
So its gonna look like msg.reply({ embeds: [ratraceembed] }) It's fine. I am just trying to be sure you learn it.
Crλb
CrλbOP6mo ago
ah
003002001
0030020016mo ago
Otherwise I could just send you the full code xD.
Crλb
CrλbOP6mo ago
but uhhh
No description
Crλb
CrλbOP6mo ago
cant really tell what happened when i ran the command
003002001
0030020016mo ago
Can i see your code again?
Crλb
CrλbOP6mo ago
yeah
Crλb
CrλbOP6mo ago
huh?
No description
Crλb
CrλbOP6mo ago
i think its because it contains the word "TOKEN"
003002001
0030020016mo ago
You have to put your embed variable inside of Array in msg.reply function
Crλb
CrλbOP6mo ago
yeah youre right
003002001
0030020016mo ago
So it's not {embeds: ratraceembed}, it should be {embeds: [ratracembed]} because embeds option supports multiple embeds
Crλb
CrλbOP6mo ago
i did that and when i did the /ratrace command it gave me the same error its weird since my code looks fine
msg.reply({embeds:[ratraceembed]})
.then((SentMessage) => {
const getembed = SentMessage.embeds[0]

const newEmbed = EmbedBuilder.from(getembed).setDescription("Edited Description")

SentMessage.edit({embeds:[newEmbed]})
});
msg.reply({embeds:[ratraceembed]})
.then((SentMessage) => {
const getembed = SentMessage.embeds[0]

const newEmbed = EmbedBuilder.from(getembed).setDescription("Edited Description")

SentMessage.edit({embeds:[newEmbed]})
});
also idk why the formatting looks weird
003002001
0030020016mo ago
Are you sure you saved your changes?
Crλb
CrλbOP6mo ago
yeah i didnt 😭
003002001
0030020016mo ago
Yeah because embeds option expects Array
Crλb
CrλbOP6mo ago
yeah
003002001
0030020016mo ago
So try again and let me know
Crλb
CrλbOP6mo ago
it worked but it didnt change the description
No description
Crλb
CrλbOP6mo ago
oh
No description
TÆMBØ
TÆMBØ6mo ago
Hold on, why are you using interactions but have your parameter named msg? It's a bit of a misnomer since there's different actions and behaviors between interactions and standard messages Can you share a broader context of what you're working with here?
d.js docs
d.js docs6mo ago
To share long code snippets, use a service like gist, sourcebin, pastebin, or similar instead of posting them as large code blocks or files.
TÆMBØ
TÆMBØ6mo ago
To help get a solution for this, what is your end goal here that you're trying to accomplish? Because depending on what you're doing, you might want to take a different approach to it than what you're doing now
Crλb
CrλbOP6mo ago
oh yeah just noticed you replied but heres what im going for, you do the command and the embed pops-up , theres a 3 second countdown and the race begins , the racers can go back or forth and the embed will be edit to show their positions , whoever reaches the finish line first wins

Did you find this page helpful?