diff --git a/src/database.js b/src/database.js index aa16211..db162d2 100644 --- a/src/database.js +++ b/src/database.js @@ -108,6 +108,15 @@ async function findUserVoteForPoll(userId, pollId) { return response.rows[0]; } +async function isPollOpened(poll_id){ + const response = await DATABASE_CLIENT.query( + "SELECT opened FROM poll WHERE poll_id=$1", + [poll_id] + ); + return response.rows[0]; +} + + async function findBetValueForUser(userId, guildId) { const response = await DATABASE_CLIENT.query( "SELECT bet_value FROM member WHERE user_id=$1 AND member.guild_id=$2", @@ -187,4 +196,5 @@ exports.findUsersAndBetValueByVoteId = findUsersAndBetValueByVoteId; exports.addPointsToUser = addPointsToUser; exports.closePoll = closePoll; exports.findUserPoints = findUserPoints; -exports.changeNextBetValueForUser = changeNextBetValueForUser; \ No newline at end of file +exports.changeNextBetValueForUser = changeNextBetValueForUser; +exports.isPollOpened = isPollOpened; \ No newline at end of file diff --git a/src/main.js b/src/main.js index 09c9d5e..454cc59 100644 --- a/src/main.js +++ b/src/main.js @@ -28,7 +28,7 @@ const { addPointsToUser, closePoll, findUserPoints, - changeNextBetValueForUser + changeNextBetValueForUser, isPollOpened } = require('./database'); const {TOKEN} = require('../config.json'); const {v4: uuidv4} = require('uuid'); @@ -216,24 +216,39 @@ client.on(Events.InteractionCreate, async interaction => { 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) { - await interaction.reply({ - flags: MessageFlags.Ephemeral, - content: `${interaction.user.username}, tu as déjà voté pour **${userVote.vote_id.split("_")[1]}**` - }); - } else { - const userBetValue = await findBetValueForUser(interaction.user.id, interaction.guildId); - await bet(interaction.user.id, interaction.customId.split("_")[0], interaction.guildId, interaction.customId, userBetValue) - await interaction.reply({ - flags: MessageFlags.Ephemeral, - content: `${interaction.user.username}, tu as misé ${userBetValue} sur l'option **${interaction.customId.split("_")[1]}** ! 🎉` - }); - } + sendAppropriateMessageToUserWhenHeVotes(interaction); } }); +async function sendAppropriateMessageToUserWhenHeVotes(interaction) { + // interaction.customId.split("_")[0] c'est le pollId au cas où c'est pas assez clair mdr + const isConcernedPollOpened = await isPollOpened(interaction.customId.split("_")[0]); + if (!isConcernedPollOpened.opened) { + await interaction.reply({ + flags: MessageFlags.Ephemeral, + content: `Ce sondage a déjà été cloturé !` + }); + return + } + + const userVote = await findUserVoteForPoll(interaction.user.id, interaction.customId.split("_")[0]) + if (userVote) { + await interaction.reply({ + flags: MessageFlags.Ephemeral, + content: `${interaction.user.username}, tu as déjà voté pour **${userVote.vote_id.split("_")[1]}**` + }); + } else { + const userBetValue = await findBetValueForUser(interaction.user.id, interaction.guildId); + await bet(interaction.user.id, interaction.customId.split("_")[0], interaction.guildId, interaction.customId, userBetValue) + await interaction.reply({ + flags: MessageFlags.Ephemeral, + content: `${interaction.user.username}, tu as misé ${userBetValue} sur l'option **${interaction.customId.split("_")[1]}** ! 🎉` + }); + } + +} + async function createGuildAndUserIfNecessary(guildId, user) { if (!await guildExistsInDB(guildId)) { await insertGuildIntoDB(guildId);