Nick
Nick

Reputation: 105

Discord.js Ban command issues

I am trying to fix an error that I have gotten today, the error is this:

C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\commands\moderation\ban.js:15 let { user } = message.mentions.members.first() || message.guild.members.get(args[0]); ^

TypeError: Cannot destructure property 'user' of '(message.mentions.members.first(...) || message.guild.members.get(...))' as it is undefined. at Object.run (C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\commands\moderation\ban.js:15:11) at Client. (C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\index.js:36:34) at Client.emit (events.js:323:22) at MessageCreateHandler.handle (C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34) at WebSocketPacketManager.handle (C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:108:65) at WebSocketConnection.onPacket (C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:336:35) at WebSocketConnection.onMessage (C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:299:17) at WebSocket.onMessage (C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\node_modules\ws\lib\event-target.js:120:16) at WebSocket.emit (events.js:311:20) at Receiver.receiverOnMessage (C:\Users\nalfo\OneDrive\Desktop\Discord Bot\NetSync\node_modules\ws\lib\websocket.js:789:20)

It was working perfectly fine before, not sure what went wrong. Here's my module exports.

module.exports = {
    name: "ban",
    category: "moderation",
    description: "Bans the mentioned user.",
    usage: "<imputs>",
    run: (client, message, args) => {
    let { user } = message.mentions.members.first() || message.guild.members.get(args[0]);

    if(!message.member.hasPermission("BAN_MEMBERS")){
        message.channel.send(permission);
    } else {
        if(!user)
            return message.channel.send(novalidmember);
        if(!user.bannable) 
            return message.channel.send("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");

        let reason = args.slice(1).join(' ');
        if(!reason) reason = "No reason provided";

        user.ban({
            reason: `${reason}`
        })

        const embed = new Discord.RichEmbed()
         The embed is hidden.

        let logchannel = message.guild.channels.find(x => x.name === `logs`);


        if (!logchannel){
            Sends a message to the channel.
            Logs the ban in a file.
        } else {
            Sends a message to the channel.
            Logs ban in the log channel.
          }


    }
}
}

Some of the code I've hidden as it isn't important. But I am still not sure what is wrong with the whole command.

Upvotes: 0

Views: 1535

Answers (1)

megubyte
megubyte

Reputation: 1579

The error here is mentioning that on the line below you can not "destructure" the result of message.mentions.members.first() || message.guild.members.get(args[0]), because the result is undefined.

let { user } = message.mentions.members.first() || message.guild.members.get(args[0]);

Is it possible that the result of message.mentions.members.first() and message.guild.members.get(args[0]) is undefined? I do not see you checking for undefined before trying to get the user key from the object they return.

Here is what I would do instead of checking for a falsy value from user after assigning it: replace

if (!user)
    return message.channel.send(novalidmember);

with something further up in the file (before you assign user) along the lines of below:

if (message.mentions.members.first() == undefined || message.guild.members.get(args[0]) == undefined)
{
    return message.channel.send(novalidmember);
}

Upvotes: 1

Related Questions