Home > Software design >  How could I make the setTimeout(function(){message.delete();} function delete a Discord Message Embe
How could I make the setTimeout(function(){message.delete();} function delete a Discord Message Embe

Time:01-06

I'm getting into javascript, or more specifically discord.js and I've decided to try to make a moderation bot. I created a ban command, and after it bans the user, it sends a message embed showing the info of the ban, such as the reasoning of the ban, the user's tag, etc. I've decided to use the setTimeout function to delete the embed after 5 seconds. But, I can't figure out how to. I know exactly how to use the setTimeout function, it's just that I don't know what to do to delete the embed. Here's my code:

            let kickEmbed = new Discord.MessageEmbed()
            .setTitle('Member Kicked!')
            .setColor("GREEN")
            .addFields(
                {name: `Member:`, value: `${memberTarget}`},
                {name: `Moderator`, value: `${message.author}`},
                {name: `Reason`, value: `${kickReason}`}
            )
            await message.channel.send({embeds: [kickEmbed]})
            setTimeout(function(){
                kickEmbed.delete();
            }, 5000);

The problem is the "kickEmbed.delete();" part. I don't know what to put there. kickEmbed doesn't work, msg doesn't work, message doesn't work either. Here's an error if that helps: 'kickEmbed.delete' is not a function

Can anyone help me solve this?

CodePudding user response:

setTimeout(function(newArg) {
  newArg.delete();
}, 5000, KickEmbed);

I don't like making posts but first part to solution is understanding what you send to a timeout. If you send no arguments it's a global call and if that updates/changes or is removed errors in code occur so passing the value you intend on usage is optimal here. Second part is discord and its proper usage. GL

CodePudding user response:

This is because you are running it on the MessageEmbed object itself. Make it so you save the sent message to a variable, rather than the embed object

let kickEmbed = new Discord.MessageEmbed()
            .setTitle('Member Kicked!')
            .setColor("GREEN")
            .addFields(
                {name: `Member:`, value: `${memberTarget}`},
                {name: `Moderator`, value: `${message.author}`},
                {name: `Reason`, value: `${kickReason}`}
            )
            let sentMessage = await message.channel.send({embeds: [kickEmbed]})
            setTimeout(function(){
                sentMessage.delete();
            }, 5000)
  •  Tags:  
  • Related