问题
I'm making my own Discord Bot because I don't trust the bigger ones (Dyno, Hime, NosoBot, etc.) And my bot is pretty much done. The only problem is that my code allows all members to use these commands. I only want people to be able to use the functions they have permissions to. The code works, but how can I make it allow only people with permission to kick/ban?
if (msg.content.startsWith("$kick ")) {
if (msg.mentions.members.first()) {
msg.mentions.members.first.kick().then((member) => {
msg.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
}).catch(() => {
msg.channel.send("I do not have permissions to do this");
});
}
}else if (msg.content.startsWith("$ban ")) {
if (msg.mentions.members.first()) {
msg.mentions.members.first.ban().then((member) => {
msg.channel.send(":wave: " + member.displayName + " has been successfully banned :point_right: ");
}).catch(() => {
msg.channel.send("I do not have permissions to do this");
});
}
}
回答1:
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
回答2:
Make a role in the server, and check for the role in your code
For example
if(!message.member.roles.find("name", "Admin"))
return;
Would exit the method if the user does not have the role
So the code would look something like this
if (message.content.startsWith("$kick")) {
if (!message.member.roles.find("name", "Admin"))
return;
// Easy way to get member object though mentions.
var member = message.mentions.members.first();
// 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");
});
}
});
client.on("message", (message) => {
if (message.content.startsWith("$ban")) {
if (!message.member.roles.find("name", "Admin"))
return;
// Easy way to get member object though mentions.
var member = message.mentions.members.first();
// ban
member.ban().then((member) => {
// Successmessage
message.channel.send(":wave: " + member.displayName + " has been successfully banned https://gfycat.com/playfulfittingcaribou :point_right: ");
}).catch(() => {
// Failmessage
message.channel.send("Access Denied");
});
}
});
A nicer approach would be making a function that returns whether the user has the permission or not and using it in your code
回答3:
if (message.content.startsWith(${prefix}kick
)) {
let member = message.mentions.members.first();
member.kick().then((member) => {
message.channel.send(`:wave: ${member.displayName} has been kicked`);
}).catch(() => {
if (!message.member.hasPermission(['KICK_MEMBERS', 'ADMINISTRATOR'])) {
message.reply("You cannot kick members");
} else if (member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS', 'ADMINISTRATOR'])) {
message.reply("You cannont kick this member");
}
})
}
if (message.content.startsWith(`${prefix}ban`)) {
let member = message.mentions.members.first();
member.ban().then((member) => {
message.channel.send(`:wave: ${member.displayName} has been kicked`);
}).catch(() => {
if (!message.member.hasPermission(['BAN_MEMBERS', 'ADMINISTRATOR'])) {
message.reply("You cannot ban members");
} else if (member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS', 'ADMINISTRATOR'])) {
message.reply("You cannont ban this member");
}
})
}
回答4:
I used this for the roles permission:
if(message.member.roles.find(r => r.name === 'name of role')){
//Enter command coding here
}else{
return message.channel.send('Enter Speech here')
}
break;
Maybe this might help you, i don't know.
回答5:
Hey i have something better for you
const Discord = require('discord.js');
if (message.content.startsWith(${prefix}BAN)) => {
if (!message.guild.member(message.author).hasPermission('BAN_MEMBERS')) { return message.channel.send('You do not have the permission for ban users" !'); }
if (!message.guild.member(client.user).hasPermission('BAN_MEMBERS')) { return message.channel.send('I don\'t have the permission for ban users" !'); }
if (message.mentions.users.size === 0) { return message.channel.send('You need to ping a user !'); }
let banMember = message.guild.member(message.mentions.users.first());
if (!banMember) { return message.channel.send('User not found!'); }
banMember.ban().then((member) => {
message.channel.send(member.displayName + " has left the server")
message.channel.send(member.displayName + " has been successfully banned by " + message.author);
})
}
Just delete BAN and replace by KICK
回答6:
if (message.content.startWith(`${prefix}ban`)){
let buser = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if (!buser) return message.channel.send("Please provid a user to ban")
let reason = args.join(" ").slice(22)
if (!reason) return message.channel.send("Please provide a reason")
if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("You don't have acces to this command")
if (buser.hasPermission("BAN_MEMBERS")) return message.channel.send("This user has the perm ban user")
let embed = new Discord.RichEmbed()
buser.ban()
.setAuthor("Ban")
.setDescription(`The user ${buser} has been ban for: ${reason}\n by the admin ${message.author.tag}`)
bot.channels.get("an channel id").send(embed);
}
来源:https://stackoverflow.com/questions/54892508/discord-js-ban-kick-commands-available-to-all-users-how-can-i-fix-this