Comment only with a specific role and not dm - discord.js

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.

Related

Get the role(s) of a member in discord.py

I am trying to make a command that shows the roles of the mentions user.
This command is a test command which I am going to implement into my mute command. (the command will remove the member's current role and add the mute role)
This is what I have:
#client.command()
async def roles(ctx, member: discord.Member):
roles = member.roles
role_names = [role.name for role in roles]
await ctx.send(role_names)
The command works fine, but the output isn't quite what I expected.
Output:
['#everyone', 'Member']
It correctly displays the 2 roles mentioned by the user, but it isn't formatted in the specific way I want.
I want the output to simply be "Member" or whatever other roles the mentioned member has besides #everyone. basically, I want to remove the square brackets and "#everyone" from the output, leaving only the role name.
Hopefully, somebody can help me with this.
Thanks!
To fix your problem you just have to format the information from role_names in string form and remove the '#everyone'. You can use a list comprehension and the join() method .
Here would be the amended code:
#client.command()
async def roles(ctx, member: discord.Member):
roles = member.roles
role_names = ' '.join([role.name for role in roles if role.name != '#everyone'])
await ctx.send(role_names)

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!`));

How to check the role of a reacting member discord.js

I'm fairly new to discord.js and have recently ran into a problem with a section of my code.
if (reaction.message.member.roles.cache.some(role => role.name == "Goof Archivist 📚")) {
This section is supposed to check if a reacting member has the role "Goof Archivist 📚". But instead checks the role of the person that sent the message that is being reacted to. if that makes sense. any help would be appreciated.
You can use the second parameter of the messageReactionAdd event, the User that reacted, and the Guild.member() method.
Guild.member() can convert a global user object to a guildmember object in which you can see the roles of. To learn the difference between the two, check out What is the difference between a User and a GuildMember in discord.js?.
if (
reaction.message.guild
.member(user)
.roles.cache.some((role) => role.name == 'Goof Archivist 📚')
)

Check if bot has Admin?

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;

Discord.py role membercount

I'm looking for a way to let the discord API return the number of members of a role so I can count that and later add that to a nickname. does anyone have any idea how to do this? I've been looking all day and it looks like there is a way but the documentation doesn't refer to it.
You need to enumerate though the server members and roles, adding them to a list if they have the role:
for member in server.members:
for role in member.roles:
if role.name == "somename":
add_it_to_some_list
See: https://www.reddit.com/r/discordapp/comments/8yvq4g/get_all_users_with_a_role_using_discordpy/

Resources