#Command not removing role

1 messages · Page 1 of 1 (latest)

tired pivot
#
@bot.slash_command()
@discord.option("role", description="Role you would like to display.", type=discord.Role, required=True)
async def setrole(ctx, role: discord.Role):
    author_id = ctx.author.id
    author_roles = []
    author_record = session.query(UserRoles).filter_by(user_id=author_id).first()
    author_unlocked_roles = json.loads(author_record.unlocked_roles)
    role_id = role.id
    set_role = discord.utils.get(ctx.guild.roles, id=role_id)

    if not author_record:
        await ctx.respond("You do not have any unlocked roles", ephemeral=True)
        return
    else:
        if role_id not in author_unlocked_roles:
            await ctx.respond("You do not have this role unlocked.", ephemeral=True)
            return
        else:
            if set_role in ctx.author.roles:
                await ctx.respond("You already have this role selected", ephemeral=True)
                return
            else:
                emblem_role_present = False

                for role1 in ctx.author.roles:
                    if role1.icon:
                        await ctx.author.remove_roles(role1)
                        await ctx.author.add_roles(role)
                        emblem_role_present = True
                        break

                if emblem_role_present:
                    await ctx.respond(f"Successfully set display role to {role.name}.", ephemeral=True)
                else:
                    await ctx.author.add_roles(role)
                    await ctx.respond(f"Successfully set display role to {role.name}.", ephemeral=True)

I have a command which I would hope adds the role specified in the command and removes any roles with icons from the user
however after 2 uses of the command working fine, it seems to just add the role and not remove the role with the icon

#

Let's say I have 2 roles, one called moderator and one called admin, both have emblems

#

If i did

#

/setrole moderator

#

it works

#

/setrole admin

#

it works

#

But if i run /setrole moderator again

#

It doesn't remove the admin role, just adds the moderator role

neat sleet
#

btw elif exist if you are doing else: if

your nested if else is hard to read

try applying the principle of guard clause
ex.

author_id = ctx.author.id
author_roles = []
author_record = session.query(UserRoles).filter_by(user_id=author_id).first()
author_unlocked_roles = json.loads(author_record.unlocked_roles)
role_id = role.id
set_role = discord.utils.get(ctx.guild.roles, id=role_id)

if not author_record:
    return await ctx.respond("You do not have any unlocked roles", ephemeral=True)
if role_id not in author_unlocked_roles:
    return await ctx.respond("You do not have this role unlocked.", ephemeral=True)

emblem_role_present = False

for role1 in ctx.author.roles:
    if role1.icon:
        await ctx.author.remove_roles(role1)
        await ctx.author.add_roles(role)
        emblem_role_present = True
        break  

if emblem_role_present:
    return await ctx.respond(f"Successfully set display role to {role.name}.", ephemeral=True)     

await ctx.author.add_roles(role)
await ctx.respond(f"Successfully set display role to {role.name}.", ephemeral=True)

to remove a role, i think you should not rely on the icons that much since if i am correct, icons are dependent on the boost level
instead try making a list of roles you are planning to add icons to and do a check based on that list

tired pivot
#

It fetches all roles with icons just fine except one

#

This role for some reason doesn't have an icon according to role.icon

neat sleet
tired pivot
neat sleet
tired pivot
#

Im not in my computer rn

neat sleet
#

ohhh

tired pivot
#

It's not an issue with the command but the role itself

#

But im not 100% sure

#

Pycord is telling me this role doesn't have an icon

#

That's the only role that is red which has permissions

neat sleet
#

was that heart an image ?

tired pivot
tired pivot
neat sleet
#

also the bot's role should be placed higher than the role it is removing

#

i don't know of you need intents

#

but try wrapping that function in a try catch and then log the error and check the stack trace which part throws an error

tired pivot
#

It literally returns nothing

#

Im 90% sure the bot thinks that the role has no icon

neat sleet
tired pivot
#

printing server_roles just returns all of the roles with icons

#

except that one

neat sleet
#

try printing the role inside that for loop and see that it returns

tired pivot
#
    for server_role in server.roles:
        if server_role.icon:
            role_names = [r.name for r in server_roles]
            if server_role.name not in role_names:
                server_roles.append(server_role)
                print(server_role)
        else:
            continue
#

like this?

neat sleet
#

no just loop over every role

print the name
print the id
print the icon

#

by printing these 3 for each role you'll get am idea which role has icons during debugging

tired pivot
#

I figured out the issue

#

I think you were right earlier

#

Every other role has a select image

#

Though these roles

#

Use emojis

#

So pycord doesnt get them as icons

#

Strange

#

I just made a new role to test this and used an image and an emoji
when there was an image all was good
when there was an emoji, it didnt get it

neat sleet
#

yeah check it later if you can that might be the problem if it is not

try logging every role with the name id and icon

neat sleet
tired pivot
#

Is there a reason pycord does that?

#

Like if theres am emoji as the emblem the role doesnt technically have an icon

neat sleet
#

dunno 🤣 you could report it here that unicode as role icon is not being recognized

neat sleet
tired pivot
#

Ill do that

tired pivot
#

Its a bot for a decently sized server

#

where can i report this?

neat sleet
tired pivot
#

@ancient pivot

#

Unicode or emoji role icons make role.icon == False

#

Despite there technically being an icon which is just an emoji