Edit: Use unique interaction IDs for each edit action

Resolves #30 where follow up edits would fail if previous
modals have been canceled/dismissed
This commit is contained in:
2023-01-16 17:05:58 +01:00
parent 1548ecf73b
commit 98d4f5c2ac

View File

@@ -79,13 +79,13 @@ module.exports = {
let option = m.customId.split("-")[0]; let option = m.customId.split("-")[0];
switch (option) { switch (option) {
case 'name': 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) { if (newName) {
await this.updateRecord(user, record, option, newName.value); await this.updateRecord(user, record, option, newName.value);
} }
break; break;
case 'description': 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) { if (newDesc) {
await this.updateRecord(user, record, option, newDesc.value); await this.updateRecord(user, record, option, newDesc.value);
} }
@@ -119,14 +119,15 @@ module.exports = {
fs.writeFileSync(path, imageBuffer.data); fs.writeFileSync(path, imageBuffer.data);
}, },
async openParagraphModal(interaction, title, placeholder) { async openParagraphModal(interaction, title, placeholder, persistentId) {
const customId = 'paragraphModal-' + persistentId;
const modal = new ModalBuilder() const modal = new ModalBuilder()
.setCustomId('paragraphModal') .setCustomId(customId)
.setTitle(title.substr(0, 44)); .setTitle(title.substr(0, 44));
let row = new ActionRowBuilder(); let row = new ActionRowBuilder();
let textInput = new TextInputBuilder() let textInput = new TextInputBuilder()
.setCustomId(`input-${interaction.id}`) .setCustomId(`input-${persistentId}`)
.setLabel("Enter new value") .setLabel("Enter new value")
.setStyle(TextInputStyle.Paragraph) .setStyle(TextInputStyle.Paragraph)
.setRequired(false) .setRequired(false)
@@ -145,19 +146,21 @@ module.exports = {
return null return null
}) })
if (submitted) { if (submitted && submitted.customId == customId) {
submitted.reply({ content: `You submitted: ${submitted.fields.getTextInputValue(`input-${interaction.id}`)}`}) await submitted.deferReply();
return { i: submitted, value: submitted.fields.getTextInputValue(`input-${interaction.id}`)}; 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() const modal = new ModalBuilder()
.setCustomId('textModal') .setCustomId(customId)
.setTitle(title.substr(0, 44)); .setTitle(title.substr(0, 44));
let row = new ActionRowBuilder(); let row = new ActionRowBuilder();
let textInput = new TextInputBuilder() let textInput = new TextInputBuilder()
.setCustomId(`input-${interaction.id}`) .setCustomId(`input-${persistentId}`)
.setLabel("Enter new value") .setLabel("Enter new value")
.setStyle(TextInputStyle.Short) .setStyle(TextInputStyle.Short)
.setRequired(false) .setRequired(false)
@@ -175,10 +178,11 @@ module.exports = {
console.error(error) console.error(error)
return null return null
}) })
if (submitted) { if (submitted && submitted.customId == customId) {
submitted.reply({ content: `You submitted: ${submitted.fields.getTextInputValue(`input-${interaction.id}`)}` }); await submitted.deferReply();
return { i: submitted, value: submitted.fields.getTextInputValue(`input-${interaction.id}`)}; 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) { async updateRecord(user, record, property, value) {