Modal collecting two responses

I'm coding a bot with discord.js and I would like to prevent my bot from collecting two responses to a modal because we have no way of knowing if the user has canceled his first response and therefore if he completes a second response, it will satisfy the filter of the two interactions and the response will be processed twice. To solve this I had thought of adding the interaction token in the filter to collect the unique response linked to the interaction. How to add the token in the filter?
24 Replies
d.js toolkit
d.js toolkit6mo 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!
treble/luna
treble/luna6mo ago
just use the interaction id
Gykas
GykasOP6mo ago
so i have to compare the ids of which interactions? do you have an example? because I thought the id inherited from the slash command and therefore would be the same for both my submits
treble/luna
treble/luna6mo ago
no id will ever be the same Each time you use a slash command it will have a different id interaction id that is
Gykas
GykasOP6mo ago
yeah but in this case, it's the same command for both submits so i will have the same id ?
treble/luna
treble/luna6mo ago
no
Gykas
GykasOP6mo ago
okk mb so i compare the id of the interaction and the id of the parent interaction ?
treble/luna
treble/luna6mo ago
you store the interaction id in the customId of the modal
Gykas
GykasOP6mo ago
and
const filter = (int) => int.customId === interaction.id
const filter = (int) => int.customId === interaction.id
await i.channel
.awaitMessageComponent({
filter,
time: 30000,
})
.then(async (interaction) => {
const modalChannel = new ModalBuilder()
.setCustomId("channel_modal")
.setTitle("Modifier le salon de log");

const channelInput = new TextInputBuilder()
.setCustomId("channel_input")
.setLabel("Nouveau salon")
.setRequired(false)
.setStyle(TextInputStyle.Short);

const channelActionRow = new ActionRowBuilder().addComponents(
channelInput
);

modalChannel.addComponents(channelActionRow);

await interaction.showModal(modalChannel);

await interaction
.awaitModalSubmit({
filter,
time: 30000,
})
.then(async (int) => {
await int.deferUpdate().catch(() => {});
// ...
await i.channel
.awaitMessageComponent({
filter,
time: 30000,
})
.then(async (interaction) => {
const modalChannel = new ModalBuilder()
.setCustomId("channel_modal")
.setTitle("Modifier le salon de log");

const channelInput = new TextInputBuilder()
.setCustomId("channel_input")
.setLabel("Nouveau salon")
.setRequired(false)
.setStyle(TextInputStyle.Short);

const channelActionRow = new ActionRowBuilder().addComponents(
channelInput
);

modalChannel.addComponents(channelActionRow);

await interaction.showModal(modalChannel);

await interaction
.awaitModalSubmit({
filter,
time: 30000,
})
.then(async (int) => {
await int.deferUpdate().catch(() => {});
// ...
it's the second one the modal interaction so basically just .setCustomId(interaction.id)
treble/luna
treble/luna6mo ago
those arent slash commands
Gykas
GykasOP6mo ago
and then my filter is filter: (int) => int.customId === interaction.id
treble/luna
treble/luna6mo ago
Those are components
d.js docs
d.js docs6mo ago
If you are waiting for button or select menu input from a specific message, don't create the collector on the channel. - Channel collectors return component interactions for any component within that channel.
- <Channel>.createMessageComponentCollector(…)
+ <Message>.createMessageComponentCollector(…)
- <Channel>.createMessageComponentCollector(…)
+ <Message>.createMessageComponentCollector(…)
Gykas
GykasOP6mo ago
it's in a slash command and yeah its msg.awaitMessageComponent not i.channel
treble/luna
treble/luna6mo ago
it quite literally is i.channel
Gykas
GykasOP6mo ago
yes but i changed it nvm but otherwise the two other modifications that I made are correct?
await msg.awaitMessageComponent({ time: 30000 })
.then(async (interaction) => {
const modalChannel = new ModalBuilder()
.setCustomId(interaction.id)
.setTitle("Modifier le salon de log");

const channelInput = new TextInputBuilder()
.setCustomId("channel_input")
.setLabel("Nouveau salon")
.setRequired(false)
.setStyle(TextInputStyle.Short);

const channelActionRow = new ActionRowBuilder().addComponents(
channelInput
);

modalChannel.addComponents(channelActionRow);

await interaction.showModal(modalChannel);

await interaction
.awaitModalSubmit({
filter: (int) => int.id === interaction.id,
time: 30000,
})
.then(async (int) => {
await int.deferUpdate().catch(() => {});
// ...
await msg.awaitMessageComponent({ time: 30000 })
.then(async (interaction) => {
const modalChannel = new ModalBuilder()
.setCustomId(interaction.id)
.setTitle("Modifier le salon de log");

const channelInput = new TextInputBuilder()
.setCustomId("channel_input")
.setLabel("Nouveau salon")
.setRequired(false)
.setStyle(TextInputStyle.Short);

const channelActionRow = new ActionRowBuilder().addComponents(
channelInput
);

modalChannel.addComponents(channelActionRow);

await interaction.showModal(modalChannel);

await interaction
.awaitModalSubmit({
filter: (int) => int.id === interaction.id,
time: 30000,
})
.then(async (int) => {
await int.deferUpdate().catch(() => {});
// ...
here is the final code
treble/luna
treble/luna6mo ago
Compare the custom id and put the interaction id in the modals custom id
Gykas
GykasOP6mo ago
with the interaction.id, that's what im doing no ?
treble/luna
treble/luna6mo ago
no interaction.id isnt the same as interaction.customId
Gykas
GykasOP6mo ago
So its int.customId ? In my filtrer And that’s it?
treble/luna
treble/luna6mo ago
amd change the customid of your modal
Gykas
GykasOP6mo ago
Thats what I have done no ?
treble/luna
treble/luna6mo ago
ah yeah, i looked at the textinput one
Gykas
GykasOP6mo ago
Ok so we are good I will test it soon thx for ur help !
Want results from more Discord servers?
Add your server