Check if bot has Admin? - discord.js

I want to make a small if statement to see if my bot has administrator privileges in the server.
[bot = new Discord.Client();]
Any help?
I tried:
if(!bot.guild.hasPermission("ADMINISTRATOR") return msg.author.send(":x: I need administartor priviliages in"+bot.guild.name+"! :x:")

bot.guild (client.guild) doesn't exist.
client.guilds is a collection of all the guilds your bot is in, mapped by their IDs.
You can check if a member / the bot has a permission using GuildMember's .hasPermission method.
if (!message.guild.members.get(client.user.id).hasPermission("ADMINISTRATOR")) return message.reply("I need Administrator permissions!")

Shortest way possible would be
if (!message.guild.me.hasPermission("ADMINISTRATOR")) return;

Related

Discord bot issuing roles based on member usernames

I'm creating a bot for the purpose of Discord moderation, one of the components of this bot is the following task:
"When every new user comes to server, his role by default will be #NotMember, but when he changes his server nickname to the "Nickname | (Real name)", for example, CoolPerson (Alex), then his role is automatically changing to the #Member".
The one way of doing this that I can see is to check if the username contains the brackets, if not then his role is old #NotMember.
Is there any another way to detect if server members have changed their name to a nickname? And is this actually possible?
I'm making this bot in JavaScript, but Python is also welcome here.
You can use the guildMemberUpdate event that is triggered whenever a member changes their name, with this you will have access to the guild member and their old/new username. Note that your bot will need to have the Server Members Intent
enabled on the developer portal.
Example in Discord.js:
client.on("guildMemberUpdate", function(oldMember, newMember) {
// oldMember is the old guild member object before the change, you
// can use it to fetch their old username.
// Add a role to the newMember object.
newMember.roles.add(YOUR_ROLE_HERE);
});

Comment only with a specific role and not dm

I want a comment to only work if the author has a specific role. How can I do that? Also, I don't want to write the comment in dm's what is still working. Thanks in advance!
Kind regards
I don't know what you mean by comment, but to check if a member has a role use this:
let role = message.guild.roles.cache.get("INSERT ROLE ID HERE");
if (!message.member.roles.cache.has(role.id)) return message.channel.send("You need the required role to use this!");
Also, just check if message.guild is null to see if the command is being used in a DM.

How do I check if anyone in a guild has a role? discord.js

I want to be able to check if anyone in the whole server has a specific role. Is this possible?
Role.members returns a collection of every cached member who has the given role. Note: you will need to update the required intents to use this correctly.
// get the role
const role = guild.roles.cache.get('role-id');
role.members.each((member) => console.log(`${member.username} has the role!`));

Trying to implement Patreon-only commands/functions on my discord bot, how would I accomplish this?

My discord bot gives the role of 'Patreon' to my patreon supporters. This role is given on my main discord bot server. So right now I'm trying to write some commands that would be only available to users who have the role 'Patreon' in the BOTS discord server, how can I accomplish this?
Like is there a way I can be like -
message.member.has('Patreon Role').in('My Discord Server)?
Let's go over the tasks you need to accomplish this.
Get the "home guild" with your users and corresponding Patreon role.
See Client.guilds and Map.get().
Find the user in the guild.
See Guild.member().
Check whether or not the user has the Patreon role.
See GuildMember.roles and Collection.find().
You can define a function to help you out with this, export it and require it where you need it (or define it within relevant scope), and then call it to check if a user is one of your Patreon supporters.
Here's what this function would look like...
// Assuming 'client' is the instance of your Discord Client.
function isSupporter(user) {
const homeGuild = client.guilds.get('idHere');
if (!homeGuild) return console.error('Couldn\'t find the bots guild!');
const member = homeGuild.member(user);
if (!member) return false;
const role = member.roles.find(role => role.name === 'Patreon');
if (!role) return false;
return true;
}
Then, as an example, using this function in a command...
// Assuming 'message' is a Message.
if (!isSupporter(message.author)) {
return message.channel.send(':x: This command is restricted to Patreon supporters.')
.catch(console.error);
}
message.member.roles.find('name', 'Patreon Role');//this returns either undefined or a role
What that does is it searches the users collection to see if the have "Patreon Role"
If the message is on the same server, otherwise you could do
client.guild.find('name','My Discord Server').member(message.author).roles.find('name', 'Patreon Role'); //this also returns undefined or a role
Clearly that second option is long, but what is basically does is searches the servers the bot is in for a server called 'My Discord Server' then it finds the GuildMember form of the message.author user resolvable, then it searches their roles for the role 'Patreon Role'
There is a chance it will crash though if they aren't on the server(the documentation doesn't say if it returns and error or undefined for some reason) so if it does crash you could instead do
client.guild.find('name','My Discord Server').members.find('id', message.author.id).roles.find('name', 'Patreon Role'); //this also returns undefined or a role
You can read more here: https://discord.js.org/#/docs/main/stable/class/User
and here
https://discord.js.org/#/docs/main/stable/class/Client
and here
https://discord.js.org/#/docs/main/stable/class/Guild
To try and give a full example, assuming this is in your message event
if (message.member.roles.find(r => r.name === 'Patreon') == undefined &&
commandIsExclusive || message.guild.id !== 'this-id-for-BOTS-server') {
// Don't allow them in here
}
Essentially, to run a command they must be a supporter, in a specific server and if it is exclusive and the other criteria aren't met, they are denied

Changing nickname in discord.js

So I've looked at a fair few forum posts and can't find something fitting my code.
I'm trying to do something like this...
case "setnick":
if (args[1]) {
if (message.guild.me.hasPermission("MANAGE_NICKNAMES")) {
if (message.member.hasPermission("CHANGE_NICKNAME"))
message.member.setNickname(args[1])
else message.channel.send("You do not have the permissions! **(CHANGE_NICKNAME)**")
}
else message.channel.send("The bot does not have the permissions! **(MANAGE_NICKNAMES)**")
}
else message.channel.send("There's no argument to set your nickname too! **Eg. f!setnick NICKNAME**")
break;
So it checks if there is a argument like
f!setnick NICKNAME
Then checks if the bot has permission MANAGE_NICKNAMES
if it doesn't it sends a chat message.
And it then checks for if the user has the permission CHANGE_NICKNAME
so i'm wondering why its not working.
Any reason why?
Have you tried checking if it works on other users besides yourself? Bots can not do administrator commands (like change someone's nickname) if you are higher/the same in the hierarchy than it, and seeing the owner is the ultimate power it is probably returning a permission error.
Try catching it and see what the error is
message.member.setNickname(args[1]).catch(e=>console.log(e))
if it returns DiscordAPIError: Privilege is too low... then my theory is correct. It should work for lower users.
message.member.me doesn't get the bot's user, it gets the user sending the message. Try message.guild.members.find("id", client.user.id).hasPermission("MANAGE_NICKNAMES")

Resources