Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
807 views
in Technique[技术] by (71.8m points)

node.js - bulkDelete function in discord.js deletes more than what it is supposed to delete

Iam a newbie to js and i wanted to try and create a discord bot using discord.js

I used the tutorial from youtube to do this code. This is the link of the video tutorial which helped me build this code (https://www.youtube.com/watch?v=INQgI-MQcj0) .This code deletes more than what its supposed to delete, idont know what went wrong.

this is my code...

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

This is the main 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');

This is the error it gave after accidentally deleting the whole text channel.

(node:5876) UnhandledPromiseRejectionWarning: DiscordAPIError: You can only bulk delete messages that are under 14 days old.    at RequestHandler.execute (D:DiscordBot
ode_modulesdiscord.jssrc
estRequestHandler.js:154:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async RequestHandler.push (D:DiscordBot
ode_modulesdiscord.jssrc
estRequestHandler.js:39:14)
    at async TextChannel.bulkDelete (D:DiscordBot
ode_modulesdiscord.jssrcstructuresinterfacesTextBasedChannel.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.

Help me out please and Thanks :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have a typo when passing the Options: { Limit: args[0] } limit Should be lower case. So the function tries to bulk delete 100 messages not only so many messages you limited. So replace { Limit: args[0] } with { limit: args[0] }.

And BTW you can filter messages which are older than 14 days (You can only delete messages which are younger than 14 days) with an argument you can pass with the function: https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=bulkDelete (filterOld)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...