From 8fe4e73973b17dda1e60950d7a628e3c44470a8c Mon Sep 17 00:00:00 2001 From: guams Date: Sat, 5 Jul 2025 10:21:40 +0200 Subject: [PATCH] bug fix, won't create poll if the question or one of the options contains the char '_' --- src/main.js | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main.js b/src/main.js index 54f8744..09c9d5e 100644 --- a/src/main.js +++ b/src/main.js @@ -181,26 +181,40 @@ client.on(Events.InteractionCreate, async interaction => { const buttons = new ActionRowBuilder(); let pollDesc = ''; + let areVoteOptionsAndQuestionValid = true; voteOptions.forEach((voteOption, index) => { - buttons.addComponents(new ButtonBuilder() - .setCustomId(`${interaction.customId}_${voteOption}_${index + 1}`) - .setLabel(voteOption) - .setStyle(ButtonStyle.Primary)); - pollDesc += `Option ${index + 1}: ${voteOption}\n` + // Le caractère "_" peut littéralement tout casser dans ma manière de stocker la donnée + if (!(voteOption.includes("_") || pollQuestionObj.value.includes("_"))) { + buttons.addComponents(new ButtonBuilder() + .setCustomId(`${interaction.customId}_${voteOption}_${index + 1}`) + .setLabel(voteOption) + .setStyle(ButtonStyle.Primary)); + pollDesc += `Option ${index + 1}: ${voteOption}\n` + } else { + areVoteOptionsAndQuestionValid = false; + } }); - const pollEmbed = new EmbedBuilder() - .setTitle(`Sondage: ${pollQuestionObj.value}`) - .setDescription(pollDesc) - .setColor(Colors.Green) - .setFooter({text: 'https://guams.fr', iconURL: 'https://guams.fr/icon.webp'}); + if (!areVoteOptionsAndQuestionValid) { + await interaction.reply({ + flags: MessageFlags.Ephemeral, + content: `Une des options/question entrée est invalide (caractère "_" interdit !)` + }); + } else { - await insertPollIntoDB(sender.id, interaction.customId, guildFromWhereUserSendedInteraction.id, pollQuestionObj.value); - for (let i = 0; i < voteOptions.length; i++) { - await insertVoteOptions(interaction.customId, `${interaction.customId}_${voteOptions[i]}_${i + 1}`, voteOptions[i]); + const pollEmbed = new EmbedBuilder() + .setTitle(`Sondage: ${pollQuestionObj.value}`) + .setDescription(pollDesc) + .setColor(Colors.Green) + .setFooter({text: 'https://guams.fr', iconURL: 'https://guams.fr/icon.webp'}); + + await insertPollIntoDB(sender.id, interaction.customId, guildFromWhereUserSendedInteraction.id, pollQuestionObj.value); + for (let i = 0; i < voteOptions.length; i++) { + await insertVoteOptions(interaction.customId, `${interaction.customId}_${voteOptions[i]}_${i + 1}`, voteOptions[i]); + } + + await interaction.reply({embeds: [introEmbed, pollEmbed], components: [buttons]}); } - - await interaction.reply({embeds: [introEmbed, pollEmbed], components: [buttons]}); } else if (interaction.isMessageComponent()) { const userVote = await findUserVoteForPoll(interaction.user.id, interaction.customId.split("_")[0]) if (userVote) { @@ -217,6 +231,7 @@ client.on(Events.InteractionCreate, async interaction => { }); } } + }); async function createGuildAndUserIfNecessary(guildId, user) {