【问题标题】:Discord.JS Prune Command Also Prunes User PinsDiscord.JS 修剪命令还修剪用户引脚
【发布时间】:2020-01-14 20:53:22
【问题描述】:

我有一个用于我正在构建的 Discord 审核机器人的 prune 命令,它几乎可以满足我的需要。但它也有点过分了。它还修剪用户引脚,这对于简单的修剪命令来说有点太多了。

我想知道我需要进行什么编辑,这样它就不能删除用户 pin。

这是我的代码:

const { prefix } = require('../config.json');
const Discord = require('discord.js');

module.exports = {
    name: 'prune',
    description: 'Prune up to 99 messages.',
    aliases: '[p]',
    execute(message, args) {
        if(message.member.roles.find("name", "Helper")){
            const amount = parseInt(args[0]) + 1;
            if (isNaN(amount)) {
                return message.reply('that doesn\'t seem to be a valid number.');

                } else if (amount <= 1 || amount > 100) {
                    return message.reply('you need to input a number between 1 and 99.');
                }

            message.channel.bulkDelete(amount, true).catch(err => {
                    console.error(err);
                    message.channel.send('there was an error trying to prune messages in this channel!');
            });

        }else{
            message.channel.send(`Access Denied`);
        }
}};

当然,它应该修剪消息。它确实如此。但是,不应该运行用户密码。

因为它也修剪用户密码。

【问题讨论】:

标签: module discord.js fs


【解决方案1】:

您可以使用Collection 的消息,而不是将大量消息传递给批量删除。

要获取频道中的消息,请使用TextChannel.fetchMessages()。然后,您可以使用Collection.filter() 方法检索新的集合排除固定消息。在谓词函数中,您应该检查消息的pinned 属性。

考虑这个例子...

message.channel.fetchMessages({ limit: amount })
  .then(fetchedMessages => {
    const messagesToPrune = fetchedMessages.filter(msg => !msg.pinned);
    return message.channel.bulkDelete(messagesToPrune, true);
  })
  .then(prunedMessages => {
    message.channel.send(`Deleted ${prunedMessages.size} message${prunedMessages.size !== 1 ? 's' : ''}.`);
  })
  .catch(console.error);

【讨论】:

  • 这会导致控制台中出现错误,它实际上并没有做任何事情 这是错误 ``` (node:6154) DeprecationWarning: Collection#find: pass a function instead ReferenceError: messagesToPrune is未在 process._tickCallback (internal/process/next_tick.js:188:7 ) ```
  • @JoshuaVL1988 我的代码在声明变量时有错字,我很抱歉。我已经编辑了答案来修复它。
  • 谢谢。这似乎正在做我需要的事情。但是,它响应的消息是否假设为“已删除 [对象映射] 消息。”?还是假设更具体?
  • @JoshuaVL1988 我再次编辑了答案来解决这个问题。它现在将显示已删除消息的 number 条,而不是尝试显示整个 Collection
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 2016-05-23
  • 1970-01-01
  • 2019-03-20
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多