Bot not editing embed as expected

const existingMessages = await dchannel.messages.fetch({ limit: 1 });
const existingMessage = existingMessages
?.filter((msg) => msg.author.bot && msg.author.id === interaction.client.user.id)
.first();

if (existingMessage) {
if (existingMessage.embeds.length > 0) {
const existingEmbed = existingMessage.embeds[0];
const updatedEmbed = new EmbedBuilder(existingEmbed)
.addFields({
name: displayName || "Unknown User",
value: formattedDate,
});
await existingMessage.edit({ embeds: [updatedEmbed] });
}
} else {
const existingMessages = await dchannel.messages.fetch({ limit: 1 });
const existingMessage = existingMessages
?.filter((msg) => msg.author.bot && msg.author.id === interaction.client.user.id)
.first();

if (existingMessage) {
if (existingMessage.embeds.length > 0) {
const existingEmbed = existingMessage.embeds[0];
const updatedEmbed = new EmbedBuilder(existingEmbed)
.addFields({
name: displayName || "Unknown User",
value: formattedDate,
});
await existingMessage.edit({ embeds: [updatedEmbed] });
}
} else {
The bot should edit the original embed if there is one with the new data from the command, which it does successfully. However, if there is a message sent in the channel from another user (not the bot) which is more recent than the embed, instead of updating the original embed it creates a new one - which it should only do if there's no embed in the channel at all.
3 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
Syjalo
Syjalo17mo ago
The message count in a channel isn't limited and fetch returns only 50 (by default, max 100). It's better to have a db record about the channel and the message with the embed.
scotty
scottyOP17mo ago
Returning 50 isn’t an issue as the channel is locked, message could would never exceed ≈5, just need to ensure that the embed still updates if a user posts under it, and if that did happen there would only ever be up to 5ish messages under the embed anyway
Want results from more Discord servers?
Add your server