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
Related
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
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
So, members who have ban permissions cannot ban, which is good. But those who have ban permissions can ban those with higher roles than them, which means a mod can ban an admin if he has ban permissions. I want it so that you can't ban members with higher roles than you, even if you have ban permissions.
const Discord = require('discord.js');
module.exports = {
name : 'kick',
description : 'Kick',
async execute(message, args) {
console.log("Works");
if (!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send("You do not have permissions to use this command");
const member = message.mentions.members.first();
let reason = args.slice(1).join(" ");
if (!reason) reason = "No specified reason.";
const kickEmbed = new Discord.MessageEmbed()
.setTitle(`${member.user.tag} was kicked from ${message.guild.name}`)
.setDescription(`Reason: ${reason}`)
.setColor("#FF0000")
.setTimestamp()
.setFooter(message.author.tag, message.author.displayAvatarURL())
.setAuthor(member.user.tag, member.user.displayAvatarURL({dynamic : true}))
.setThumbnail(member.guild.iconURL({ dynamic: true }))
if (!args[0]) return message.channel.send(`You need to specify the user that is going to be kicked`);
if (!member) return message.channel.send(`The member you mentioned is not in the server`);
try {
await member.send(kickEmbed);
}
catch(err){
console.log(`I was unable to message the member`);
}
try {
member.kick(reason);
await message.channel.send(kickEmbed);
}
catch(err){
console.log(err);
message.channel.send("I was unable to kick the member you mentioned");
}
}
}
You can use <message>.member.roles.position.highest to determine the position of a member role and apply an appropriate fix
const Discord = require('discord.js');
module.exports = {
name : 'kick',
description : 'Kick',
async execute(message, args) {
console.log("Works");
if (!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send("You do not have permissions to use this command");
const member = message.mentions.members.first();
let reason = args.slice(1).join(" ");
if (!reason) reason = "No specified reason.";
const kickEmbed = new Discord.MessageEmbed()
.setTitle(`${member.user.tag} was kicked from ${message.guild.name}`)
.setDescription(`Reason: ${reason}`)
.setColor("#FF0000")
.setTimestamp()
.setFooter(message.author.tag, message.author.displayAvatarURL())
.setAuthor(member.user.tag, member.user.displayAvatarURL({dynamic : true}))
.setThumbnail(member.guild.iconURL({ dynamic: true }))
if (!args[0]) return message.channel.send(`You need to specify the user that is going to be kicked`);
if (!member) return message.channel.send(`The member you mentioned is not in the server`);
// the fix for your issue
if (member.roles.highest.position >= message.member.roles.highest.position) return message.reply('Yikes the person you tried to kick has higher or equal roles than you!')
try {
await member.send(kickEmbed);
}
catch(err){
console.log(`I was unable to message the member`);
}
try {
member.kick(reason);
await message.channel.send(kickEmbed);
}
catch(err){
console.log(err);
message.channel.send("I was unable to kick the member you mentioned");
}
}
}
I'm trying to make discord.js ban command, make better like a role checking before the ban.
e.g. Administrator trying to ban another Administrator, but they're roles are same, this will give to bot signal and don't ban mentioned user, but the code just giving me always same message = Sorry, but the mentioned member have the higher role by position or you having the same role. (from russian)
if(target == message.author) {
return message.channel.send('You can\'t ban yourself!');
}
if(message.member.roles.highest.position <= member.roles.highest.position) {
return message.channel.send('Your role are same or the mentioned user is have higher role by position!');
}
if(target) {
const targetMember = message.guild.members.cache.get(target.id);
const duration = args.slice(1).join(' ')
const reason = args.slice(2).join(' ') || 'Причина не указана.';
targetMember.ban(
{ days: duration,
reason: `${reason}`}).catch(err => {
message.channel.send(`err ${err}`)
})
if(target == message.author) {
return message.channel.send('Вы не можете забанить себя!');
}
if(message.member.roles.highest.position <= member.roles.highest.position) {
return message.channel.send('Ваша роли либо одинаковы или роль пользователя выше!');
}
if(target) {
const targetMember = message.guild.members.cache.get(target.id);
const duration = args.slice(1).join(' ')
const reason = args.slice(2).join(' ') || 'Причина не указана.';
targetMember.ban(
{ days: duration,
reason: `${reason}`}).catch(err => {
message.channel.send(`err ${err}`)
})
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}**!`);
}
}