#Remove role with button interaction

1 messages · Page 1 of 1 (latest)

hearty kindle
#

I really hate to ask for help, but I've tried searching for answers in here and on Google and couldn't find anything. So I'm making a button that gives you a role when you click it, and it does, but I also want it to remove the role if you already have the role (so basically have the bot check if that user has the role and remove it if they do when they interact with the button). I’ve tried interaction.used.remove_roles but didn’t really know what to do to check if they have the role. So if anyone can help, I would appreciate it very much.

@discord.ui.button(custom_id="male", style=discord.ButtonStyle.gray, emoji='![Male](https://cdn.discordapp.com/emojis/1028394140175716502.webp?size=128 "Male")')
    async def male(self, button, interaction):
        if interaction.channel.id == 1027780758699511908:
            guild = bot.get_guild(1027417323331649556)
            Male = guild.get_role(1028387057829163141)
            await interaction.user.add_roles(Male)

           #remove role part

            await interaction.user.remove_roles(Male)
            embed = discord.Embed(description="You have been given the **Male** role.", color=0x2f3136)
            await interaction.response.send_message(embed=embed, ephemeral=True)
hearty kindle
#

This ended up working for me,

if role:
    buttons = [self.children[0], self.children[1]]  # remove first 2 buttons
    for button in buttons:
        self.remove_item(button)
#

Wait removing the button or the role thonk

#

the role

#

the role that's added onto the user when they click the button the first time

#

Im pretty sure you can check with

if Male in interaction.user.roles:
  ...
#

I have, but maybe I just put it in the wrong place

#

should I put it before or after this part

#

Before

#

Could also wrap the whole thing as an if-else statement

#

and now to set a response, should I also put it before that part, so like this
nvm, now it doesn't respond, just gives an error

#

Yeah

#

WHAT

#

no error in the terminal, just on Discord

#

Oh

#

Wait show whole code

#

also, if I have the last await interaction.response part under the elif statement, it just gives me the "This interaction failed" error, but if I move it up above the elif statement, it replies with this only this

hearty kindle
# hearty kindle Wait show whole code
class GenderButtons(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)

    @discord.ui.button(custom_id="male", style=discord.ButtonStyle.gray, emoji='![Male](https://cdn.discordapp.com/emojis/1028394140175716502.webp?size=128 "Male")')
    async def male(self, button, interaction):
        if interaction.channel.id == 1027780758699511908:
            guild = bot.get_guild(1027417323331649556)
            Male = guild.get_role(1028387057829163141)
            await interaction.user.add_roles(Male)
        elif Male in interaction.user.roles:
            await interaction.user.remove_roles(Male)
            await interaction.response.send_message('temp')
            embed = discord.Embed(description="You have been given the **Male** role.", color=0x2f3136)
            await interaction.response.send_message(embed=embed, ephemeral=True)
#

I'm also assuming that button is intended to only work in whatever channel 1027780758699511908 is?

#

yes

#

Oh, elif is kinda being used wrong
Basically since the first condition has been met (being that the button was pressed in that channel) the elif part was never executed
The error comes from the lack of any response, since the embed is in the 2nd condition

#

Anyways, im 90% sure this would work

    @discord.ui.button(custom_id="male", style=discord.ButtonStyle.gray, emoji=':Male:')
    async def male(self, button, interaction):
        if interaction.channel.id == 1027780758699511908:
            guild = bot.get_guild(1027417323331649556)
            Male = guild.get_role(1028387057829163141)
            if Male in interaction.user.roles:
                embed = discord.Embed(description="The **Male** role has been removed.", color=0x2f3136)
                await interaction.user.remove_roles(Male)
            else:
                embed = discord.Embed(description="You have been given the **Male** role.", color=0x2f3136)
                await interaction.user.add_roles(Male)
            await interaction.response.send_message(embed=embed, ephemeral=True)
hearty kindle