Rtxxz
Rtxxz

Reputation: 1

Purge Command Discord.JS Works But Error Pops Up?

const Command = require('../../Structures/Command'); const Discord = require('discord.js');

module.exports = class extends Command {

    constructor(...args) {
        super(...args, {
            aliases: ['clear']
        });
    }

    async run(message, args) {
            // This command removes all messages from all users in the channel, up to 100.
            if(!message.member.hasPermission("KICK_MEMBERS"))
              return message.reply("Sorry, you don't have permissions to use this!");
            // get the delete count, as an actual number.
            const deleteCount = parseInt(args[0], 10);

            // Ooooh nice, combined conditions. <3
            if(!deleteCount || deleteCount < 1 || deleteCount > 100)
              return message.reply("Please provide a number between 1 and 100 for the number of messages to delete");

            // So we get our messages, and delete them. Simple enough, right?
            const fetched = await message.channel.messages.fetch({limit: deleteCount});
            message.channel.send(fetched.size + ` deleted messages by ${message.author.tag}`).then(msg => {msg.delete(2000)});
            message.channel.bulkDelete(fetched)   
    }

};

This works perfectly but there is an error saying:

(node:1132) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied options is not an object.
    at Message.delete (C:\Users\xdswe\Desktop\Zero\node_modules\discord.js\src\structures\Message.js:501:44)
    at C:\Users\xdswe\Desktop\Zero\src\commands\Utilities\Purge.js:26:118
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:1132) 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=s
trict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

Please help

Upvotes: 0

Views: 371

Answers (1)

Syntle
Syntle

Reputation: 5174

That's caused by the last part of your code, since discord.js v12 Message.delete() needs an object with the options timeout and reason which are optional, but in your case you want timeout to be 2000 like so:

// So we get our messages, and delete them. Simple enough, right?
const fetched = await message.channel.messages.fetch({limit: deleteCount});
message.channel.send(fetched.size + ` deleted messages by ${message.author.tag}`)
  .then(msg => {msg.delete({ timeout: 2000 })});
message.channel.bulkDelete(fetched)

Upvotes: 1

Related Questions