Passing data between interactions

Hi everyone, I'm looking for suggestions on how to handle passing data between interactions in discord.js. Currently, I use the customId for buttons, select menus, and similar components, as most do. However, the 100-character limit of customId is restrictive, and I need to pass more data than it allows. For example, one button fetches some data, and later, when another button is clicked, I need to reuse that same information. I'd like to avoid querying the database or an external API again. Has anyone encountered this situation or have any recommendations on best practices? Should I consider caching, storing data in memory, or is there another more suitable approach?
5 Replies
d.js toolkit
d.js toolkit3w ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
d.js docs
d.js docs3w ago
:class: InteractionCollector [email protected] Collects interactions. Will automatically stop if the message (messageDelete or messageDeleteBulk), channel (channelDelete), or guild (guildDelete) is deleted. Interaction collectors that do not specify time or idle may be prone to always running. Ensure your interaction collectors end via either of these options or manual cancellation.
Samtino
Samtino3w ago
Shoot wrong one. Not this
d.js docs
d.js docs3w ago
:method: BaseGuildTextChannel#createMessageComponentCollector() [email protected] Creates a component interaction collector.
// Create a button interaction collector
const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
const collector = channel.createMessageComponentCollector({ filter, time: 15_000 });
collector.on('collect', interaction => console.log(`Collected ${interaction.customId}`));
collector.on('end', collected => console.log(`Collected ${collected.size} items`));
// Create a button interaction collector
const filter = (interaction) => interaction.customId === 'button' && interaction.user.id === 'someId';
const collector = channel.createMessageComponentCollector({ filter, time: 15_000 });
collector.on('collect', interaction => console.log(`Collected ${interaction.customId}`));
collector.on('end', collected => console.log(`Collected ${collected.size} items`));
souji
souji2w ago
if you really want to cram data into the custom id there is https://www.npmjs.com/package/@sapphire/string-store if that doesn't suffice you can always use a unique id (for example the interaction id) to associate in-memory or data at rest (database) with it. the above (collectors) is less versatile but can make sense to keep state between steps where you expect an answer/action to be coming along shortly (collectors are just temporary event listeners with some exit condition that removes that instance of the collector after it is met)

Did you find this page helpful?