'Button Interaction Fails after sometime | discord.py

I have created button roles using discord.py/pycord and they work pretty well but after some time the interaction gets failed it works well for some time but later it starts getting failed. How can it work forever and also surpass reboots

Code:

client.command()
async def genderrole(ctx: commands.Context):
    embed= discord.Embed(
        title= "Assign yourself Gender Roles by clicking on below given options", 
        description="<a:Arrow:944179619433676800> All the members are requested to click on below options in order to get gender roles", 
        color= discord.Colour.green()
        )


    button= Button(label= "Male", style= discord.ButtonStyle.green, emoji= "<a:male:945567841615241276>")
    button1= Button(label= "Female", style= discord.ButtonStyle.green, emoji= "<a:female:945567715530272778>")

    async def male(interaction: discord.Interaction):
        role = ctx.guild.get_role(945566721861906465)
        member = ctx.guild.get_member(interaction.user.id)
        if role in interaction.user.roles:
            await interaction.user.remove_roles(role)
            await interaction.response.send_message(f"{role} role has been taken from you", ephemeral=True)
        else:
            await member.add_roles(role)
            await interaction.response.send_message(f"{role} role has been given to you", ephemeral=True)
    button.callback=male

    async def female(interaction: discord.Interaction):
        role = ctx.guild.get_role(945566773548298252)
        member = ctx.guild.get_member(interaction.user.id)
        if role in interaction.user.roles:
            await interaction.user.remove_roles(role)
            await interaction.response.send_message(f"{role} role has been taken from you", ephemeral=True)
        else:
            await member.add_roles(role)
            await interaction.response.send_message(f"{role} role has been given to you", ephemeral=True)
    button1.callback=female

    view = View(button, button1)
    await ctx.send(embed=embed, view=view)

Help would be appreciated



Solution 1:[1]

you can use the on_interaction event ,this event has oe argument wich is an instance to a Interaction class so, you can use it as a you normaly do with it but there are an exception you should add a custom_id argument to these button so your code will look somthing like this

#the command part
client.command()
async def genderrole(ctx: commands.Context):
    embed= "do what you did before"
    #buttons right here
    button = Button(..., custom_id="any id you want")
    button1 = Button(..., custom_id="any id you want")
    #just a note don't make the ids the same
    view = View(button, button1)
    await ctx.send(embed=embed, view=view)
#the event part
@client.event
async def on_interaction(interaction):
    #put here avery function you did create before 
    ...
    #now the callback
    if interaction.custom_id == "the id you put for 'button'":
        await male(interaction): #just pass interaction as an argument
    elif interaction.custom_id == "the id you put for 'button1'":
        await female(interaction)

so this 'on_interaction' will trigger every time you someone click a button in the server that the bot on. Bonus information: you should add some functions or a way to dedect the specific server that the button is clicked on in you're case you do not need this

more information about the Interaction class check the officiel document https://docs.pycord.dev/en/master/api.html?highlight=interaction#discord.Interaction

if anything goes wrong please let me know

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Youssef Hmidi