【发布时间】:2021-09-14 03:24:26
【问题描述】:
我是 Discord.js 的新手,我最近做了一个“清除”命令,它看起来像这样-
if (message.content.toLowerCase().startsWith(prefix + "purge") || message.content.toLowerCase().startsWith(prefix + "clear")) {
const args = message.content.split(' ').slice(1); // All arguments behind the command name with the prefix
const amount = args.join(' '); // Amount of messages which should be deleted
if (message.channel.type === "DM") return message.reply('Cannot run command inside DMs.')
if (!message.member.hasPermission('MANAGE_MESSAGES') || !message.member.hasPermission('ADMINISTRATOR')) return message.channel.reply('You do not have enough permissions to run this command')
if (!message.guild.me.hasPermission('MANAGE_MESSAGES') || !message.guild.me.hasPermission('ADMINISTRATOR')) return message.reply('I do not have permission to delete messages.')
if (!amount) return message.reply('You haven\'t given an amount of messages which should be deleted!'); // Checks if the `amount` parameter is given
if (isNaN(amount)) return message.reply('The amount parameter isn`t a number!'); // Checks if the `amount` parameter is a number. If not, the command throws an error
if (amount > 99) return message.reply('You can`t delete more than 99 messages at once!'); // Checks if the `amount` integer is bigger than 100
if (amount < 1) return message.reply('You have to delete at least 1 message!'); // Checks if the `amount` integer is smaller than 1
message.channel.messages.fetch({
limit: amount
}).then(messages => { // Fetches the messages
message.channel.bulkDelete(messages) // Bulk deletes all messages that have been fetched and are not older than 14 days (due to the Discord API)
message.channel.send(`Deleted ${amount} messages. Messages purged by ${message.author.username}`)
});
}
}
所以问题是,当我在旧服务器上用旧消息测试我的机器人时,有 23 条新消息和 44 条旧消息。我试图删除 50 条消息。它所做的只是在控制台中记录错误并发送一条消息,指出 50 条消息已被删除。它并没有真正删除 50 条消息,但它发送了一条它确实删除的消息。是否可以仅删除可以删除的最大消息数,然后发送一条消息,说明实际删除了多少消息,这会有所帮助。我举个例子——我想删除 50 条消息,但 10 条不到 14 天,其余超过 14 天。该机器人将删除 10 条消息(它可以像 Carl 机器人一样删除的最大值。)
【问题讨论】:
标签: discord.js