如果用户在 discord.js 中提供了 20 条消息(超过 14 天)进行批量删除,我如何仅删除 10 条消息

Posted

技术标签:

【中文标题】如果用户在 discord.js 中提供了 20 条消息(超过 14 天)进行批量删除,我如何仅删除 10 条消息【英文标题】:How do I delete only 10 messages if the user provided 20 messages (older than 14 days) for bulk deleting in discord.js 【发布时间】: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 机器人一样删除的最大值。)

【问题讨论】:

【参考方案1】:

TextChannel.bulkDelete() 中有一个filterOld 参数。以下是你如何实现它:

<TextChannel>.bulkDelete(100, true);
//<TextChannel> just means instance of TextChannel, like message.channel

【讨论】:

抱歉回复晚了,谢谢!这解决了它。【参考方案2】:

要删除号码消息,您只需在.bulkDelete 方法中提供号码消息。并且您需要等到使用.then 完成删除,而不是像提供代码一样在删除后立即发送它

示例代码

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.bulkDelete(amount).then(messages =>  // Fetches the messages
        message.channel.send(`Deleted $messages.size messages. Messages purged by $message.author.username`)
    );

【讨论】:

感谢您的回答。它确实解决了我在删除消息之前机器人发送消息的问题,但它仍然没有解决主要错误:(如果用户要求清除 20 条消息和 10 条消息,它仍然不会删除它可以删除的最大消息超过 14 天。

以上是关于如果用户在 discord.js 中提供了 20 条消息(超过 14 天)进行批量删除,我如何仅删除 10 条消息的主要内容,如果未能解决你的问题,请参考以下文章

我如何在 discord.js 中获得提及用户的用户名

无法让 Discord.js 在命令中提及用户

从数组 discord.js v12 访问项目

无法添加角色,Discord.js

Discord.js - 删除所有包含角色的用户的命令

Discord.js - .then 即使在函数中也没有定义?