【问题标题】:bulkDelete function in discord.js deletes more than what it is supposed to deletediscord.js 中的 bulkDelete 函数删除的内容超过了它应该删除的内容
【发布时间】:2021-01-13 02:51:28
【问题描述】:

我是 js 的新手,我想尝试使用 discord.js 创建一个不和谐的机器人

我使用 youtube 上的教程来编写此代码。 这是帮助我构建此代码的视频教程的链接 (https://www.youtube.com/watch?v=INQgI-MQcj0) .这段代码删除的比它应该删除的要多,不知道出了什么问题。

这是我的代码...

    module.exports = {
    name: 'clear',
    description: "Clears the certain number of messsages.",
    async execute(message, args){
        if(!args[0]) return message.reply("Please enter the amount of messages that you want to clear!");
        if(isNaN(args[0])) return message.reply("Please enter real number!");

        if(args[0] > 100) return message.reply("You cannnot delete more than 100 messages at a time!");
        if(args[0] < 1) return message.reply("You must delete at least one message");

        await message.channel.messages.fetch({Limit: args[0]}).then(messages =>{
            message.channel.bulkDelete(messages);
        });
      }
    }

这是主要的fn

const Discord = require('discord.js');

const client = new Discord.Client();

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

client.once('ready', () => {
    console.log('Knight is online!');
});

client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    }
    else if(command === 'hi'){
        client.commands.get('hi').execute(message, args);
    }
    else if(command === 'token'){
        client.commands.get('token').execute(message, args);
    }
    else if(command === 'hey'){
        client.commands.get('hey').execute(message, args);
    }
    else if(command === "kick_permission"){
        client.commands.get("kick_permission").execute(message, args);
    }
    else if(command === "rules"){
        client.commands.get("rules").execute(message, args, Discord);
    }
    else if(command === 'clear'){
        client.commands.get('clear').execute(message, args);
    }
    
});

client.login('token');

这是它不小心删除整个文本频道后出现的错误。

(node:5876) UnhandledPromiseRejectionWarning: DiscordAPIError: You can only bulk delete messages that are under 14 days old.    at RequestHandler.execute (D:\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async RequestHandler.push (D:\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:39:14)
    at async TextChannel.bulkDelete (D:\DiscordBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:342:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:5876) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:5876) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

请帮帮我,谢谢:)

【问题讨论】:

  • 您所说的删除超出其预期是什么意思?
  • 如果我输入“-clear 3”,它会从文本频道中删除超过 3 条消息

标签: node.js discord.js


【解决方案1】:

你在传递选项时有一个错字:{ Limit: args[0] }limit应该是小写的。因此,该功能尝试批量删除 100 条消息,而不仅仅是您限制的这么多消息。所以用{ limit: args[0] }替换{ Limit: args[0] }

顺便说一句,您可以过滤超过 14 天的消息(您只能删除早于 14 天的消息),您可以使用以下函数传递参数:https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=bulkDelete (filterOld)

【讨论】:

  • 这是正确的答案,拼写错误实际上有很大的不同,它会不可靠地删除消息。请单击此答案旁边的勾号将其标记为答案。
  • 非常感谢 Xandrrr .... 这解决了我的问题
【解决方案2】:

您似乎正在尝试删除超过 14 天的消息(Discord API 只允许 bulkDelete 函数删除 14 天以下的消息)。尝试清除最近的消息。我用你的代码和我的机器人作为测试,它运行良好。 请告诉我这是如何工作的。

除此之外,下面的答案还提到了您如何用大写 L 拼写 limit。这就是您的机器人不可靠地删除消息的原因。

【讨论】:

  • 非常感谢...它解决了我的问题。 :)
猜你喜欢
  • 1970-01-01
  • 2013-05-25
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 2021-04-04
相关资源
最近更新 更多