简体   繁体   中英

Discordjs how to set delay before logging sent message or attachment?

So my Discord logging bot keeps hitting rate limits as I log every message sent to a specific server, I was wondering how can I get it to log the sent message a few seconds after the message is sent so it doesn't hit the rate limits, here is the code:

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

The code above sends an embed of the attachment or message sent: Sent Message Example Sent Attachment Example

I would like the bot to send the sent image or attachment after a specific set amount of time so it doesn't send it all at once and reach rate limits.

const wait = (ms) => new Promise(resolve => setTimeout(resolve,ms));

in an async function

await wait(5000)//waits 5 secons

or use then()

wait(5000).then(()=>{
  //do something...
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM