JavaScript ReferenceError: member is not defined - javascript

So I'm trying to code a utility bot on my server and for some reason, this error popped out "Reference error"
ReferenceError: member is not defined
Every single time I solve a problem another one pops out. I know you guys will be asking me to save it (member) but I already did, like 7 times?
This is my current code:
const { Discord } = require("discord.js");
exports.run = async(client, msg, args) => {
if(!msg.member.hasPermission('BAN_MEMBERS')) return msg.reply('You do not have permission to use this command!')
var user = msg.mentions.user.first() || msg.guild.members.cache.get(args[0]);
if(!user) return msg.reply('You did not mention a user for me to punish!')
var member;
try {
member = await msg.guild.members.fetch(user)
} catch(err) {
member = null;
}
if(member){
if(member.hasPermission('MANAGE_MESSAGES')) return msg.reply('You cannot ban a fellow staff member!');
}
var reason = args.splice(1).join(' ');
if(!reason) return msg.reply('Please make sure to specify a reason for me to punish this user!')
var channel = msg.guild.channels.cache.find(c => c.name === 'mod-logs');
var verify = msg.guild.emojis.cache.find(emoji => emoji.name === 'white_check_mark')
var log = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`${verify} ${user} has been kicked by ${msg.author} for "**${reason}**"`)
channel.send(logs);
var userLog = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`You have been banned from the server! Thats sadge. You can appeal the ban by message a staff member!`)
try {
await user.send(userLog);
} catch(err) {
console.warn(err);
}
msg.guild.members.ban(user);
var confir = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`${verify} ${user} has been banned by ${msg.author} for "**${reason}**"`)
msg.channel.send(confir);
msg.delete();
}
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
const { Discord } = require("discord.js");
exports.run = async(client, msg, args) => {
if(!msg.member.hasPermission('BAN_MEMBERS')) return msg.reply('You do not have permission to use this command!')
var user = msg.mentions.user.first() || msg.guild.members.cache.get(args[0]);
if(!user) return msg.reply('You did not mention a user for me to punish!')
var member;
try {
member = await msg.guild.members.fetch(user)
} catch(err) {
member = null;
}
if(member){
if(member.hasPermission('MANAGE_MESSAGES')) return msg.reply('You cannot ban a fellow staff member!');
}
var reason = args.splice(1).join(' ');
if(!reason) return msg.reply('Please make sure to specify a reason for me to punish this user!')
var channel = msg.guild.channels.cache.find(c => c.name === 'mod-logs');
var verify = msg.guild.emojis.cache.find(emoji => emoji.name === 'white_check_mark')
var log = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`${verify} ${user} has been kicked by ${msg.author} for "**${reason}**"`)
channel.send(logs);
var userLog = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`You have been banned from the server! Thats sadge. You can appeal the ban by message a staff member!`)
try {
await user.send(userLog);
} catch(err) {
console.warn(err);
}
msg.guild.members.ban(user);
var confir = new Discord.MessageEmbed()
.setColor('0xecd776')
.setDescription(`${verify} ${user} has been banned by ${msg.author} for "**${reason}**"`)
msg.channel.send(confir);
msg.delete();
}

You can try this code:
const CUser = message.mentions.users.first() || message.guild.members.cache.get(args[0]) || message.guild.member(message.author);
let member
if (message.mentions.members.first()) {
member = CUser
} else {
member = CUser.user
}
instead of your:
var user = msg.mentions.user.first() || msg.guild.members.cache.get(args[0]);
if(!user) return msg.reply('You did not mention a user for me to punish!')
var member;
try {
member = await msg.guild.members.fetch(user)
} catch(err) {
member = null;
}
Please note this code was written on discord.js version 12.5.3

Related

The bot crashes whe3n i use user id

const Discord = require('discord.js');
const config = require('../config.json');
const { Permissions } = require('discord.js');
module.exports.run = async (client, message, args) => {
if(!message.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS, true)) return message.channel.send('You can\'t use that!')
if(!message.guild.me.permissions.has(Permissions.FLAGS.BAN_MEMBERS, true)) return message.channel.send('I don\'t have the permissions.')
const member = message.mentions.members.first();
if(!args[0]) return message.channel.send('Please specify a user');
let reason = args.slice(1).join(" ");
if(!reason) reason = 'Unspecified';
message.guild.members.unban(`${member}`, `${reason}`)
.catch(err => {
if(err) return message.channel.send('Something went wrong')
})
let banembed = new Discord.MessageEmbed()
.setTitle('Member Unbanned')
.addField('User Unbanned', member)
.addField('Unbanned by', message.author)
.addField('Reason', reason)
.setFooter('Time Unbanned', client.user.displayAvatarURL())
.setTimestamp()
message.channel.send(banembed);
}
You're using a message.mentions.members.first() which is referring to a pinged person. To use their ID's you can use .fetch or cache.get
const member = await message.guild.members.fetch(args[0])
const member = await message.guild.members.cache.get(args[0])
also you can use them on the same line using or ||
const member = message.mentions.members.first() || await message.guild.members.fetch(args[0]) || await message.guild.members.cache.get(args[0])
I only don't know if its going to work with unban because the bot and the user should on the same server

Cannot ban with ID

I cannot ban a GuildMember using its ID, only with a mention. There are no errors in the console.
if (!message.member.hasPermission("BAN_MEMBERS")) {
return message.channel.send("Invalid Permissions");
}
let User = message.guild.member(message.mentions.users.first()) || message.guild.members.cache.get(args[0]);
if (!User) {
return message.channel.send("Invalid User");
}
if (User.hasPermission("BAN_MEMBERS")) {
return message.reply("Can't ban that one, he also can ban");
}
let banReason = args.join(" ").slice(22);
if (!banReason) {
banReason = "None";
}
console.log(`USER = ${User}`);
User.ban({ reason: banReason });
var UserID = User.id;
console.log(`USER ID = ${UserID}`);
You are missing cache, introduced in discord.js V12 - please look here
let userPending = message.guild.members.cache.fetch(message.mentions.members.first().id) || message.guild.members.cache.fetch(args[0]);
let user = await userPending;
This will ensure the bot searches for the correct guildMember

Discord.js checking roles not permissions

The CMD error:
C:\Users\<name>\Desktop\Discord Bots\AaryanBot\commands\kick.js:21
if(member.hasPermission('MANAGE_MESSAGES')) return message.reply('You cannot kick this person!');
^
TypeError: member.hasPermission is not a function
at Object.execute (C:\Users\<name>\Desktop\Discord Bots\AaryanBot\commands\kick.js:21:15)
And kick.js
var Discord = require('discord.js');
module.exports = {
name: 'kick',
description: 'Kick COmmand(ADMINS ONLY)',
execute(message, args) {
if(!message.member.hasPermission('KICK_MEMBERS')) return message.reply('You can\'t use that!');
var user = message.mentions.users.first();
if(!user) return message.reply('You didn\'t mention anyone!');
var member;
try {
member = message.guild.members.fetch(user);
} catch(err) {
member = null;
}
if(!member) return message.reply('They aren\'t in the server!');
if(member.hasPermission('MANAGE_MESSAGES')) return message.reply('You cannot kick this person!');
var reason = args.splice(1).join(' ');
if(!reason) return message.reply('You need to give a reason!');
var channel = message.guild.channels.cache.find(c => c.name === 'log');
var log = new Discord.MessageEmbed()
.setTitle('User Kicked')
.addField('User:', user, true)
.addField('By:', message.author, true)
.addField('Reason:', reason)
channel.send(log);
var embed = new Discord.MessageEmbed()
.setTitle('You were kicked!')
.setDescription(reason);
try {
user.send(embed);
} catch(err) {
console.warn(err);
}
member.kick(reason);
message.channel.send(`**${user}** has been kicked by **${message.author}**!`);
}
}

How to make bot send message to another channel after reaction | Discord.js

How do I make it so that when someone reacts with the first emoji in this command, the bot deletes the message and sends it to another channel?
Current Code:
const Discord = require("discord.js");
module.exports.run = async (bot, message, args) => {
if (!message.member.hasPermission("MANAGE_MESSAGES"))
return message.channel.send("You are not allowed to run this command.");
let botmessage = args.join(" ");
let pollchannel = bot.channels.cache.get("716348362219323443");
let avatar = message.author.avatarURL({ size: 2048 });
let helpembed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, avatar)
.setColor("#8c52ff")
.setDescription(botmessage);
pollchannel.send(helpembed).then(async msg => {
await msg.react("715383579059945512");
await msg.react("715383579059683349");
});
};
module.exports.help = {
name: "poll"
};
You can use awaitReactions, createReactionCollector or messageReactionAdd event, I think awaitReactions is the best option here since the other two are for more global purposes,
const emojis = ["715383579059945512", "715383579059683349"];
pollchannel.send(helpembed).then(async msg => {
await msg.react(emojis[0]);
await msg.react(emojis[1]);
//generic filter customize to your own wants
const filter = (reaction, user) => emojis.includes(reaction.emoji.id) && user.id === message.author.id;
const options = { errors: ["time"], time: 5000, max: 1 };
msg.awaitReactions(filter, options)
.then(collected => {
const first = collected.first();
if(emojis.indexOf(first.emoji.id) === 0) {
msg.delete();
// certainChannel = <TextChannel>
certainChannel.send(helpembed);
} else {
//case you wanted to do something if they reacted with the second one
}
})
.catch(err => {
//time up, no reactions
});
});

About the Quick.db module:

db module is possible to add writings. But is it possible to subtract the writings? It may also be caused by my incompetence this is my code:
const db = require('quick.db')
const Discord = require('discord.js')
exports.run = async (client, message, args, config) => {
if(message.author.id === 'My ID'){
let user = message.mentions.members.first() || message.author
if (args[0]) return message.channel.send(`${message.author}, bir eşya ismi girmen gerek!`)
db.subtract(`${user.id}.envan`, args[0])
let bal = await db.fetch(`${user.id}.envan`)
let embed = new Discord.RichEmbed()
.setAuthor(`Your item is received!`, message.author.displayAvatarURL)
.addField(`Received Item:`, `${args[0]}`)
.setColor("RED")
.setTimestamp()
message.channel.send(embed)
}else{
return message.channels.send('Yetkin yok knk').then(msg => {
setTimeout(() => {
msg.delete()
}, 2500);
})
}
}
You can use db.delete if you want to delete the reference instead of 'subtracting' (as it seems to be your goal in your question)
db.subtract(`${user.id}.envan`, Math.floor(args[0]))
try this.

Categories