How would I find how many members are in a specific role.
let memberCount = message.guild.roles.cache.find(r => r.name === "Lobby 1").members.size;
This only returns 0 or 1 depending on if the user has the role.
You can fetch the user list of the server and then loop through the array checking if one of the users has the role. If a user has the role, you can increment a global var.
Hope I could help
ShadowLp174
Related
I've been making Discord bots for a while, however I'm kinda stumbling upon this one element where as i want to fetch every user on the server that has the role "RoleA"
For each of the users having RoleA - I want to check if they then have any two or more of the role ids in the following array:
["232543645756","8989789789678","7567567567567","34534534534325"]
I do know you can loop the users roles to check if he has RoleID || RoleID
-- However as I want this to expandable I want to define all the roles in an Array, and then loop the user through the array to see if they have 2 or more role IDs found in the array tied to them.
Meaning if UserA has 232543645756 - but not any of the others in the array, it returns exact length === 1.
If user in this case has multiple role ids found in the array example:
232543645756 & 7567567567567 ... 345345345345 & 8989789789678 ... 345345345345 & 8989789789678 & 232543645756 etc (Multiple roles found in the array)
I want this to be fetchable by: If length >= 2 (or length > 1)
My question is now then
Is it possible to make a system like this? See if a user has multiple roles without having to define role1 && role2 || role1 && role3 || role1 && role4 etc - as this will be very messy if that array expands with more ids
Or is it possible to do something like If (roleArray.includes(member.roles.has...).length > 1) { Code here }
-- Or is this only possible by pushing all the users role IDs to an empty array, and then check if there is two or more matches out from the members.role.... id?
Use Collection#filter()
// roles to check
let roles = ["232543645756","8989789789678","7567567567567","34534534534325"]
// all members with RoleA
const members = guild.members.cache.filter(m => m.roles.cache.has("RoleA-id"))
// all members with RoleA and at least 2 other roles from the array
const validMembers = members.filter(m =>
roles.filter(r => m.roles.cache.has(r)).length >= 2
)
So, I am making a command that allows the users in my server to create aesthetic-only roles, and I already have a command to create them (they can only specify the colour and the name) and to add/remove them. However, I still want to add a feature, which searches all roles in the server if a role with the same name already exists when it is created. I found this code from another question to list all the roles in the server (and I honestly do not understand a bit what is happening in that line but it works):
rolelist = (", ".join([str(r.id) for r in ctx.guild.roles]))
This code unfortunately only returns the IDs of the roles and I can not find a way to convert the IDs into the names of the roles so that I can actually use it to check if the role created already exist. I tried adding ".name" to the end of "ctx.guild.roles" but that did not work and returned this error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'list' object has no attribute 'name'
To summarize, I need a way to put in a string of integers taken from the previously mentioned "rolelist" variable and turn them into the names of the roles. Any help is greatly appreciated and I thank you in advance.
You can use discord.utils.get() function. It returns None if an object with specified attributes not found.
See an example below:
#bot.command()
async def check_role(ctx, *, name: str):
await ctx.send("Role with specified name found." if discord.utils.get(ctx.guild.roles, name=name) is None else "Role with specified name not found.")
If you know role ID you can do something like this:
role_id = 12345
role = guild.get_role(role_id) # get the role
print(role.name) # print role name
Using the discord.py library, is it possible to check if the #everyone role has send_message permissions in a given channel within the server? My goal is to avoid 're-opening' a channel.
That is simple, just check the Permissions for the everyone role.
everyone_role = guild.roles[0] #ctx.guild in commands and message.guild in on_message
if everyone_role.permissions.send_messages:
#everyone can send messages generally
# checking if overwrites exist for the everyone role
everyone = [e for e in channel.changed_roles if role.position == 0]
if everyone:
if everyone.permissions.send_messages:
#everyone can send message in this channel
else:
#they can't
Note: everyone is the lowest role in the hierarchy order and has position 0 and index 0 in role lists.
References:
changed_roles
Permissions
guild.roles
I'm making a public member count for my server, which will be displayed in the name of a locked voice channel on top of the channel list.
Though, to make the count even more accurate, I would like not to include the bots into the member count. I assume the best way to do that would be to subtract the number of bots from the total number of members in the server.
The thing is, I don't know how to get the number of bots in a server (only the total number of members).
Thank you all in advance :D
guild.members returns the list of members of the guild,
member.bot which has the attribute bot is False for user accounts.
Notice you will need to turn on member intents for your bot for this to work:
#client.command()
async def bot_count(ctx):
members = ctx.author.guild.members
bot_count = 0
for i in members:
member = i.bot
if member == True:
bot_count += 1
await ctx.send(f"Server has {bot_count} bots!")
The idea is to accomplish the following when a new user is added:
Create a new group (OG)
Save it
Add user to saved group (OG)
Assign group role (OG)
I am using Rules with Organic Groups. All is fine apart from assigning the group role. I know you can add general system roles but does anyone know how to assign a group (OG) role programmaticaly so it happens automagically?
Any help much appreciated
I use the following custom php action on a rule, to add user to group and assign OG role.
OG role numbers appear to be in order of OG roles, as viewed through admin/config OG entries.
global $user;
// Load the user we want to add to the group
$account = user_load($user->uid);
// Add the user to the group - hard code group 18 which is my group as cant
// get PID from Ubercart order, to pull gid from nid. User, current user, active,
// etc., all default in 2nd array() param to og_group.
og_group(18);
// Changes the users role in the group (1 = non-member, 2 = member, 3 = administrator member, 4 = Forum Administrator)
og_role_grant(18, $account->uid, 2);
Note, OG role 4 (forum administrator) is a custom OG role I created. Also, 'member' (2) is the default, I believe, but I put this in so I'd remember how to allocate other OG roles if I needed to in future.
I'm not a php guru unfortunately, and I still havent worked out how to pull the pid from the node of the Ubercart product ordered so I can get its gid and hence not hard code the gid of 18.
Hope the above code snipped (og_role_grant mainly) works for you as a rule action custom php code snippet (remember not to include the php tags at top and bottom as rules does this for you).
If you have any thoughts on my problem of getting the gid of the ordered ubercart product, as above, please feel free to share. :)
Best wishes
There is a proposed Organic Group patch that add this functionality to the Organic Group Rules integration.
You can find the patch here: https://drupal.org/node/1327326
It adds Grant and Revoke role actions.
You can use og_role_grant($group_type, $gid, $uid, $rid) to assign a role to a user into an organic group programmatically.
To use this with rules, you can define a custom action using hook_rules_action_info() .