bug fix, can't vote on a closed poll
This commit is contained in:
parent
8fe4e73973
commit
49e52bf2dd
@ -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;
|
||||
exports.changeNextBetValueForUser = changeNextBetValueForUser;
|
||||
exports.isPollOpened = isPollOpened;
|
45
src/main.js
45
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);
|
||||
|
Loading…
Reference in New Issue
Block a user