简体   繁体   中英

Discord Bot Clear Commands Eror

I took a small break from coding my bot, and I've been playing around with adding modules to make an economy type thing. I was having some issues with that, so I went to check if the 'clear' command worked still. I originally wrote the code without the async function, but now I am using it and I think maybe that has caused a problem? Wondering if anyone knows how to fix it, thanks for checking this out.

The ban/kick commands also aren't working at all, so if anyone could take a look at those?

For the code, I've used bits and pieces of tutorials and guides from around the internet and I think some of that may have interfered with what's already been written. ErrorCode: (node:16360) UnhandledPromiseRejectionWarning: DiscordAPIError: You can only bulk delete messages that are under

module.exports = {
    name: "clear",
    description: "Clears messages",

    async run (client, message, args) {

        const amount = args.join(" ");

        if(!amount) return message.reply('please provide an amount of messages for me to delete')

        if(amount > 100) return message.reply(`you cannot clear more than 100 messages at once`)

        if(amount < 1) return message.reply(`you need to delete at least one message`)

        await message.channel.messages.fetch({limit: amount}).then(messages => {
            message.channel.bulkDelete(messages
    )});

    }
}

As Tyler2P pointed out in the comments, you can only bulk-delete messages under 2 weeks old. If you'd like to keep your command running smoothly, you can filter out messages sent before that time.

// as a general tip, you don't need to use `await` and `.then()`
// use only one at a time
message.channel.messages.fetch({ limit: amount }).then(messages => {
  // filter out all messages sent more than two weeks ago
  messages = messages.filter((msg) => msg.createdTimestamp < new Date(Date.now() - 12096e5).getTime())
 
  message.channel.bulkDelete(messages)
     
  messages.size !== amount
    ? message.channel.send(`Unfortunately, ${amount - messages.size} messages were sent more than two weeks ago, but we deleted the other ${messages.size}.`)
    : message.channel.send(`${messages.size} messages were deleted`);
}); 

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