简体   繁体   中英

Purge command not repeating bulk delete

I've got a purge command on my Discord.js v12.2.0 bot and it doesn't work as I would've hoped. I wanted to be able to purge up to 1,000 messages by repeating bulk delete, but unfortunately it doesn't actually repeat bulk delete and it only bulk deletes once. Any help would be super appreciated.

Here's my code:

module.exports = {
    name: 'purge',
    description: "Purge messages.",
    usage: '[amount]',
    aliases: ['prune'],
    execute(client, Discord, message, args, prefix, wrongchannelpublic, wrongchannelstaff, talkingserverbump, disabledcommand, nopermission, noarguments, colour, footer1, footer2) {
        if (!message.member.roles.cache.some(role => role.id === '703316042965057546')) { return message.delete({ timeout: 100 }), message.channel.send(nopermission).then(sentMessage => sentMessage.delete({ timeout: 7000})) }
        if (!args[0] || isNaN(args[0])) { return message.delete({ timeout: 100 }), message.channel.send(noarguments).then(sentMessage => sentMessage.delete({ timeout: 7000})) }
        if (args[0] < 1 || args[0] > 1000) { return message.delete({ timeout: 100 }), message.channel.send(`Please provide a number between 1 and 100, ${message.author}!`).then(sentMessage => sentMessage.delete({ timeout: 7000})) }
        message.delete({ timeout: 100 })
        .then(() => {
            let remainder = args[0] % 100;
            let numOfTimes = (args[0]-remainder)/100;
            try {
                for (let i = 0; i < numOfTimes; i++) {
                    message.channel.bulkDelete(100);
                }
                if (remainder > 0) { message.channel.bulkDelete(remainder); }
            } catch (err) {
                const purgeerror = new Discord.MessageEmbed()
                  .setDescription(`**Execution error**\nThe \`purge\` command ran by ${message.author} in ${message.channel} failed to execute.\nError: \`${err}\``)
                  .setColor('#ff0000')
                  .setTimestamp();
                message.channel.send(`<:cross:740242485728772137> Error: \`${err}\``);
                client.channels.cache.get('740251593382821920').send(purgeerror);
                console.log(`The purge command ran by ${message.author.tag} in #${message.channel.name} failed to execute. Error: ${err}`);
            }
        if (args[0] === "1") {
            setTimeout(()=>{ message.channel.send(`<:tick:740242485908996128> Purged ${args[0]} message.`).then(sentMessage => sentMessage.delete({ timeout: 4000})) }, 1000)
        } else {
            setTimeout(()=>{ message.channel.send(`<:tick:740242485908996128> Purged ${args[0]} messages.`).then(sentMessage => sentMessage.delete({ timeout: 4000})) }, 1000)
        }
        })
    }
}

Use await so it will wait one or two seconds between each bulkDelete (at this time, all the bulkDelete are sent but they delete the same messages):

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

module.exports = {
    name: 'purge',
    description: "Purge messages.",
    usage: '[amount]',
    aliases: ['prune'],
    execute(client, Discord, message, args, prefix, wrongchannelpublic, wrongchannelstaff, talkingserverbump, disabledcommand, nopermission, noarguments, colour, footer1, footer2) {
        if (!message.member.roles.cache.some(role => role.id === '703316042965057546')) { return message.delete({ timeout: 100 }), message.channel.send(nopermission).then(sentMessage => sentMessage.delete({ timeout: 7000})) }
        if (!args[0] || isNaN(args[0])) { return message.delete({ timeout: 100 }), message.channel.send(noarguments).then(sentMessage => sentMessage.delete({ timeout: 7000})) }
        if (args[0] < 1 || args[0] > 1000) { return message.delete({ timeout: 100 }), message.channel.send(`Please provide a number between 1 and 100, ${message.author}!`).then(sentMessage => sentMessage.delete({ timeout: 7000})) }
        message.delete({ timeout: 100 })
        .then(async () => {
            let remainder = args[0] % 100;
            let numOfTimes = (args[0]-remainder)/100;
            try {
                for (let i = 0; i < numOfTimes; i++) {
                    await message.channel.bulkDelete(100);
                    await delay(1000); // wait one second
                }
                if (remainder > 0) { message.channel.bulkDelete(remainder); }
            } catch (err) {
                const purgeerror = new Discord.MessageEmbed()
                  .setDescription(`**Execution error**\nThe \`purge\` command ran by ${message.author} in ${message.channel} failed to execute.\nError: \`${err}\``)
                  .setColor('#ff0000')
                  .setTimestamp();
                message.channel.send(`<:cross:740242485728772137> Error: \`${err}\``);
                client.channels.cache.get('740251593382821920').send(purgeerror);
                console.log(`The purge command ran by ${message.author.tag} in #${message.channel.name} failed to execute. Error: ${err}`);
            }
        if (args[0] === "1") {
            setTimeout(()=>{ message.channel.send(`<:tick:740242485908996128> Purged ${args[0]} message.`).then(sentMessage => sentMessage.delete({ timeout: 4000})) }, 1000)
        } else {
            setTimeout(()=>{ message.channel.send(`<:tick:740242485908996128> Purged ${args[0]} messages.`).then(sentMessage => sentMessage.delete({ timeout: 4000})) }, 1000)
        }
        })
    }
}

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