Discord.js ban/kick commands available to all users. How can I fix this?
The "KICK_MEMBERS" permission tells you if they have the permission to kick members, hence the name.
The "BAN_MEMBERS" permission tells you if they have the permission to ban members, hence the name.
Your Kick Command:
if (msg.member.hasPermission("KICK_MEMBERS")) {
if (msg.members.mentions.first()) {
try {
msg.members.mentions.first().kick();
} catch {
msg.reply("I do not have permissions to kick " + msg.members.mentions.first());
}
} else {
msg.reply("You do not have permissions to kick " + msg.members.mentions.first());
}
}
Your Ban Command:
if (msg.member.hasPermission("BAN_MEMBERS")) {
if (msg.members.mentions.first()) {
try {
msg.members.mentions.first().ban();
} catch {
msg.reply("I do not have permissions to ban" + msg.members.mentions.first());
}
} else {
msg.reply("You do not have permissions to ban" + msg.members.mentions.first());
}
}
The reason for the try
and catch
ensures that if the bot does not have permissions to kick or ban that user, it will not cause an error.
Another note:
You do not have to create another bot.on('message')
event. Instead just use an elseif
Discord.js kick command permissions problem
You will need to import Permissions
from discord.js
. You can import it at the top of the page, like this:
const { Permissions } = require('discord.js');
Also, message.mentions
returns an object and it includes the mentioned members, channels, and roles. It won't have a kick()
method and if you try to call it, it will throw an error. As you don't log the error in your catch
block, you will have no idea what the error is; it will just send a reply saying "You cannot ban undefined".
You will need to check if there are mentioned members by checking message.mentions.members
. It returns a collection; you can grab the first member by using first()
.
You can kick the first mentioned member like this:
const { Permissions } = require('discord.js');
module.exports = new Command({
name: 'kick',
description: 'kick',
async run(message, args, client) {
if (!message.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS))
return message.reply('You have no permission to ban members');
if (!message.mentions.members)
return message.reply('You need to mention a member to ban');
let mentionedMember = message.mentions.members.first();
try {
mentionedMember.kick();
} catch (err) {
console.log(err);
message.reply(`Oops, there was an error banning ${mentionedMember}`);
}
},
});
Discord.js ban and kick commands not working properly
I was able to find an answer to the problem myself!
It took up 2 parts:
I replaced
mentions.users.first()
withmentions.members.first()
as suggested by MrMythical.Instead of using the
${tag}
template literal in the message that tells you which user has been banned, I used${targetMember}
which returned the correct member and pinged the right user!
Ban/kick command crashes on use
Your code crashed when you have no mention because you did not catch this use case.
Adding a simple catch, with a return for example, should work for you :
client.on("message", (message) => {
if (message.content.startsWith("$kick")) {
if(!message.member.roles.find("name", "MODS"))
return;
// Easy way to get member object though mentions.
var member= message.mentions.members.first();
// No mentions catch
if (member === undefined) return;
// Kick
member.kick().then((member) => {
// Successmessage
message.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
}).catch(() => {
// Failmessage
message.channel.send("Access Denied");
});
}
});
discord.js ban/kick commands
For the code you tried, it doesn't work as there is no members
property on the Message object. I think what you want to try is msg.mentions.members.first()
- this gives you the first valid mention as a GuildMember object. From here, you can use the kick()
and ban()
methods as you please.
Discord.js kick/ban permisions
I do not really understand what you mean, but I assume you want the command to work for certain members with the kick/ban permissions.
You could use .hasPermission()
, and an example of using it is the code below.
if (message.member.hasPermission("BAN_MEMBERS")) { // "KICK_MEMBERS" for kicking
//your code
} else {
message.reply('You do not have the permissions!')
}
If this does not answer your question then I am sorry.
Related Topics
How to Disable and Enable a Click Event on a Table Td
How to Format Numbers by Prepending 0 to Single-Digit Numbers
React Component Not Re-Rendering When State Changes
How to Make a Owl Carousel With Arrows Instead of Next Previous
Check If a String Is HTML or Not
Move an Image With JavaScript Using Mouse Events
Uncaught Typeerror: $(...).Modal Is Not a Function
How to Block +,-,E in Input Type Number
How to Detect iPad and iPad Os Version in iOS 13 and Up
Django Template JavaScript Passing a Python Variable to a JavaScript One
How to Redirect to Another Page Using Angularjs
How to Get the Index from a Json Object With Value
How to Count the Entire Number of Rows in a Datatable
Programmatically Clicking Button in React Native
How to Attach Multiple Images in a Embed