#How to get button label

1 messages · Page 1 of 1 (latest)

narrow gazelle
#

I'm making a button voting system. If the person has not yet voted, then +1 vote is added to the button.

Creating a button

class VotingManager(disnake.ui.View):
                    def __init__(self):
                        super().__init__(timeout=None)

                    @disnake.ui.button(label='0', style=disnake.ButtonStyle.gray, custom_id=f"voting-discord-like-{voting_discord_id}")
                    async def voting_like(self, button: disnake.ui.Button, interaction: disnake.MessageInteraction):
                        ...

Later call to the button

elif interaction.data.custom_id.startswith("voting-discord-like"):
                voting_id = interaction.data.custom_id.replace(
                    'voting-discord-like-', '')
                connection = await aiosqlite.connect(settings['DATABASE'])
                cursor = await connection.cursor()
                await cursor.execute(
                    "SELECT voting_status FROM voting WHERE votingID = ?", (voting_id,))
                voting_status = await cursor.fetchone()
                print(voting_status[0])
                if int(voting_status[0]) == 0:
                    await cursor.execute(
                        "UPDATE voting SET voting_status = ? WHERE votingID = ?", (True,voting_id,))
                    number = int(interaction.data.label)
                    interaction.data.label = str(number + 1)
                    await interaction.message.edit(view=self)
                    await connection.commit()
                else:
                    error = disnake.Embed(
                        description=f'You have already voted for this idea.  If you want to re-vote, then click on the reset button.', title='──⊰ ✜ ⊱ « :discord: Discord » ⊰ ✜ ⊱──', color=0xdfbe85)
                    await interaction.response.send_message(embed=error, ephemeral=True)
                await connection.commit()
                await connection.close()
#

in the button itself, everything obviously works, but I need to do this in on_button_click

coral mural
#

Are you using a callback and listening to the event?

coral mural
#

Why?

narrow gazelle
coral mural
#

So do you have a problem or not? 😅

narrow gazelle
#

I'm just tired

coral mural
#

What do you have inside your async def function for one of your buttons?
While I look up the docs

tawdry sentinelBOT
coral mural
#

You can access the button from the event/callback.
But note that this is a disnake.Button. Not a disnake.ui.Button. Which means you can't simply change its label and edit the message.

narrow gazelle
#

thanks

coral mural
#

Meaning if you want to update the button with a new label you need to construct a full set of new buttons. Or view in your case. And add the changes

narrow gazelle
#

May I ask 1 more question?

coral mural
#

Ofc

#

Since you already have the logic inside an event listener I would just rewrite your view to check it's id against your db in the init and set all the labels to however many votes are stored. That way you just need to make a new view, pass it the voting_discord_id and it should remake the view and pass it along

#

Altho you're sort of doing one half of each method now. Using views for its bundling feature, and events for its persistence. Ideally you should choose either or.

#

Instead of using a view you could make a function that returns all the buttons your need and you can pass them along instead of the view.

narrow gazelle
#

view = Button(label="1", custom_id="{voting_id}"

#

Understood. Thank you

#

I want to do more like this. I'll try to describe

#

I can sort through the data from the database, insert it into embed. And so that each time a new embed is created with scrolling through the pages.

Example: There are numbers from 1 to 10 in the database. Looping through, I display the number 1 in 1 embed, in 2 embed I display the number 2, and so on. Do all this through buttons with page scrolling from the documentation.

coral mural
#

Paginators are not hard to implement at all 😁 good luck