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.)
76 Replies
- 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 OPFirst 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.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?
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 ?
Answer for first question is: it doesn't matter, if you want to make your bot mention user then use reply.
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 )
For other questions, let me show you how to place codes correctly.
That is how its gonna look like
Try again and let me know!
ok
do i replace <Message> with msg ?
Yeah replace it with your message variable, in example that i sent you message variable is SentMessage
ok
what do i put inside of SentMessage.edit() ? this is my current code btw
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 messageok
assuming MessageEmbed is the embed i made with the EmbedBuilder class right?
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 sendi know
i think i fixed it
You're still missing the payload you want to reply with
i need to use the method .catch() ?
No, I'm talking about the lack of any parameters you're passing to
reply()
ooohh
so what parameters does .reply use?
:IRT_This:
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?
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@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?
Well, Embed#description is a getter, not a property, which you can't overwrite
Structures from the API cannot be edited directly. To do so, you can create a new structure (a builder) using the
.from()
method
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
So
const newEmbed = EmbedBuilder.from(getembed).setDescription("Edited Description")
gonna solve the problem for this exampleYou could call Embed#toJSON() which returns a raw JSON object of the embed which, sure, you can then edit using the same pattern
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
so i just use this one or i should do what @TÆMBØ said
You can use that snippet Sabri sent, that's what's best practice
ok yeah lets see if it works
ok no errors...
it sent the message but the bot also crashed
Can you send a screenshot of your code again?
yeah sure, i put newEmbed in embeds because it was gonna get unused
As we already mentioned, you must send your embed to edit it
oh yeah the rat race embed im stupid
Just focus on msg.reply function
ok
do we even send a embed since when i sent it is just the "this is a message " string
Look at your msg.reply function in the first screenshot
And compare it with your current msg.reply function
huh
No i mean
msg.reply({ embeds: ... })
ioh
wait
i gotta put the embed in msg.reply?
instead of the string?
is it like really obvious and im not seeing it ...
You just have to send your embed with using embeds option.
Thats how you send embeds
a
you can kinda notice im slow at picking up on stuff
So its gonna look like
msg.reply({ embeds: [ratraceembed] })
It's fine. I am just trying to be sure you learn it.ah
Otherwise I could just send you the full code xD.
but uhhh
cant really tell what happened when i ran the command
Can i see your code again?
yeah
huh?
i think its because it contains the word "TOKEN"
You have to put your embed variable inside of Array in msg.reply function
yeah youre right
So it's not
{embeds: ratraceembed}
, it should be {embeds: [ratracembed]}
because embeds option supports multiple embedsi did that
and when i did the /ratrace command it gave me the same error
its weird since my code looks fine
also idk why the formatting looks weird
Are you sure you saved your changes?
yeah
i didnt 😭
Yeah because embeds option expects Array
yeah
So try again and let me know
it worked but it didnt change the description
oh
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?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
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