Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord.py Buttons (count)

So I am trying to make a suggestion which uses buttons instead of reactions to vote. Everything has worked so far except two things. Here is the code:

        count = 0
        count +=1
        await interaction.response.edit_message(embed=embed2)
        print(f"{count}") #check if count has gone up
    button.callback = button_callback

Everytime I click on the button, instead of showing a number it shows "<class ‘itertools.count’>", and everytime ‘count’ is 1.
Screenshot of embed

Here is the full code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@client.command()
async def suggest(ctx, *, reason):
    button = Button(label="Good Suggestion", style=discord.ButtonStyle.green,)
    button2 = Button(label="Bad Suggestion", style=discord.ButtonStyle.red,)
    async def button_callback(interaction):
        count = 0
        count +=1
        print(f"{count}")
        await interaction.response.edit_message(embed=embed2)
    button.callback = button_callback
   
#count 2 is the same code

 async def button_callback(interaction):
        count2 = 0
        count2 +=1
        print(f"{count2}")
        await interaction.response.edit_message(embed=embed2)
        
    button2.callback = button_callback
    
    embed = discord.Embed(color=ctx.message.author.color, timestamp=ctx.message.created_at)
    embed.set_author(name=f"{ctx.message.author}({ctx.message.author.id})", icon_url= ctx.message.author.avatar)
    embed.add_field(name='Suggestion:', value=f"{reason}")
    embed.set_footer(text=f"Likes: 0 | Dislikes: 0")

    embed2 = discord.Embed(color=ctx.message.author.color, timestamp=ctx.message.created_at)
    embed2.set_author(name=f"{ctx.message.author}({ctx.message.author.id})", icon_url= ctx.message.author.avatar)
    embed2.add_field(name='Suggestion:', value=f"{reason}")
    embed2.set_footer(text=f"Likes: {count} | Dislikes: {count2}")
    view = View()
    view.add_item(button)
    view.add_item(button2)
    await ctx.send(embed=embed,view=view)

I’m not sure what to do because I have done anything I know how to do. Could someone please help me and explain what to do?

>Solution :

# Globally
count1 = 0
    
async def suggest(ctx, *, reason):
    async def button_callback(interaction):
            global count1  # Let the scope know that we are using count1 from the global scope
            count1 += 1
            # ... everything else

alternatively we could opt to pass a referance of count into the function

async def suggest(ctx, *, reason, count)

or we could opt to grab the value from the embed and increment. This would avoid an issue where the server stops running for whatever reason and we lose track of the current count. I guess we could also write it to a file but this would get messy with multiple buttons/embeds etc..

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading