Find role id by name discord rewrite

brown wooden cross on green grass field under blue sky during daytime

Right now I’m working on a command for my discord bot where I create a role named after a user, and then automatically assign it to that user. The only problem I’m running into is trying to assign the role without knowing its id yet to the person mentioned.
My command so far:

@client.command(aliases = ["cmr", "CMR"])
@commands.has_permissions(manage_roles=True)
async def creatememberrole(ctx, *, member:discord.Member):
    author = ctx.message.author
    guild = ctx.guild
    print(author.display_name)
    await guild.create_role(name=str(member.display_name))
    await member.add_roles()
          #need help here ^

asked 3 hours ago
ZaydZayd

111 bronze badge

2 Answers

You could go trough the list of all the roles, and find the role you created and assign this role. Here’s an example:

for role in message.channel.guild.roles:
    if role.name == str(member.display_name):
        await member.add_roles(role)

answered 3 hours ago
behanep757behanep757

713 bronze badges

It can be done by utils.get easily, no need of for loop

role = discord.utils.get(ctx.guild.roles, name=member.display_name)
await member.add_roles(role

In your case there is no need of any for loop/utils.get, just assign the create_role() method to a variable and use that variable

@client.command(aliases = ["cmr", "CMR"])
@commands.has_permissions(manage_roles=True)
async def creatememberrole(ctx, *, member:discord.Member):
    author = ctx.message.author
    guild = ctx.guild
    print(author.display_name)
    role = await guild.create_role(name=str(member.display_name))
    await member.add_roles(role)

answered 16 mins ago
PoojanPoojan

4415 bronze badges

Latest posts