Home > Software design >  How to clear previous Discord.MessageEmbed data in Discord.js
How to clear previous Discord.MessageEmbed data in Discord.js

Time:06-30

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)
                }
              
            })

      })

CodePudding user response:

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)
    }
})

CodePudding user response:

You don't need a embed = {}, instead use client.snipes = new Map() as collector. So if someone delete message when the bot online, the bot can detect it.

client.snipes = new Map()
client.on('messageDelete', function(message, channel) {
    client.snipes.set(message.channel.id, {
        content: message.content,
        author: message.author,
        image: message.attachments.first() ? message.attachments.first().proxyURL : null
    })
}) //This will be your collector on your index file.

Then create a command file.

const msg = client.snipes.get(message.channel.id)
      if(!msg) return message.channel.send("Didn't find any deleted messages.")
        
      const embed = new MessageEmbed()
      .setDescription(`Your_Message`)
      .setTimestamp()
        
      if(msg.image) embed.setImage(msg.image) //If image deleted, it will go here.
      message.channel.send({ embeds: [embed] })
  • Related