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
4 Answers
Reset to default 2You 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
版权声明:本文标题:javascript - how do i make my code wait before deleting a message (discord.js) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744239402a2596714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论