Home > Mobile >  Discord.py Buttons (count)
Discord.py Buttons (count)

Time:01-30

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:

@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?

CodePudding user response:

# 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..

CodePudding user response:

You can use TheLazyScripter’s approach, there’s an issue with this though, the count variable will get messed up as soon as the suggest command is invoked more than once, here’s a way to prevent that:

async def suggest(ctx, *, reason):
    count = 0
    async def button_callback(interaction):
        nonlocal count
        count  = 1
  •  Tags:  
  • Related