Make admins not kick each other - discord

Making a kick command.
I am trying to make a command so that the admins cannot kick each other.
if(message.author.hasPermission('KICK_MEMBER', 'ADMINISTRATOR') && message.member.hasPermission('KICK_MEMBER', 'ADMINISTRATOR'))
return ('You cant kick another admin!')
But I get an error.
TypeError: message.author.hasPermission is not a function

So basically you are trying to use message.author.hasPermission('permission'). The property author of the message object is a User, which is a Discord user. You need to reference the GuildMember, using message.member.hasPermission('permission').
Also, it looks like you are trying to compare the same person (message.author and message.member are the same person, only one is a User and the other is a GuildMember). You need to compare message.member.hasPermission() with otherMember.hasPermission().

if you don't want an admin to kick other admins then you should check like
if(user.member.hasPermission("KICK_MEMBERS")) { return message.channel.send("msg here"); }
assuming you have "user" defined if not then you can do
let user = message.mentions.users.first() || message.guild.members.cache.get(args[0]);
if you have that defined then you can check for the user using
if(!user) { return message.channel.send("member not found") }
and if you want to add a reason then you can do
let user = message.mentions.users.first() || message.guild.members.cache.get(args[0]);
let reason = args.slice(1).join(' ')
if(message.member.hasPermission("KICK_MEMBERS") { return message.channel.send("You do not have permission to kick members") }
if(!user) { return message.channel.send("member not found") }
if(!reason) { return reason = "No reason supplied" }
if(user.member.hasPermission("KICK_MEMBERS")) { return message.channel.send("You cannot kick that member") }
if(user.id = message.author.id) { return message.channel.send("you should not kick yourself")
message.mentions.users(user).kick()
message.mentions.users(user).send("You were kicked from " + message.guild + ", Reason: " + reason)
message.channel.send(`${message.author.username} Kicked ${user}#{user.discriminator}`)
this is assuming that you have "message" defined and "args" defined but this is a quick example of how you could stop admins from kicking each other if you use "Discord.JS V12" it will work perfectly

Related

Command is giving the bot role

I'm trying to make a command that gives a user a role. It was working before, but not it just gives the bot a role.
module.exports = {
name: "addevent",
description: "Add the event ping role to a user",
async run(client, message, args) {
const { member } = message;
if (message.member.roles.cache.has('793222158981529621')) {
const { member } = message;
message.channel.send(`${member} You already have this role.`)
}
if (message.channel.id !== '793224633759432755') return;
member.roles.add('793222158981529621')
message.channel.send(`${member} Added the event ping role.`)
}
}
This is also happening to my level command that's supposed to check a user's level. It checks the bot's level, not the user's.
let role = message.guild.roles.cache.find(r => r.name === "Role Name Here");
if (message.channel.id !== 'Channel ID Here') return;
if (message.member.roles.cache.has(role.id)) {
return message.channel.send(`${message.member} You already have this role.`)
}
message.member.roles.add(role);
message.channel.send(`${message.member} Added the event ping role.`);
My example has a variable called role which finds the role by its name, so you have to provide the name of the role there. Your if-statement with the message.channel.id was understandable and is a nice way to get the role in only one channel. The second if-statement just asks if a user already has that role (dont forget: in .has() you have to provide an ID, in this case it is easily done with role.id). Anyways if the user has that role it will send a message and return. Otherwise it will add the role to the user and also send a message.

Always getting back an, "Internal Server Error," when trying to run ban command on my Discord bot

I have tried many different ways of formatting the code, however, whenever I add code so that I must provide a reasoning to ban someone, I am always given an Internal Server Error. Here is my code.
module.exports.run = async (client, message, args) => {
const member = message.mentions.members.first();
const reason = args.slice(1).join(" ")
if (!message.member.hasPermission("BAN_MEMBERS")) {
return message.reply("you lack sufficiant permissions to execute this command.");
} else if (member.hasPermission("ADMINISTRATOR")) {
message.reply("you cannot ban this member.")
}
member.ban(reason).then((member) => {
message.channel.send(`${member} has been banned.`);
});
I use a command handler, and all my other commands work fine.
first step: Define the user
let user = message.mentions.members.first() || message.guild.members.cache.get(args.join(' '));
Second step: Create embed message or normal message
const userbanned = new Discord.MessageEmbed()
.setColor('#FF0000')
.setAuthor('User Banned')
.setDescription(`**${user.user.username}#${user.user.discriminator}** is now banned from this server`)
.setFooter(`bot_name`);
Third step: Send message
user.send(`You were banned from **${message.guild.name}** by ${message.author.username}#${message.author.discriminator}`)
return user
.ban()
.then(() => message.channel.send(userbanned))
.catch(error => message.reply("ERROR"));
Try changing
member.ban().then((member) =>//
to
member.ban({reason : args.slice(1).join(' ')}).then((member) =>//

Discord.js How can I edit previous bot's message?

so I am trying to make a ROSTER command. The command is $roster add/remove #user RANK. This command basically should edit a previous bot's message (roster) and add a user to the roster to the RANK in the command... This is my code so far, but I haven't managed to make the roster message and the editing part of it and the RANK system. If someone could help that would be very amazing!
//ROOSTER COMMAND
client.on('message', async message => {
if (message.content.startsWith(prefix + "roster")) {
if (!message.member.hasPermission('ADMINISTRATOR')) return message.channel.send('You do not have that permission! :x:').then(message.react(':x:'))
const args = message.content.slice(prefix.length + 7).split(/ +/)
let uReply = args[0];
const user = message.mentions.members.first()
if(!uReply) message.channel.send("Please use `add` or `remove`.")
if(uReply === 'add') {
if(!user) return message.channel.send("Please make sure to provide which user you would like to add...")
message.channel.send(`you are adding **${user.displayName}** from the roster.`)
} else if(uReply === 'remove') {
if(!user) return message.channel.send("Please make sure to provide which user you would like to add...")
message.channel.send(`you are removing **${user.displayName}** from the roster.`)
}
}})
Sounds like the .edit() method is what you want.
Example from the docs:
// Update the content of a message
message.edit('This is my new content!')
.then(msg => console.log(`Updated the content of a message to ${msg.content}`))
.catch(console.error);
To edit your bots previous message, you need to have a reference of the message you want to edit. The returned reference is a promise so do not forget to use await keyword. you can then use .edit() function on the reference to update you msg.
const msgRef = await msg.channel.send("Hello");
msgRef.edit("Bye");

TypeError: message.member.roles.some is not a function

if (message.content.startsWith(`${prefix2}red`)){
if (message.member.roles.some(role => role.name === 'Red')) return message.channel.send(`You already has that role `)
let role = message.guild.roles.find(r => r.name === "Red");
let member = message.member;
message.delete(1)
member.addRole(role).catch(console.error)
}
})
What do I need to change? for it to work?
the error is
if (message.member.roles.some(role => role.name === 'Red')) return message.channel.send(`You already has that role `)
TypeError: message.member.roles.some is not a function
I am assuming you are using discord.js v12 and that's why your code won't work.
Try using message.member.roles.cache.some(role => role.name === 'Red') instead of message.member.roles.some(role => role.name === 'Red')
It seems that message.member is undefined, you might want to check if this is done in a guild or not. If it is in a guild it will return the member property, while if it isn't, it won't. What you want to do is check if the message was sent from a guild or not, try the code below:
client.on("message", message => {
// `!` means `non-existent` or `is not`, and if the user sends the message from a guild
// this will not be triggered, since we know they are in, rather than not in, but, if
// it was sent outside of a guild, say a DM, then it will return the command, not trigerring
// any errors or such.
if (!message.guild) return;
// This will not allow this command to be triggered by the bot itself, since it may
// return a loop.
if (message.author === client.user) return;
// If the author of the message is a bot, then return, since bots can be used to spam
// and this will also spam your bot's API request. Webhooks work the same way.
// `||` means `or` if you didn't know.
if (message.author.bot || message.webhookID) return;
// Checks if the member has the role "ROLE NAME", and if they do, return.
if (message.member.roles.cache.some(role => role.name == "ROLE NAME")) return;
// code...
});

discord.js display a specific message when trying to kick myself

const member = message.mentions.members.first()
if (!message.mentions.users.size) {
return message.channel.send('You have to mention a user to kick him!');
} else if (member === message.author) {
return message.channel.send('You can\'t kick yourself!');
} else if (!member.kickable) {
return message.channel.send('You cannot kick this user!');
} else {
return member
.kick()
.then(() => message.message.send(`${member.user.tag} was kicked.`))
.catch(error => message.message.send(`Sorry, an error occured.`))
}
I'm trying to create a code that kicks a user, but in some situations I don't want the user to be able to kick another user. One of these situations is when the user to be kicked is the same one who writes the command. The problem is that i can't do this piece of code and every time it tells me: 'You cannot kick this user!' when it should say: 'You can't kick myself'. How can i display that message when i try to kick myself?
P.S. Sorry for my english, i'm italian
Replace } else if (member === message.author) { with } else if (member.id === message.author.id) {
That way it checks for the ID instead of the member / message author object, and the ID will definetly be identical.
Your if block is not triggering for two reasons. First of all, MessageMentions.members returns a collection of GuildMembers, while Message.author returns a User object. You can learn about the difference here.
Second of all, even if the two objects were the same type, Objects in JavaScript can fundamentally never be equal.
console.log({} === {}); // false
console.log({ property: true } == { property: true }); // false
console.log({ string: 'something' } === { string: 'something' }); // false
Luckily, you can use the Collection.prototype.equals method, which takes this into account. So if you wanted to, you could use this:
// const member = message.mentions.members.first()
if (member.equals(message.member))
However, there's another method that's easier, which is simply comparing the id property of both objects. The id is the same in both the User and GuildMember objects, and it is completely unique to someone's Discord profile.
// const member = message.mentions.members.first()
if (member.id === message.author.id)

Resources