'Discord.js editing embed description problem

So I have this discord.js bot which sends an embed message and adds a value to it's description on command and deletes the value after 5 seconds, but every time I add a value to the description when it has already a value in it, the value that I add disappears when the first value gets deleted after those 5 seconds.

here is my code:

const { Client, Intents, MessageEmbed } = require('discord.js')
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] })

const mongoose = require('mongoose')
mongoose.connect(/*db adress*/, {useNewUrlParser: true, useUnifiedTopology: true})
const Schema = mongoose.Schema

const embedSchema = new Schema({
    exists: { type: String, required: true },
    embedId: { type: String, required: true },
    channelId: { type: String, required: true }
})

const Embed = mongoose.model('Embed', embedSchema, 'embed')

const embedMessage = new MessageEmbed()
    .setDescription('empty')

client.on('messageCreate', async message => {
    if (message.content === '.sendEmbed') {
        const embed = await message.channel.send({ embeds: [embedMessage] })

        const saveEmbed = new Embed({
            exists: 'true',
            embedId: embed.id,
            channelId: message.channel.id
        })
        saveEmbed.save()
    } else if (message.content.startsWith('.add')) {
        const wordsInMessage = message.content.split(' ')

        if (!wordsInMessage[2]) {
            Embed.find({ exists: 'true' }, async (err, embed) => {
                const word = wordsInMessage[1]
                const oldEmbed = await message.channel.messages.fetch(embed[0].embedId)

                const newEmbed = oldEmbed.embeds[0]
                const embedDescription = newEmbed.description.replaceAll('empty', '')

                newEmbed.setDescription(`${embedDescription}\n${word}`)
                oldEmbed.edit({ embeds: [newEmbed] })

                setTimeout(() => updateValue(word), 5000)
            })
        }
    }
})

function updateValue(word) {
    Embed.find({ exists: 'true' }, async (err, embed) => {
        Array.prototype.replaceAt = function(index, replacement) {
            if (index >= this.length) {
                return this.valueOf()
            }
        
            return this.splice(0, index) + replacement + this.splice(index + 1)
        }

        const channel = client.channels.cache.get(embed[0].channelId)
        const getEmbed = await channel.messages.fetch(embed[0].embedId)

        const embedMessage = await getEmbed.embeds[0]

        const wordsPosition = embedMessage.description.split('\n').indexOf(word)
        const newValue = embedMessage.description.split('\n')[wordsPosition].split(' ').replaceAt(0, 'empty')

        embedMessage.setDescription(`${newValue}`)
        getEmbed.edit({ embeds: [embedMessage] })
    })
}

client.login(/*bot token*/)


Solution 1:[1]

  • I'm guessing that you want to edit the description of an existing embed
const { MessageEmbed } = require("discord.js");
const Schema = require("path-to-schema");

const data = await Schema.find({ exists: true });
const msg = await message.channel.messages.fetch(data.embedId);
if(!msg && !msg.embeds) return;
const oldEmbed = msg.embeds[0];
const newEmbed = new MessageEmbed(oldEmbed.toJSON()).setDescription("new description with existing embed details");
await msg.edit({ embeds: [newEmbed] });
  • Details:

So the above code basically fetches the message ID from your Schema and fetches the message in the current channel. It creates a new Embed with the existing embed's JSON and updates the description, ultimately updating the old message.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Darshan B