[英]Discordjs how to set delay before logging sent message or attachment?
因此,当我记录发送到特定服务器的每条消息时,我的 Discord 日志记录机器人一直达到速率限制,我想知道如何让它在消息发送几秒钟后记录已发送的消息,这样它就不会达到速率限制,这里是代码:
client.on('message', message => {
if (message.author.bot) return;
if (message.channel.type === 'dm') return;
const channel = client.users.cache.get('256916902939590656');
if(channel) {
if (message.attachments.size > 0) {
const Attachment = (message.attachments).array();
Attachment.forEach(function(attachment) {
const deletedMessageEmbed = new MessageEmbed()
.setColor('#cc5500')
.setAuthor(message.author.tag, message.author.avatarURL({ format: 'png', dynamic: true }))
.setTitle('Attachment Content')
.setURL(attachment.url)
.addField('Author', `${message.author}`)
.addField('Server', `${message.guild.name}`)
.addField('Name', `${attachment.name}`)
.setImage(attachment.proxyURL)
.addField('Channel', `<#${message.channel.id}> #${message.channel.name}`)
.setFooter(`Message ID: ${message.id}`)
.setTimestamp();
try {
client.users.cache.get('256916902939590656').send(deletedMessageEmbed);
}
catch (err) {
message.channel.send('No logs channel found. Please make sure I have access to it and make sure the channel name is called logs');
}
});
}
else {
const messageContent = new MessageEmbed()
.setColor('#cc5500')
.setURL(message.url)
.setAuthor(message.author.tag, message.author.avatarURL({ format: 'png', dynamic: true }))
.setTitle('Message Content')
.addField('Author', `${message.author}`)
.addField('Server', `${message.guild.name}`)
.addField('Channel', `<#${message.channel.id}> #${message.channel.name}`)
.setDescription(message.content)
.setFooter(`Message ID: ${message.id}`)
.setTimestamp();
try {
client.users.cache.get('256916902939590656').send(messageContent);
}
catch (err) {
message.channel.send('No logs channel found. Please make sure I have access to it and make sure the channel name is called logs');
}
}
}
});
上面的代码发送附件或已发送消息的嵌入: Sent Message Example Sent Attachment Example
我希望机器人在特定的设定时间后发送发送的图像或附件,这样它就不会一次发送所有内容并达到速率限制。
const wait = (ms) => new Promise(resolve => setTimeout(resolve,ms));
在async
函数中
await wait(5000)//waits 5 secons
或使用then()
wait(5000).then(()=>{
//do something...
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.