So the last question I asked, I tried to use the sample code in MrMythical answer. This time, I encountered another problem:
if (message.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS)){
^
ReferenceError: Permissions is not defined
The code:
module.exports = new Command({
name: "kick",
description: "kick",
async run(message, args, client) {
if (message.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS)) {
if (message.mentions.members) {
try {
message.mentions.kick();
} catch {
message.reply("I don't have permission to ban " + message.mentions.members.first());
}
} else {
message.reply("You cannot ban " + message.member.mentions.first());
}
}
}
});
Let's say I deleted the if (message.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS)), the message the returned when executing the command was I don't have permission to ban <insert_user_id>.
I'm kinda stuck here for a while, if you can help me, thank you so much.
Edit: I forgot to import the Permissions, so there's just "Don't have permissions to kick" now
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}`);
}
},
});