Home > other >  Multiple Button not showing up in nextcord
Multiple Button not showing up in nextcord

Time:10-30

I'm currently coding a quizz bot and I came up with this code:

class answers(nextcord.ui.View):
    def __init__(self, ans1, ans2, ans3):
        super().__init__(timeout=None)
        self.ans1 = ans1
        self.ans2 = ans2
        self.ans3 = ans3
        self.value = False

    @nextcord.ui.button(label = '1', custom_id="1", style = nextcord.ButtonStyle.red)
    async def reaction(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        await interaction.response.send_message('ans1')
        self.value = True
        self.stop()
    @nextcord.ui.button(label='2', custom_id='2' )
    async def reaction(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        await interaction.response.send_message('ans2')
        self.value = True
        self.stop()
    @nextcord.ui.button(label='3', custom_id='3')
    async def reaction(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
        await interaction.response.send_message('ans3')
        self.value = True
        self.stop()

This is the class that should display 3 buttons. However it displays only the third one. I do not get any errors.

How can I write this code so it does display all three?

CodePudding user response:

You'll need to use different method names for each of the buttons. Changing the names to reaction1, reaction2, reaction3 would fix your issue.

The reason is the ui.button decorator doesn't store the button info elsewhere, instead it attaches the relevant info onto the method and returns the method back. So having the same name for the three method, you're actually reassigning answers.reaction every time you define a button, and only the last one gets kept. discord.py only evaluates all of the components from a view at runtime, so the last button is all it sees—thus it only displays the last button.

  • Related