Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to clear previous Discord.MessageEmbed data in Discord.js

I’m trying to make a snipe command for the dc bot but I can’t get the embed to reset. Tried putting embed = {} in different locations, then it tries sending an empty message the next time and errors out. Also it’s let embed now since I was testing, tried const first. Edit: works now when checking messages elsewhere, should have done that to start with. Code:

bot.on('messageDelete', message => {
        let embed = new Discord.MessageEmbed()
          .setAuthor(`${message.author.username} (${message.author.id})`, message.author.avatarURL())
          .setDescription(message.content || "None")
          
          bot.on('message', message => {
          const args = message.content.slice(PREFIX.length).split(/ +/);

          const cmd = args.shift().toLowerCase();
      
              if (cmd === 'msg'){
                message.channel.send(embed)
                }
              
            })

      })

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Every time a message is deleted you are resubscribing to the message event.
I would suggest taking some of that logic to the outside of that scope.

let embed = null;

bot.on('messageDelete', message => {
    embed = new Discord.MessageEmbed()
        .setAuthor(`${message.author.username} (${message.author.id})`, message.author.avatarURL())
        .setDescription(message.content || "None")
})

bot.on('message', message => {
    const args = message.content.slice(PREFIX.length).split(/ +/);
    const cmd = args.shift().toLowerCase();

    if (cmd === 'msg' && embed){
        message.channel.send(embed)
    }
})
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading