Discord.js kick command permissions problem - javascript

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}`);
}
},
});

Related

Getting "Missing Access" error in console

I'm working on an add role command in discord.js v13 and this is the error I am getting:
Error
const { MessageEmbed } = require("discord.js");
const Discord = require("discord.js")
const config = require("../../botconfig/config.json");
const ee = require("../../botconfig/embed.json");
const settings = require("../../botconfig/settings.json");
module.exports = {
name: "addrole",
category: "Utility",
permissions: ["MANAGE_ROLES"],
aliases: ["stl"],
cooldown: 5,
usage: "addrole <user> <role>",
description: "Add a role to a member",
run: async (client, message, args, plusArgs, cmdUser, text, prefix) => {
/**
* #param {Message} message
*/
if (!message.member.permissions.has("MANAGE_ROLES")) return message.channel.send("<a:mark_rejected:975425274487398480> **You are not allowed to use this command. You need `Manage Roles` permission to use the command.**")
const target = message.mentions.members.first();
if (!target) return message.channel.send(`<a:mark_rejected:975425274487398480> No member specified`);
const role = message.mentions.roles.first();
if (!role) return message.channel.send(`<a:mark_rejected:975425274487398480> No role specified`);
await target.roles.add(role)
message.channel.send(`<a:mark_accept:975425276521644102> ${target.user.username} has obtined a role`).catch(err => {
message.channel.send(`<a:mark_rejected:975425274487398480> I do not have access to this role`)
})
}
}
What your error means is that your bot doesn't have permissions to give a role to a user. It might be trying to add a role which has a higher position than the bot's own role. One way to stop the error is to give the bot the highest position. The bot will also require the MANAGE_ROLES permission in the Discord Developers page to successfully add roles in the first place. If you want to learn more about role permissions, I suggest you go here => Roles and Permissions. Also, when you are using .catch() at the end, all it is checking for is if the message.channel.send() at the end worked and if not, then send a message to channel telling that the bot was not able to add the role. Instead you need to use .then() after adding the role and then use .catch() to catch the error. Then, your code might look like this:
target.roles.add(role).then(member => {
message.channel.send(`<a:mark_accept:975425276521644102> ${target.user.username} has obtined a role`)
}).catch(err => {
message.channel.send(`<a:mark_rejected:975425274487398480> I do not have access to this role`)
})

TypeError: message.author is not a function

For this piece of code, I aim for the author of the message that sent the ban command to be checked for whether they have the permissions necessary to ban the member, instead of instantly being allowed to, however I am unsure of what the const mod should be equivalent to to do so. Any help would be appreciated, thanks.
module.exports = {
name: 'ban',
description: "This command ban a member!",
execute(message, args) {
const target = message.mentions.users.first();
const mod = message.author();
if (mod.hasPermission('BAN_MEMBERS')) {
if (target) {
const memberTarget = message.guild.members.cache.get(target.id);
memberTarget.ban();
message.channel.send("User has been banned");
}
} else if (mod.hasPermission(!'BAN_MEMBERS')) {
message.channel.send(`You couldn't ban that member either because they don't exist or because you dont have the required roles!`);
}
}
}
As it says, Message.author is not a function. It is a property. You access it without the brackets
const mod = message.author
Another problem you may get is you can't get permissions. You will need Message.member for that
const mod = message.member

How to assign roles with a command - discord.js

I wanted to give my bot the functionality to assign roles with a command
For example, +mod #user would give #user the role of Mod.
Code in my main.js:
if(command == 'mod'){
client.commands.get('mod').execute(message, args);
}
Code in my mod.js file:
module.exports = {
name: 'mod',
description: "This command gives member the Mod role",
execute(message, args){
const member = message.mentions.users.first();
member.roles.add('role ID xxxx');
}
}
I get an error saying the member is empty. Am I doing something wrong?
I don't think it said member is empty. users don't have roles though, members have. So you will need to get the first mentioned member instead.
module.exports = {
name: 'mod',
description: 'This command gives member the Mod role',
async execute(message, args) {
const member = message.mentions.members.first();
if (!member) {
return message.reply('you need to mention someone');
}
try {
await member.roles.add('ROLE_ID');
message.reply(`${member} is now a mod 🎉`);
} catch (error) {
console.log(error);
message.reply('Oops, there was an error');
}
},
};

recently i build discord bot, the main problem is, i dont know how to set the permission, so every member of my server can kick and ban others

module.exports = {
name: 'ban',
description: "This command bans a member!",
execute(message, args){
const member = message.mentions.users.first();
if(member){
const memberTarget = message.guild.members.cache.get(member.id)
memberTarget.ban();
message.channel.send("User has been banned");
}else{
message.channel.send('you couldnt ban that member');
}
}
}
module.exports = {
name: 'kick',
description: "This command kicks a member!",
execute(message, args){
const member = message.mentions.users.first();
if(member){
const memberTarget = message.guild.members.cache.get(member.id)
memberTarget.kick();
message.channel.send("User has been kicked");
}else{
message.channel.send('you couldnt kick that member');
}
}
}
We could simply check for a user's permission using the .hasPermission() function of the GuildMember object. We could simply integrate it with a simple if statement, that would include the permissions you want to check for:
if (!message.member.hasPermission('BAN_MEMBERS') return; // Would return if the message author does not have permission to Ban Members
if (!message.member.hasPermission('KICK_MEMBERS') return; // Same thing for the Kick Members permission.

Discord.js v12 Ban Command

I made a ban command for my discord.js v12 bot. However whenever I run the command I get an error.
Here is my code:
const Discord = require('discord.js');
module.exports = {
name: "ban",
description: "Kicks a member from the server",
async run (client, message, args) {
if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send('You can\'t use that!')
if(!message.guild.me.hasPermission("BAN_MEMBERS")) return message.channel.send('I don\'t have the right permissions.')
const member = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
if(!args[0]) return message.channel.send('Please specify a user');
if(!member) return message.channel.send('Can\'t seem to find this user. Sorry \'bout that :/');
if(!member.bannable) return message.channel.send('This user can\'t be banned. It is either because they are a mod/admin, or their highest role is higher than mine');
if(member.id === message.author.id) return message.channel.send('Bruh, you can\'t ban yourself!');
let reason = args.slice(1).join(" ");
if(!reason) reason = 'Unspecified';
member.ban(`${reason}`).catch(err => {
message.channel.send('Something went wrong')
console.log(err)
})
const banembed = new Discord.MessageEmbed()
.setTitle('Member Banned')
.setThumbnail(member.user.displayAvatarURL())
.addField('User Banned', member)
.addField('Kicked by', message.author)
.addField('Reason', reason)
.setFooter('Time kicked', client.user.displayAvatarURL())
.setTimestamp()
message.channel.send(banembed);
}
}
This is the error I get whenever I run the command
DiscordAPIError: Invalid Form Body
DICT_TYPE_CONVERT: Only dictionaries may be used in a DictType
at RequestHandler.execute (/home/runner/SweatyBeautifulHelpfulWorker/node_modules/discord.js/src/rest/RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
method: 'put',
path: '/guilds/751424392420130907/bans/155149108183695360',
code: 50035,
httpStatus: 400
}
I could'nt understand how to correct the problem in the code. I'm a bit new to coding. Can you please help me out!. Thanks in advance
This is pretty easy to solve, all you have to to is pass the right amount of Parameters in the right way to the .ban function.
.ban({ days: 7, reason: 'your reason here' })
https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=ban
This looks like code from my video. It is a very simple error I made in the code. The .ban part should actually look like this:
.ban({ reason: 'your reason here' })
-thesportstacker

Categories

Resources