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.
message
is undefined. Have you checked its value? – Zsolt Meszaros.execute()
function. – Zsolt Meszaros