Discord.py role membercount - discord

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/

Related

I'm trying to give role to a member after inviting 2 members to the server or have 2 or more invite uses, I'm uncertain if I used the correct event

#client.event
async def on_ready(member):
if member.invites.uses == 2:
Premium = discord.utils.get(member.server.roles, name="Premium")
await client.add_roles(member, Premium)
I haven't had any errors and yes, intents are enabled
on_ready() will only execute on bot startup. What you want is probably on_member_join(member).
You can't get the invite by doing member.invite, as discord.Member objects does not provide it.
Member.add_roles() takes a list of roles, not a single role.
A solution to your problem could be to register each invite in a dict, then compare each of them when a member joins.

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 to remove roles from a user in every server? Discord.py

I am making a verification bot and I want to do a command where if somebody unlinks their Roblox account then it removes the verified role from them in all the servers they are in. I know how to do it in a single server like this:
role1 = discord.utils.get(ctx.guild.roles, name='Verified')
await ctx.author.remove_roles(role1)
But how would I do it across all of the servers the member is in with the bot. Thanks!!!
A role is unique per guild, you'd need to iterate through every guild, get the member and role, and remove it.
for guild in bot.guilds:
member = guild.get_member(member_id) # Change the ID accordingly
if member is not None:
role = discord.utils.get(guild.roles, name='Verified')
await member.remove_roles(role)
This code loops through every guild the bot is in, tried to get the member, if it's not a nonetype (cause the member doesn't have to share that specific guild with the bot), it gets the role obj and removes it.
Reference:
Bot.guilds
Guild.get_member
Also you need intents.guilds and intents.members

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

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;

Resources