Button Interaction Pass Data
Hello im traying to make a coinflip game which is simple, but I need to pass some data to the button handler and I'm not sure how to do it, and I even need to send and embed to an other channel and interact with 3 differents button which modify the embeds in different ways, if someone can answer all of that I will be happy where some code if something is wrong let me know.
7 Replies
The data must be the following:
- balance
- choice
- userid (owner of the created confilp)
- opposite_userid (the member who joined the coinflip)
There will be differents games and I even need to handle that too, but I'm not sure if someone can help me out
you have 3 options:
- Use an in memory Map with unique keys that you store in the custom_id
- Use an external fast database like Redis with unique keys that you store in the custom_id
- Store the data entirely in the custom_id
and about the section section?
what do you mean?
and I even need to send and embed to an other channel and interact with 3 differents button which modify the embeds in different ways
The answer I gave is still the correct one. You'll need to store the data either in the custom_id, or some in memory storage (be that in nodejs memory or redis or a persistent database like sqlite, postgres, mysql, etc).
how you solve that exactly is up to you and how to you determine what data to pass in what way is also up to you.
there is no one correct way
For my bot Bloombot I pass an identifier as well as a database id throug the custom id in the format of
prefix-job|databaseid
then I extract the first part in my parse function (https://github.com/favware/bloombot/blob/main/src/interaction-handlers/buttons/jobs/melee/dragoon.ts) and the second part in the handler (https://github.com/favware/bloombot/blob/main/src/lib/util/functions/handleJobOrRoleButtonClick.ts#L11)
On the other hand, for @Dragonite I dump everything in the custom_id using a custom compression algorithm to ensure it is never above the 100 character limit that Discord permits
https://github.com/favware/dragonite/blob/main/src/commands/Pokemon/pokemon.ts#L107
https://github.com/favware/dragonite/blob/main/src/interaction-handlers/select-menus/pokemonSelectMenu.ts#L61instead I made a list, passed the id from the button and retrieved what I needed
it's working sooo