added bet feature
This commit is contained in:
parent
192035b1e7
commit
f163f9a536
@ -17,7 +17,7 @@ async function userExistsInDB(userId, guildId) {
|
|||||||
return response.rows.length > 0;
|
return response.rows.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getPollCreatedByUserAndOpened(userId) {
|
async function findPollCreatedByUserAndOpened(userId) {
|
||||||
const response = await DATABASE_CLIENT.query(
|
const response = await DATABASE_CLIENT.query(
|
||||||
"SELECT poll_id, question FROM poll WHERE creator=$1 and opened=true",
|
"SELECT poll_id, question FROM poll WHERE creator=$1 and opened=true",
|
||||||
[userId]
|
[userId]
|
||||||
@ -41,6 +41,43 @@ async function isLastPollCreatedByUserOpen(userId) {
|
|||||||
return !response.rows.length > 0;
|
return !response.rows.length > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function findUserVoteForPoll(userId, pollId) {
|
||||||
|
const response = await DATABASE_CLIENT.query(
|
||||||
|
"SELECT vote_id FROM vote_member WHERE user_id=$1 AND poll_id=$2",
|
||||||
|
[userId, pollId]
|
||||||
|
);
|
||||||
|
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",
|
||||||
|
[userId, guildId]
|
||||||
|
);
|
||||||
|
return response.rows[0].bet_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function substractUserPoints(userId, betValue) {
|
||||||
|
await DATABASE_CLIENT.query(
|
||||||
|
"UPDATE member SET points = points - $1 WHERE user_id = $2 AND points - $1 >= 50",
|
||||||
|
betValue, userId
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function bet(userId, pollId, guildId, voteId, betValue) {
|
||||||
|
await DATABASE_CLIENT.query(
|
||||||
|
"INSERT INTO vote_member (user_id, poll_id, guild_id, vote_id, bet_value) VALUES ($1, $2, $3, $4, $5)",
|
||||||
|
[userId, pollId, guildId, voteId, betValue]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function insertVoteOptions(pollId, voteId, voteOption) {
|
||||||
|
await DATABASE_CLIENT.query(
|
||||||
|
"INSERT INTO vote (poll_id, vote_id, vote_option) VALUES ($1, $2, $3)",
|
||||||
|
[pollId, voteId, voteOption]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
async function insertUserIntoDB(userId, guildId, username) {
|
async function insertUserIntoDB(userId, guildId, username) {
|
||||||
await DATABASE_CLIENT.query(
|
await DATABASE_CLIENT.query(
|
||||||
"INSERT INTO member (user_id, guild_id, username) VALUES ($1, $2, $3)",
|
"INSERT INTO member (user_id, guild_id, username) VALUES ($1, $2, $3)",
|
||||||
@ -78,5 +115,10 @@ exports.guildExistsInDB = guildExistsInDB;
|
|||||||
exports.insertGuildIntoDB = insertGuildIntoDB;
|
exports.insertGuildIntoDB = insertGuildIntoDB;
|
||||||
exports.insertUserIntoDB = insertUserIntoDB;
|
exports.insertUserIntoDB = insertUserIntoDB;
|
||||||
exports.isLastPollCreatedByUserOpen = isLastPollCreatedByUserOpen;
|
exports.isLastPollCreatedByUserOpen = isLastPollCreatedByUserOpen;
|
||||||
exports.getPollCreatedByUserAndOpened = getPollCreatedByUserAndOpened;
|
exports.findPollCreatedByUserAndOpened = findPollCreatedByUserAndOpened;
|
||||||
exports.findUserById = findUserById;
|
exports.findUserById = findUserById;
|
||||||
|
exports.findUserVoteForPoll = findUserVoteForPoll;
|
||||||
|
exports.findBetValueForUser = findBetValueForUser;
|
||||||
|
exports.bet = bet;
|
||||||
|
exports.substractUserPoints = substractUserPoints;
|
||||||
|
exports.insertVoteOptions = insertVoteOptions;
|
||||||
|
34
src/main.js
34
src/main.js
@ -9,8 +9,15 @@ const {
|
|||||||
Colors, ButtonBuilder
|
Colors, ButtonBuilder
|
||||||
} = require('discord.js');
|
} = require('discord.js');
|
||||||
const {
|
const {
|
||||||
DATABASE_CLIENT, insertPollIntoDB, guildExistsInDB, insertGuildIntoDB, userExistsInDB, insertUserIntoDB,
|
DATABASE_CLIENT,
|
||||||
isLastPollCreatedByUserOpen, getPollCreatedByUserAndOpened, findUserById
|
insertPollIntoDB,
|
||||||
|
guildExistsInDB,
|
||||||
|
insertGuildIntoDB,
|
||||||
|
userExistsInDB,
|
||||||
|
insertUserIntoDB,
|
||||||
|
isLastPollCreatedByUserOpen,
|
||||||
|
findPollCreatedByUserAndOpened,
|
||||||
|
findUserById, findUserVoteForPoll, findBetValueForUser, bet, insertVoteOptions
|
||||||
} = require('./database');
|
} = require('./database');
|
||||||
const {TOKEN} = require('../config.json');
|
const {TOKEN} = require('../config.json');
|
||||||
const {v4: uuidv4} = require('uuid');
|
const {v4: uuidv4} = require('uuid');
|
||||||
@ -41,7 +48,7 @@ client.on(Events.InteractionCreate, async interaction => {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case CLOSE_COMMAND_NAME:
|
case CLOSE_COMMAND_NAME:
|
||||||
const response = getPollCreatedByUserAndOpened(interaction.user.id);
|
const response = findPollCreatedByUserAndOpened(interaction.user.id);
|
||||||
if (response.length) {
|
if (response.length) {
|
||||||
// Logique pour fermer le poll
|
// Logique pour fermer le poll
|
||||||
// [
|
// [
|
||||||
@ -116,9 +123,28 @@ client.on(Events.InteractionCreate, async interaction => {
|
|||||||
.setColor(Colors.Green)
|
.setColor(Colors.Green)
|
||||||
.setFooter({text: 'https://guams.fr', iconURL: 'https://guams.fr/icon.webp'});
|
.setFooter({text: 'https://guams.fr', iconURL: 'https://guams.fr/icon.webp'});
|
||||||
|
|
||||||
insertPollIntoDB(sender.id, interaction.customId, guildFromWhereUserSendedInteraction.id, pollQuestionObj.value);
|
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()) {
|
||||||
|
console.log(interaction)
|
||||||
|
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]}** ! 🎉`
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user