如何在一段时间后删除此消息(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);

我想在30秒后删除此消息以防止混乱。感谢您的帮助。提前感谢!

TextChannel#send()返回一个promise,因此您可以使用then()函数或使用异步等待来解决它。Message#delete()有一个超时选项,但在即将推出的版本中将不再推荐使用。因此,删除setTimeout()函数中的消息。

例如:使用then函数:

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

例如:使用异步等待:为了使用它,您需要将整个客户端事件设置为异步。

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

});

同样,如果queue是你的公会,那么queue.textChannel就不是一个东西。TextChannel是一种信道类型。因此,为了将此嵌入发送到特定频道,您需要通过id获取频道,然后发送嵌入。

简单,只使用delete函数

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

请记住,超时以miliseconds表示
1ms * 1000 = 1s
30000ms = 30s

您需要执行.then,然后放入超时函数。

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

最新更新