Regales
Regales

Reputation: 27

How Do I Delete This Message After A While (Discord.js)

let noiceEmbed = new discord.MessageEmbed()
        .setAuthor(
          "ռօɮɛʟ",
          "https://i.pinimg.com/236x/d5/e2/c5/d5e2c5c0315e6b1f3cc30189f9dccd82.jpg")
        .setTitle(`<a:playing:799562690129035294> Started Playing`)
        .setThumbnail(song.thumbnail)
        .setColor('RANDOM')
        .addField('Name', song.title, true)
        .addField('Requested By', song.requester, true)
        .addField('Views', song.views, true)
        .addField('Duration', timeString, true)
        queue.textChannel.send(noiceEmbed);

I want to delete this message after 30 seconds to prevent clutter. Any help is appreciated. Thanks in advance!

Upvotes: 0

Views: 149

Answers (3)

AdventuresX0
AdventuresX0

Reputation: 1

You'll Need To Do A .then, And Then Put A Timeout Function.

queue.textChannel.send(noiceEmbed).then((message)=> {
  setTimeout(function(){
    message.delete();
  }, 5000) //Milliseconds (5000 = 5 Seconds);
});

Upvotes: 0

Leonerrr_
Leonerrr_

Reputation: 104

Simple, only use delete function

queue.textChannel.send(noiceEmbed).then((message) => message.delete({ timeout: 30000 }));

Remember, the timeout is represented in miliseconds.
1ms * 1000 = 1s
30000ms = 30s

Upvotes: 0

Radnerus
Radnerus

Reputation: 592

TextChannel#send() returns a promise, so you can either resolve it using then() function or using async-await. Message#delete() has a timeout option, but it will de deprecated in the upcoming version. So delete the message inside a setTimeout() function.

Eg: Using then function:

let noiceEmbed = new discord.MessageEmbed()
        .setTitle(`Embed`)      
        .addField('EmbedField','Value') //your embed.
        message.channel.send(noiceEmbed)
        .then((message)=>setTimeout(()=>message.delete(),1000)); // 1000 is time in milliseconds

Eg:Using async-await: In order to using this, you need to make the whole client event as asynchronous.

client.on('message', async message=>{
    //other commands.
    let noiceEmbed = new Discord.MessageEmbed()
        .setTitle(`Embed`)      
        .addField('EmbedField','Value'); //your embed.
        let embedmessage= await message.channel.send(noiceEmbed);
        setTimeout(()=>{
            embedmessage.delete()
        },5000);
    
});

Also if queue is your guild, then queue.textChannel is not a thing. TextChannel is a type of channel. So you in order to send this embed to send to a particular channel, you need to get the channel by id and then send the embed.

Upvotes: 1

Related Questions