Last active
June 10, 2024 14:08
-
-
Save nonchris/cab381064d364488cce46a1da1e223ac to your computer and use it in GitHub Desktop.
Discord.py: Get Role by Role Name, ID or mention
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import discord | |
""" | |
A function that returns the matching role-id(s) in a list (as integers) | |
Needed Arguments: Comamnd-Context and Role-ID/ Role-Mention/ Role-Name (as a string) | |
The function will return a tuple of values that contains two values: | |
- A list with all matching role-id (should be one most of the times) | |
- The length of that list as secondary return value | |
Notice: By passing a name into the function you might get more than one result, keep this in mind! | |
Exaple call: role, length = get_rid(ctx, role_name) | |
""" | |
def get_rid(ctx, role_input): | |
role_name = "" | |
for obj in role_input: | |
role_name += "%s " %obj | |
role_name = role_name.strip() | |
#first trying regex to get the id from the message itself (is the case by mention or role ID as intput) | |
role_id = re.search(r"\d{18}", role_name) | |
roles_list = [] #initializing return list | |
if role_id != None: #checking if re found something | |
role_id = role_id.group(0) #getting readable id | |
roles_list.append(int(role_id)) #getting and appending role-id to list | |
#return roles_list, len(roles_list) | |
#if role-name was given | |
else: | |
#iterating through roles, searching for name match (case sensitive) | |
for g_role in ctx.guild.roles: | |
if role_name in str(g_role.name): | |
roles_list.append(int(g_role.id)) #appending to list | |
return roles_list, len(roles_list) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This is one command - using the function from above (saved in a file called utils.py) - copy pasted from a cog. | |
The command returns the role id of the given role. | |
It handles name-inputs as well ad ID/mention inputs. | |
Pretty simillar to dynos members command. | |
""" | |
#ROLE ID | |
@commands.command(name="role-id", aliases=["roleid", "rid", "r-id"], help="Mention a role or just give its name to get the roles ID\n Aliases: `roleid`, `rid`, `r-id`") | |
@commands.has_permissions(kick_members=True) | |
async def roleid(self, ctx, *role_name: str): | |
try: | |
role, le = utils.get_rid(ctx, role_name) #getting IDs | |
if le == 1: #if only one ID returned | |
role = ctx.guild.get_role(role[0]) | |
await ctx.send(content=role.id) | |
else: #if more than one result - giving all possible roles | |
emby = discord.Embed(title="Multiple possible roles", color=discord.Color.blue()) | |
possible_roles = "" | |
for r in role: #iterating trough roles | |
print(r) | |
r = ctx.guild.get_role(r) | |
i = 0 | |
for members in r.members: #counting members | |
i += 1 | |
possible_roles += "%s with %s members - ID: %s\n" %(r.mention, i, r.id) | |
emby.add_field(name="Which one do you mean?", value=possible_roles) | |
emby.set_footer(text="To get an easy copy-pasteable ID mention the role you mean") | |
await ctx.send(embed=emby) | |
except: | |
emby = discord.Embed(title="", color=discord.Color.red()) | |
emby.add_field(name="Something went wrong", value="Please check your given argument.\n" | |
"Note that name inputs are handled case-sensitive and spaces in names might cause trouble.\n" | |
"Syntax: `%smembers <@role | role id | role name>`"%config.PREFIX) | |
emby.set_footer(text="If you did everything right and this error keeps occuring, please contact the bot owner %s" %config.OWNER_NAME) | |
await ctx.send(embed = emby) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
wow ty