不和谐机器人清除命令错误

时间:2020-10-10 10:25:58

标签: javascript discord.js

我在编写机器人程序时花了一点时间,而且我一直在尝试添加一些模块来制作经济型的东西。我当时遇到了一些问题,因此我去检查了“清除”命令是否仍然有效。我最初编写的代码没有异步功能,但是现在我正在使用它,我认为这可能引起了问题?想知道是否有人知道如何修复它,感谢您检查出这个问题。

ban / kick命令也根本不起作用,所以如果有人可以看一下吗?

对于代码,我使用了互联网上的点点滴滴的教程和指南,我认为其中一些可能会干扰已经编写的内容。 错误代码: (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
    )});

    }
}

1 个答案:

答案 0 :(得分:0)

正如Tyler2P在评论中指出的那样,您只能批量删除2周以下的邮件。如果您想使命令平稳运行,则可以过滤掉在此之前发送的消息。

// 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`);
});