Problems with modal

setupPaginationCollector(message, pages, mode, authorId) {
const collector = message.createMessageComponentCollector({ time: 300000 });
let currentPage = 0;
let quantity = 1;

collector.on('collect', async (interaction) => {
if (interaction.user.id !== authorId) {
return interaction.reply({ content: "You can't use these controls.", ephemeral: true });
}

if (interaction.isButton()) {
if (interaction.customId === 'previous') {
currentPage = (currentPage - 1 + pages.length) % pages.length;
await interaction.update(this.createShopEmbed(pages[currentPage], currentPage, pages.length, mode));
} else if (interaction.customId === 'next') {
currentPage = (currentPage + 1) % pages.length;
await interaction.update(this.createShopEmbed(pages[currentPage], currentPage, pages.length, mode));
} else if (interaction.customId === 'quantity') {
const modal = new ModalBuilder()
.setCustomId('quantityModal')
.setTitle('Set Quantity');

const quantityInput = new TextInputBuilder()
.setCustomId('quantityInput')
.setLabel('Enter quantity')
.setStyle(TextInputStyle.Short)
.setRequired(true)
.setValue(quantity.toString());

const actionRow = new ActionRowBuilder().addComponents(quantityInput);
modal.addComponents(actionRow);

await interaction.showModal(modal);
}
} else if (interaction.isStringSelectMenu()) {
if (interaction.customId === 'buy') {
await this.handlePurchase(interaction, authorId, quantity);
} else if (interaction.customId === 'sell') {
await this.handleSell(interaction, authorId, quantity);
}
} else if (interaction.isModalSubmit() && interaction.customId === 'quantityModal') {
const newQuantity = parseInt(interaction.fields.getTextInputValue('quantityInput'));
console.log(newQuantity)
if (!isNaN(newQuantity) && newQuantity > 0) {
quantity = newQuantity;
await interaction.reply({ content: `Quantity set to ${quantity}`, ephemeral: true });
} else {
await interaction.reply({ content: 'Invalid quantity. Please enter a positive number.', ephemeral: true });
}
}
});

collector.on('end', () => {
message.edit({ components: [] });
});
}
setupPaginationCollector(message, pages, mode, authorId) {
const collector = message.createMessageComponentCollector({ time: 300000 });
let currentPage = 0;
let quantity = 1;

collector.on('collect', async (interaction) => {
if (interaction.user.id !== authorId) {
return interaction.reply({ content: "You can't use these controls.", ephemeral: true });
}

if (interaction.isButton()) {
if (interaction.customId === 'previous') {
currentPage = (currentPage - 1 + pages.length) % pages.length;
await interaction.update(this.createShopEmbed(pages[currentPage], currentPage, pages.length, mode));
} else if (interaction.customId === 'next') {
currentPage = (currentPage + 1) % pages.length;
await interaction.update(this.createShopEmbed(pages[currentPage], currentPage, pages.length, mode));
} else if (interaction.customId === 'quantity') {
const modal = new ModalBuilder()
.setCustomId('quantityModal')
.setTitle('Set Quantity');

const quantityInput = new TextInputBuilder()
.setCustomId('quantityInput')
.setLabel('Enter quantity')
.setStyle(TextInputStyle.Short)
.setRequired(true)
.setValue(quantity.toString());

const actionRow = new ActionRowBuilder().addComponents(quantityInput);
modal.addComponents(actionRow);

await interaction.showModal(modal);
}
} else if (interaction.isStringSelectMenu()) {
if (interaction.customId === 'buy') {
await this.handlePurchase(interaction, authorId, quantity);
} else if (interaction.customId === 'sell') {
await this.handleSell(interaction, authorId, quantity);
}
} else if (interaction.isModalSubmit() && interaction.customId === 'quantityModal') {
const newQuantity = parseInt(interaction.fields.getTextInputValue('quantityInput'));
console.log(newQuantity)
if (!isNaN(newQuantity) && newQuantity > 0) {
quantity = newQuantity;
await interaction.reply({ content: `Quantity set to ${quantity}`, ephemeral: true });
} else {
await interaction.reply({ content: 'Invalid quantity. Please enter a positive number.', ephemeral: true });
}
}
});

collector.on('end', () => {
message.edit({ components: [] });
});
}
This is my modal, I wanna talk about the quantityModal here, the button for it is there, but it just gives an error when im trying to execute it, im unsure where even beginn to look for the problem
7 Replies
d.js toolkit
d.js toolkit7mo 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! - Marked as resolved by OP
Kanui
KanuiOP7mo ago
Thats the error it basically just says, something went wrong please try again
No description
duck
duck7mo ago
modals aren't message components, so modal submits can't be collected with a message component collector if you want a collector, you'd want to use <Interaction>.awaitModalSubmit()
d.js docs
d.js docs7mo ago
:method: ButtonInteraction#awaitModalSubmit() @14.15.3 Collects a single modal submit interaction that passes the filter. The Promise will reject if the time expires.
// Collect a modal submit interaction
const filter = (interaction) => interaction.customId === 'modal';
interaction.awaitModalSubmit({ filter, time: 15_000 })
.then(interaction => console.log(`${interaction.customId} was submitted!`))
.catch(console.error);
// Collect a modal submit interaction
const filter = (interaction) => interaction.customId === 'modal';
interaction.awaitModalSubmit({ filter, time: 15_000 })
.then(interaction => console.log(`${interaction.customId} was submitted!`))
.catch(console.error);
Kanui
KanuiOP7mo ago
Ahh, that makes total sense damit Ill give it a read and mark the case as solved then, thanks I just notice you said, "if you want a collector" for me it implies there are other options for it? @duck
duck
duck7mo ago
well there's always just listening to the interactionCreate event you can also instantiate InteractionCollector yourself if you didn't want a promisified collector
Kanui
KanuiOP7mo ago
Damn, I guess I need to read the docs again about those options, but since I managed to make it run with the change you named me Im happy for now. thanks alot

Did you find this page helpful?