0
votes

I got a unban command code, and I get the following error in the console:

(node:9348) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'member' of undefined at Object.execute (C:\Users\19nik\Documents\GitHub\bot-project\commands\unban.js:9:22) at Client. (C:\Users\19nik\Documents\GitHub\bot-project\gb.js:81:17) at Client.emit (events.js:315:20) at MessageCreateAction.handle (C:\Users\19nik\Documents\GitHub\bot-project\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14) at Object.module.exports [as MESSAGE_CREATE] (C:\Users\19nik\Documents\GitHub\bot-project\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32) at WebSocketManager.handlePacket (C:\Users\19nik\Documents\GitHub\bot-project\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31) at WebSocketShard.onPacket (C:\Users\19nik\Documents\GitHub\bot-project\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) at WebSocketShard.onMessage (C:\Users\19nik\Documents\GitHub\bot-project\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) at WebSocket.onMessage (C:\Users\19nik\Documents\GitHub\bot-project\node_modules\ws\lib\event-target.js:132:16) at WebSocket.emit (events.js:315:20)

This is my code:

const { MessageEmbed } = require('discord.js');
const fs = require("fs");

module.exports = {
    name: `unban`,
    description: `Unbans given user ID or mentioned user.`,
    async execute(bot, args, message) {

        if (!message.member.hasPermission(["BAN_MEMBERS"])) return message.channel.send("You do not have the required permissions to use the unban command.")

        if (!args[0]) return message.channel.send("Provide me a valid USER ID.");
        //This if() checks if we typed anything after "!unban"

        let bannedMember;
        //This try...catch solves the problem with the await
        try {
            bannedMember = await bot.users.cache.fetch(args[0])
        } catch (e) {
            if (!bannedMember) return message.channel.send("That's not a valid USER ID.")
        }

        //Check if the user is not banned
        try {
            await message.guild.fetchBan(args[0])
        } catch (e) {
            message.channel.send('This user is not banned.');
            return;
        }

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

        if (!message.guild.me.hasPermission(["BAN_MEMBERS"])) return message.channel.send("I am missing permissions to unban.")
        message.delete()
        try {
            message.guild.members.unban(bannedMember, { reason: reason })
            message.channel.send(`${bannedMember.tag} has been unbanned.`)
            console.log(`AUDIT LOG: [UNBAN] ${message.author.tag} unbanned ${member.user.tag} from ${message.guild.name}.`);
            var readmessagefile = fs.readFileSync('./logging/UnbanLog.txt', 'utf-8');
            var writemessagefile = fs.writeFileSync('./logging/UnbanLog.txt', 'Type: [UNBAN] ' + 'Time ' + '(' + message.createdAt + ')' + ' | ' + member.user.tag + ' from ' + message.guild.name + ' | Moderator ' + message.author.tag + '\n' + readmessagefile)
            console.log('BOT LOG: [INTERNAL] Writing to unban log file.');
        } catch (e) {
            console.log(e.message)
        }
    }
}

I don't know what to do. It also pops up a error of cannot read property 'hasPermissions' of undefined too.

4
It seems message is undefined. Have you checked its value?Zsolt Meszaros
i have put message on the async execute(). do i have to put it somewhere elsenikoszz
You should edit your post and add how you call the .execute() function.Zsolt Meszaros
i indeed do have included the async execute() included. But, no worries. Someone gave me a fix and got a different problem...nikoszz

4 Answers

0
votes

I believe the main source of your problem is your callback, having called your client first before message and args. Since JS is sensitive for callback parameter placement, you should instead write your callback as the following:

async execute(message, args, bot)
0
votes

There error seems to be at line 9

if (!message.member.hasPermission(["BAN_MEMBERS"])) return message.channel.send("You do not have the required permissions to use the unban command.")

Which doesn't have an error, maybe the command was ran in DMS? Maybe use:

if(!message.guild) return;

Which should solve the problem.

0
votes

Two possible reasons :

Callback

make sure the parameters of execute() are in the exact order as in your main file

Command is used outside a guild

The error might have happened because the command was using outside a guild to fix this add this to your code below execute()

if (!message.guild) return;
-1
votes

I think this will work.

const { MessageEmbed } = require('discord.js');
const fs = require("fs");

module.exports = {
    name: `unban`,
    description: `Unbans given user ID or mentioned user.`,
    async execute(bot, args, message) {
        if(!message.guild) return message.channel.send("This Command Can't Be Executed In DMs.");
        if (!message.author.hasPermission(["BAN_MEMBERS"])) return message.channel.send("You do not have the required permissions to use the unban command.")

        if (!args[0]) return message.channel.send("Provide me a valid USER ID.");
        //This if() checks if we typed anything after "!unban"

        let bannedMember;
        //This try...catch solves the problem with the await
        try {
            bannedMember = await bot.users.cache.fetch(args[0])
        } catch (e) {
            if (!bannedMember) return message.channel.send("That's not a valid USER ID.")
        }

        //Check if the user is not banned
        try {
            await message.guild.fetchBan(args[0])
        } catch (e) {
            message.channel.send('This user is not banned.');
            return;
        }

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

        if (!message.guild.me.hasPermission(["BAN_MEMBERS"])) return message.channel.send("I am missing permissions to unban.")
        message.delete()
        try {
            message.guild.members.unban(bannedMember, { reason: reason })
            message.channel.send(`${bannedMember.tag} has been unbanned.`)
        } catch (e) {
            console.log(e.message)
        }
    }
}

I removed the console logging section cause it might start occurring some errors.

It might be wrong cause i am newbie