bug fix, won't create poll if the question or one of the options contains the char '_'

This commit is contained in:
guams 2025-07-05 10:21:40 +02:00
parent ff5d95695e
commit 8fe4e73973

View File

@ -181,26 +181,40 @@ client.on(Events.InteractionCreate, async interaction => {
const buttons = new ActionRowBuilder(); const buttons = new ActionRowBuilder();
let pollDesc = ''; let pollDesc = '';
let areVoteOptionsAndQuestionValid = true;
voteOptions.forEach((voteOption, index) => { voteOptions.forEach((voteOption, index) => {
buttons.addComponents(new ButtonBuilder() // Le caractère "_" peut littéralement tout casser dans ma manière de stocker la donnée
.setCustomId(`${interaction.customId}_${voteOption}_${index + 1}`) if (!(voteOption.includes("_") || pollQuestionObj.value.includes("_"))) {
.setLabel(voteOption) buttons.addComponents(new ButtonBuilder()
.setStyle(ButtonStyle.Primary)); .setCustomId(`${interaction.customId}_${voteOption}_${index + 1}`)
pollDesc += `Option ${index + 1}: ${voteOption}\n` .setLabel(voteOption)
.setStyle(ButtonStyle.Primary));
pollDesc += `Option ${index + 1}: ${voteOption}\n`
} else {
areVoteOptionsAndQuestionValid = false;
}
}); });
const pollEmbed = new EmbedBuilder() if (!areVoteOptionsAndQuestionValid) {
.setTitle(`Sondage: ${pollQuestionObj.value}`) await interaction.reply({
.setDescription(pollDesc) flags: MessageFlags.Ephemeral,
.setColor(Colors.Green) content: `Une des options/question entrée est invalide (caractère "_" interdit !)`
.setFooter({text: 'https://guams.fr', iconURL: 'https://guams.fr/icon.webp'}); });
} else {
await insertPollIntoDB(sender.id, interaction.customId, guildFromWhereUserSendedInteraction.id, pollQuestionObj.value); const pollEmbed = new EmbedBuilder()
for (let i = 0; i < voteOptions.length; i++) { .setTitle(`Sondage: ${pollQuestionObj.value}`)
await insertVoteOptions(interaction.customId, `${interaction.customId}_${voteOptions[i]}_${i + 1}`, voteOptions[i]); .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()) { } else if (interaction.isMessageComponent()) {
const userVote = await findUserVoteForPoll(interaction.user.id, interaction.customId.split("_")[0]) const userVote = await findUserVoteForPoll(interaction.user.id, interaction.customId.split("_")[0])
if (userVote) { if (userVote) {
@ -217,6 +231,7 @@ client.on(Events.InteractionCreate, async interaction => {
}); });
} }
} }
}); });
async function createGuildAndUserIfNecessary(guildId, user) { async function createGuildAndUserIfNecessary(guildId, user) {