passage d'un bot "homemade" avec des JSON écrit à discord.js #1
@ -17,7 +17,7 @@ async function userExistsInDB(userId, guildId) {
|
||||
return response.rows.length > 0;
|
||||
}
|
||||
|
||||
async function getPollCreatedByUserAndOpened(userId) {
|
||||
async function findPollCreatedByUserAndOpened(userId) {
|
||||
const response = await DATABASE_CLIENT.query(
|
||||
"SELECT poll_id, question FROM poll WHERE creator=$1 and opened=true",
|
||||
[userId]
|
||||
@ -41,6 +41,43 @@ async function isLastPollCreatedByUserOpen(userId) {
|
||||
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) {
|
||||
await DATABASE_CLIENT.query(
|
||||
"INSERT INTO member (user_id, guild_id, username) VALUES ($1, $2, $3)",
|
||||
@ -78,5 +115,10 @@ exports.guildExistsInDB = guildExistsInDB;
|
||||
exports.insertGuildIntoDB = insertGuildIntoDB;
|
||||
exports.insertUserIntoDB = insertUserIntoDB;
|
||||
exports.isLastPollCreatedByUserOpen = isLastPollCreatedByUserOpen;
|
||||
exports.getPollCreatedByUserAndOpened = getPollCreatedByUserAndOpened;
|
||||
exports.findPollCreatedByUserAndOpened = findPollCreatedByUserAndOpened;
|
||||
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
|
||||
} = require('discord.js');
|
||||
const {
|
||||
DATABASE_CLIENT, insertPollIntoDB, guildExistsInDB, insertGuildIntoDB, userExistsInDB, insertUserIntoDB,
|
||||
isLastPollCreatedByUserOpen, getPollCreatedByUserAndOpened, findUserById
|
||||
DATABASE_CLIENT,
|
||||
insertPollIntoDB,
|
||||
guildExistsInDB,
|
||||
insertGuildIntoDB,
|
||||
userExistsInDB,
|
||||
insertUserIntoDB,
|
||||
isLastPollCreatedByUserOpen,
|
||||
findPollCreatedByUserAndOpened,
|
||||
findUserById, findUserVoteForPoll, findBetValueForUser, bet, insertVoteOptions
|
||||
} = require('./database');
|
||||
const {TOKEN} = require('../config.json');
|
||||
const {v4: uuidv4} = require('uuid');
|
||||
@ -41,7 +48,7 @@ client.on(Events.InteractionCreate, async interaction => {
|
||||
}
|
||||
break;
|
||||
case CLOSE_COMMAND_NAME:
|
||||
const response = getPollCreatedByUserAndOpened(interaction.user.id);
|
||||
const response = findPollCreatedByUserAndOpened(interaction.user.id);
|
||||
if (response.length) {
|
||||
// Logique pour fermer le poll
|
||||
// [
|
||||
@ -116,9 +123,28 @@ client.on(Events.InteractionCreate, async interaction => {
|
||||
.setColor(Colors.Green)
|
||||
.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]});
|
||||
} 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