Kanui
DIdiscord.js - Imagine ❄
•Created by Kanui on 8/6/2024 in #djs-questions
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: [] });
});
}
10 replies