Getting original userID of slash command via Button Interaction

Hey, so I'm using a button Handler. But everyone can interact with the button. How do I restrict the button to be usable by the user who ran the slash command from which the button originated? (I use the interactionCreate event to reply to the buttons, so I can only get the buttonInteraction). I don't wanna use db
20 Replies
souji
souji3y ago
you will have to associate the original author with the button, somehow if you are just looking for confirm buttons, i'd probably recommend using a component collector in the scope you have "original author" available and supplying it as a filter condition. if this is happening in another scope, you still need to somehow know who triggered the original response, either via in-memory map or by having the user id in the custom id and parsing it from there
Mac
Mac3y ago
You can always use ephemeral responses so only the user can see it or another option is what Souji stated You could cache the response with something like userID: messageID Basically associating the button to specifically that user via that message itself and then if someone clicks that button, you can check if that user is associated with that button and if not, handle it with what you prefer
Polar
PolarOP3y ago
I was looking for a solution for non-ephemeral responses how do I cache the response?
Mac
Mac3y ago
by cache I mean like store it in memory You can have a collection and add it to them and them remove it after x minutes etc
Polar
PolarOP3y ago
but doesn't collection clear on restart? I'm not sure how to store in memory..
Mac
Mac3y ago
yes it does then you need a db to store it
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Polar
PolarOP3y ago
I really wish I could use that, but since I use a button handler, I'll have to handle the buttons like
module.exports = {
customId: 'the-custom-id',
run: async (client, interaction) => {
...
}}
module.exports = {
customId: 'the-custom-id',
run: async (client, interaction) => {
...
}}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Polar
PolarOP3y ago
I mean yea, but how would I access interaction when it's defined after customId
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Polar
PolarOP3y ago
I didn't figure it.. I tried this didn't work
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Polar
PolarOP3y ago
nvm, I got it. Just had to change my command handler a bit
Mac
Mac3y ago
@polar.phoenix Why not just use a file storing db If you need to store these buttons for really long times then maybe not the best call but you can always use a JSON file storing method for it if you want
Jaworek
Jaworek3y ago
u want to get user which invoked interaction with button? so <buttonInteraction>.message.interaction.user.id
Polar
PolarOP3y ago
ah, finally. Thank you
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Jaworek
Jaworek3y ago
yeah
d.js docs
d.js docs3y ago
interface MessageInteraction Partial data of the interaction that a message is a reply to
Want results from more Discord servers?
Add your server