From 98d4f5c2aca3b559cb00cded330f4ea04082cf6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Gro=C3=9F?= Date: Mon, 16 Jan 2023 17:05:58 +0100 Subject: [PATCH] Edit: Use unique interaction IDs for each edit action Resolves #30 where follow up edits would fail if previous modals have been canceled/dismissed --- commands/edit.js | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/commands/edit.js b/commands/edit.js index 98c9823..d100a4a 100644 --- a/commands/edit.js +++ b/commands/edit.js @@ -79,13 +79,13 @@ module.exports = { let option = m.customId.split("-")[0]; switch (option) { case 'name': - let newName = await this.openStringModal(m, `Edit ${option} for ${record.name}`, ""); + let newName = await this.openStringModal(m, `Edit ${option} for ${record.name}`, "", "name-"+interaction.id); if (newName) { await this.updateRecord(user, record, option, newName.value); } break; case 'description': - let newDesc = await this.openParagraphModal(m, `Edit ${option} for ${record.name}`, ""); + let newDesc = await this.openParagraphModal(m, `Edit ${option} for ${record.name}`, "", "desc-"+interaction.id); if (newDesc) { await this.updateRecord(user, record, option, newDesc.value); } @@ -119,14 +119,15 @@ module.exports = { fs.writeFileSync(path, imageBuffer.data); }, - async openParagraphModal(interaction, title, placeholder) { + async openParagraphModal(interaction, title, placeholder, persistentId) { + const customId = 'paragraphModal-' + persistentId; const modal = new ModalBuilder() - .setCustomId('paragraphModal') + .setCustomId(customId) .setTitle(title.substr(0, 44)); let row = new ActionRowBuilder(); let textInput = new TextInputBuilder() - .setCustomId(`input-${interaction.id}`) + .setCustomId(`input-${persistentId}`) .setLabel("Enter new value") .setStyle(TextInputStyle.Paragraph) .setRequired(false) @@ -145,19 +146,21 @@ module.exports = { return null }) - if (submitted) { - submitted.reply({ content: `You submitted: ${submitted.fields.getTextInputValue(`input-${interaction.id}`)}`}) - return { i: submitted, value: submitted.fields.getTextInputValue(`input-${interaction.id}`)}; + if (submitted && submitted.customId == customId) { + await submitted.deferReply(); + await submitted.editReply({ content: `You submitted: ${submitted.fields.getTextInputValue(`input-${persistentId}`)}`}) + return { i: submitted, value: submitted.fields.getTextInputValue(`input-${persistentId}`)}; } }, - async openStringModal(interaction, title, placeholder) { + async openStringModal(interaction, title, placeholder, persistentId) { + const customId = 'textModal-' + persistentId; const modal = new ModalBuilder() - .setCustomId('textModal') + .setCustomId(customId) .setTitle(title.substr(0, 44)); let row = new ActionRowBuilder(); let textInput = new TextInputBuilder() - .setCustomId(`input-${interaction.id}`) + .setCustomId(`input-${persistentId}`) .setLabel("Enter new value") .setStyle(TextInputStyle.Short) .setRequired(false) @@ -175,10 +178,11 @@ module.exports = { console.error(error) return null }) - - if (submitted) { - submitted.reply({ content: `You submitted: ${submitted.fields.getTextInputValue(`input-${interaction.id}`)}` }); - return { i: submitted, value: submitted.fields.getTextInputValue(`input-${interaction.id}`)}; + + if (submitted && submitted.customId == customId) { + await submitted.deferReply(); + await submitted.editReply({ content: `You submitted: ${submitted.fields.getTextInputValue(`input-${persistentId}`)}` }); + return { i: submitted, value: submitted.fields.getTextInputValue(`input-${persistentId}`)}; } }, async updateRecord(user, record, property, value) {