#how to edit message when button is clicked

1 messages · Page 1 of 1 (latest)

median sandal
#

currently all this does is send a new message and keep editing the old one every 1 second, how would i make it so it edits the old message to the (response 1)/(response 2) and then keep it like that?

civic plover
#

?tag codeblock

rustic finchBOT
#

Please put your code in a code block:
```py
Here is your Code
```

That makes reading code in Discord a lot easier:

print("This is an example.")
median sandal
#

my bad

#

message is too big to send lmao

#

done

#
async def game(ctx):
    listofnumbers = ["1","2","3"]
    number = random.choice(listofnumbers)
    message = await ctx.send("**IT HAS ESCAPED**\n**YOU MUST CATCH IT!**")
    await asyncio.sleep(3)
    button = Button(style = discord.ButtonStyle.green, emoji = ":arrow_backward:", custom_id = "button1")
    button2 = Button(style = discord.ButtonStyle.green, emoji = ":arrow_up_small:", custom_id = "button2")
    button3 = Button(style = discord.ButtonStyle.green, emoji = ":arrow_forward:", custom_id = "button3")
    view = View()
    view.add_item(button)
    view.add_item(button2)
    view.add_item(button3)

    async def button_callback(interaction):
        if number != ("1"):
            await interaction.response.edit_message("response 1")
        else:
            await interaction.response.edit_message("response 2")
    
    async def button_callback2(interaction):
        if number != ("2"):
            await interaction.response.edit_message("response 1")
        else:
            await interaction.response.edit_message("response 2")

    async def button_callback3(interaction):
        if number != ("3"):
            await interaction.response.edit_message("response 1")
        else:
            await interaction.response.edit_message("response 2")

    button.callback = button_callback
    button2.callback = button_callback2
    button3.callback = button_callback3

    await message.edit(content= f"⠀⠀:watermelon:⠀⠀⠀⠀⠀:watermelon:⠀⠀⠀⠀⠀:watermelon:\n{number}", view=view)   

    while (((button_callback)) or ((button_callback2)) or ((button_callback3))) != True:
        await asyncio.sleep(1)
        listofnumbers = ["1","2","3"]
        number = random.choice(listofnumbers)
        await message.edit(content= f"⠀⠀:watermelon:⠀⠀⠀⠀⠀:watermelon:⠀⠀⠀⠀⠀:watermelon:\n{number}", view=view)
        if (((button_callback)) or ((button_callback2)) or ((button_callback3))) == True:
            break```
civic plover
#

What is the while loop for?

median sandal
#

while the button isnt pressed, edit the message every 1 second

civic plover
#

And how is a function ever going to be equal to True?

median sandal
#

if you pressed the button, i havent used it before and all the guides i read online are unclear

civic plover
#

That is not even close to how it works

median sandal
#

amazing

civic plover
#

Do you know basic Python?

median sandal
#

i learnt a decent amount of it before going onto tkinter and then discord

civic plover
#

Your solution is incredibly confusing, I'm not sure why that break is there either

#

Just have a variable that is True to begin with, run the while loop as long as that variable is true and set it to false on your button callbacks

median sandal
#

how can i make it so when the button is pressed it edits the message first send instead of sending a whole new one?

civic plover
#

The same way you are editing the message in your loop?

median sandal
#

yeah apart from it only edits it once

civic plover
#

Because your loop never runs, since your if statement is completely wrong

median sandal
#

the loop actually does kinda work, it does edit the message every 1 second, but it doesnt stop editing it after its meant to

civic plover
#

Yeah, because your check doesn't make any sense at all

#

This part

#

It's completely wrong

median sandal
#

so how would i get it to edit the message in await interaction.response.edit_message("response 1")

#

i forget to change it back

#

that line is broken

median sandal
#

alriught thanks man

#

appriciate the help

civic plover
#

I would also read rule 1 in #help-rules if I were you