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 toolkit4mo 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
Cwλb
CwλbOP4mo ago
No description
No description
003002001
0030020014mo 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.
Cwλb
CwλbOP4mo 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 docs4mo ago
:class: Message @14.15.3 Represents a message on Discord.
Cwλb
CwλbOP4mo 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
0030020014mo ago
Answer for first question is: it doesn't matter, if you want to make your bot mention user then use reply.
Cwλb
CwλbOP4mo 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
0030020014mo 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!
Cwλb
CwλbOP4mo ago
ok do i replace <Message> with msg ?
003002001
0030020014mo ago
Yeah replace it with your message variable, in example that i sent you message variable is SentMessage
Cwλb
CwλbOP4mo ago
ok
Cwλb
CwλbOP4mo ago
what do i put inside of SentMessage.edit() ? this is my current code btw
No description
d.js docs
d.js docs4mo ago
:guide: Popular Topics: Embeds - Resending and editing read more
003002001
0030020014mo 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
Cwλb
CwλbOP4mo ago
ok assuming MessageEmbed is the embed i made with the EmbedBuilder class right?
TÆMBØ
TÆMBØ4mo 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
Cwλb
CwλbOP4mo ago
i know
Cwλb
CwλbOP4mo ago
i think i fixed it
No description
TÆMBØ
TÆMBØ4mo ago
You're still missing the payload you want to reply with
d.js docs
d.js docs4mo 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);
Cwλb
CwλbOP4mo ago
i need to use the method .catch() ?
TÆMBØ
TÆMBØ4mo ago
No, I'm talking about the lack of any parameters you're passing to reply()
Cwλb
CwλbOP4mo ago
ooohh so what parameters does .reply use?
TÆMBØ
TÆMBØ4mo ago
:IRT_This:
Cwλb
CwλbOP4mo 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Ø4mo 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
0030020014mo 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Ø4mo ago
Well, Embed#description is a getter, not a property, which you can't overwrite
d.js docs
d.js docs4mo 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Ø4mo 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
0030020014mo ago
So const newEmbed = EmbedBuilder.from(getembed).setDescription("Edited Description") gonna solve the problem for this example
TÆMBØ
TÆMBØ4mo 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
0030020014mo 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
Cwλb
CwλbOP4mo ago
so i just use this one or i should do what @TÆMBØ said
TÆMBØ
TÆMBØ4mo ago
You can use that snippet Sabri sent, that's what's best practice
Cwλb
CwλbOP4mo ago
ok yeah lets see if it works
Cwλb
CwλbOP4mo ago
ok no errors...
No description
Cwλb
CwλbOP4mo ago
it sent the message but the bot also crashed
No description
No description
003002001
0030020014mo ago
Can you send a screenshot of your code again?
Cwλb
CwλbOP4mo ago
yeah sure, i put newEmbed in embeds because it was gonna get unused
No description
003002001
0030020014mo ago
As we already mentioned, you must send your embed to edit it
Cwλb
CwλbOP4mo ago
oh yeah the rat race embed im stupid
003002001
0030020014mo ago
Just focus on msg.reply function
Cwλb
CwλbOP4mo ago
ok do we even send a embed since when i sent it is just the "this is a message " string
003002001
0030020014mo ago
Look at your msg.reply function in the first screenshot And compare it with your current msg.reply function
Cwλb
CwλbOP4mo ago
No description
No description
Cwλb
CwλbOP4mo ago
huh
003002001
0030020014mo ago
No i mean msg.reply({ embeds: ... })
Cwλb
CwλbOP4mo 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
0030020014mo 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
Cwλb
CwλbOP4mo ago
a you can kinda notice im slow at picking up on stuff
003002001
0030020014mo ago
So its gonna look like msg.reply({ embeds: [ratraceembed] }) It's fine. I am just trying to be sure you learn it.
Cwλb
CwλbOP4mo ago
ah
003002001
0030020014mo ago
Otherwise I could just send you the full code xD.
Cwλb
CwλbOP4mo ago
but uhhh
No description
Cwλb
CwλbOP4mo ago
cant really tell what happened when i ran the command
003002001
0030020014mo ago
Can i see your code again?
Cwλb
CwλbOP4mo ago
yeah
Cwλb
CwλbOP4mo ago
huh?
No description
Cwλb
CwλbOP4mo ago
i think its because it contains the word "TOKEN"
003002001
0030020014mo ago
You have to put your embed variable inside of Array in msg.reply function
Cwλb
CwλbOP4mo ago
yeah youre right
003002001
0030020014mo ago
So it's not {embeds: ratraceembed}, it should be {embeds: [ratracembed]} because embeds option supports multiple embeds
Cwλb
CwλbOP4mo 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
0030020014mo ago
Are you sure you saved your changes?
Cwλb
CwλbOP4mo ago
yeah i didnt 😭
003002001
0030020014mo ago
Yeah because embeds option expects Array
Cwλb
CwλbOP4mo ago
yeah
003002001
0030020014mo ago
So try again and let me know
Cwλb
CwλbOP4mo ago
it worked but it didnt change the description
No description
Cwλb
CwλbOP4mo ago
oh
No description
TÆMBØ
TÆMBØ4mo 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 docs4mo 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Ø4mo 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
Cwλb
CwλbOP4mo 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
Want results from more Discord servers?
Add your server