admin管理员组

文章数量:1405170

So I want to delete the bot's message after 5 seconds but I'm not sure how to do that. the message I want to delete is message.channel.send(sender + ' IS A NAUGHTY BOY'); . I know how to delete the message I just don't know how to make it so that it waits 5 seconds before deleting it. the code is below.

 if (msg.includes('swear1') || msg.includes('swear2') || msg.includes('swear3') || msg.includes('swear4') || msg.includes('swear5') || msg.includes('swear6')) {
 message.delete();
 message.author.send('**Please refrain yourself from swearing on this server, Thanks**' );
  message.channel.send(sender + ' IS A NAUGHTY BOY'); // this is the message i want to delete after 5 seconds
 console.log(sender + ' Just Said ' + msg.toUpperCase());
}

So I want to delete the bot's message after 5 seconds but I'm not sure how to do that. the message I want to delete is message.channel.send(sender + ' IS A NAUGHTY BOY'); . I know how to delete the message I just don't know how to make it so that it waits 5 seconds before deleting it. the code is below.

 if (msg.includes('swear1') || msg.includes('swear2') || msg.includes('swear3') || msg.includes('swear4') || msg.includes('swear5') || msg.includes('swear6')) {
 message.delete();
 message.author.send('**Please refrain yourself from swearing on this server, Thanks**' );
  message.channel.send(sender + ' IS A NAUGHTY BOY'); // this is the message i want to delete after 5 seconds
 console.log(sender + ' Just Said ' + msg.toUpperCase());
}
Share Improve this question asked May 25, 2018 at 9:25 VortifexVortifex 11 silver badge2 bronze badges 1
  • Use setTimeout to delete the message set to 5000 – Bindrid Commented May 25, 2018 at 9:29
Add a ment  | 

4 Answers 4

Reset to default 2

You can pass the timeout to the .delete method docs

message.delete(5000);

would do the job.

If you want to send a message and then delete that message after 5 seconds you need to use Promises to get the message that was sent and after that delete it.

message.channel.send(sender + ' IS A NAUGHTY BOY')
    .then(newMessage => newMessage.delete(5000));

This is gonna send the message, and after was sent will trigger the .then promise. You will get the new message object to do whatever you want with it.

Documentation:

  • message.channel.send()
  • message.delete()

If you need to wait some seconds to do a function, use setTimeout Method.

setTimeout(function(){ 
    //Code
 }, 5000); //time in milliseconds

Discord.js updated this is now

message.delete({timeout: 5000})

本文标签: javascripthow do i make my code wait before deleting a message (discordjs)Stack Overflow