From 192035b1e7c1d523f96c4f841e838f095d0bd527 Mon Sep 17 00:00:00 2001 From: guams Date: Sat, 28 Jun 2025 19:25:03 +0200 Subject: [PATCH] added profil command --- src/database.js | 21 ++++++++++-- src/main.js | 85 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 88 insertions(+), 18 deletions(-) diff --git a/src/database.js b/src/database.js index 1d6a5a2..5e11951 100644 --- a/src/database.js +++ b/src/database.js @@ -17,12 +17,28 @@ async function userExistsInDB(userId, guildId) { return response.rows.length > 0; } +async function getPollCreatedByUserAndOpened(userId) { + const response = await DATABASE_CLIENT.query( + "SELECT poll_id, question FROM poll WHERE creator=$1 and opened=true", + [userId] + ); + return response.rows; +} + +async function findUserById(userId, guildId) { + const response = await DATABASE_CLIENT.query( + "SELECT username, points, bet_value FROM member WHERE user_id=$1 AND guild_id=$2", + [userId, guildId] + ); + return response.rows[0]; +} + async function isLastPollCreatedByUserOpen(userId) { const response = await DATABASE_CLIENT.query( "SELECT opened FROM poll WHERE creator=$1 AND opened=true", [userId] ); - return response.rows.length > 0; + return !response.rows.length > 0; } async function insertUserIntoDB(userId, guildId, username) { @@ -62,4 +78,5 @@ exports.guildExistsInDB = guildExistsInDB; exports.insertGuildIntoDB = insertGuildIntoDB; exports.insertUserIntoDB = insertUserIntoDB; exports.isLastPollCreatedByUserOpen = isLastPollCreatedByUserOpen; - +exports.getPollCreatedByUserAndOpened = getPollCreatedByUserAndOpened; +exports.findUserById = findUserById; diff --git a/src/main.js b/src/main.js index edba6c5..667b50d 100644 --- a/src/main.js +++ b/src/main.js @@ -8,13 +8,15 @@ const { EmbedBuilder, Colors, ButtonBuilder } = require('discord.js'); -const {DATABASE_CLIENT, insertPollIntoDB, guildExistsInDB, insertGuildIntoDB, userExistsInDB, insertUserIntoDB, - isLastPollCreatedByUserOpen +const { + DATABASE_CLIENT, insertPollIntoDB, guildExistsInDB, insertGuildIntoDB, userExistsInDB, insertUserIntoDB, + isLastPollCreatedByUserOpen, getPollCreatedByUserAndOpened, findUserById } = require('./database'); const {TOKEN} = require('../config.json'); const {v4: uuidv4} = require('uuid'); -const {POLL_COMMAND_NAME} = require("./constants"); +const {POLL_COMMAND_NAME, PROFIL_COMMAND_NAME, BET_COMMAND_NAME, CLOSE_COMMAND_NAME} = require("./constants"); const {ButtonStyle} = require("discord-api-types/v10"); +const {MessageFlags} = require("discord-api-types/v10"); const client = new Client({intents: [GatewayIntentBits.Guilds]}); @@ -26,17 +28,59 @@ client.once(Events.ClientReady, async readyClient => { client.on(Events.InteractionCreate, async interaction => { if (interaction.isCommand()) { const {commandName} = interaction; + await createGuildAndUserIfNecessary(interaction.guildId, interaction.user); + switch (commandName) { + case POLL_COMMAND_NAME: + if (await isLastPollCreatedByUserOpen(interaction.user.id)) { + await interaction.showModal(buildPollModal()); + } else { + await interaction.reply({ + flags: MessageFlags.Ephemeral, + content: `${interaction.user.username}, il faut d'abord clôturer ton sondage avant de pouvoir en créer un nouveau. (commande: **/close**)` + }) + } + break; + case CLOSE_COMMAND_NAME: + const response = getPollCreatedByUserAndOpened(interaction.user.id); + if (response.length) { + // Logique pour fermer le poll + // [ + // { + // poll_id: '9b02d63e-5e4b-411e-bb65-2f412f65b21f', + // question: 'coucou' + // } + // ] + } + break; + case PROFIL_COMMAND_NAME: + let concernedUser; + if (interaction.options.data.length) { + concernedUser = interaction.options.data[0].user; + if (!await userExistsInDB(concernedUser.id)) { + await insertUserIntoDB(concernedUser.id, interaction.guildId, concernedUser.username); + } + } else { + concernedUser = interaction.user; + } + const userFetched = await findUserById(concernedUser.id, interaction.guildId); - if (commandName === POLL_COMMAND_NAME) { - if (!await guildExistsInDB(interaction.guildId)) { - insertGuildIntoDB(interaction.guildId); - } - if (!await userExistsInDB(interaction.user.id, interaction.guildId)) { - insertUserIntoDB(interaction.user.id, interaction.guildId, interaction.user.username); - } - if (isLastPollCreatedByUserOpen(interaction.user.id)) { - await interaction.showModal(buildPollModal()); - } + + const profilEmbed = new EmbedBuilder() + .setTitle(`Profil de ${concernedUser.username}`) + .setThumbnail(`https://cdn.discordapp.com/avatars/${concernedUser.id}/${concernedUser.avatar}`) + .setFooter({text: 'https://guams.fr', iconURL: 'https://guams.fr/icon.webp'}) + .addFields( + {name: 'Solde du compte', value: `${userFetched.points}`, inline: true}, + {name: 'Valeur du prochain pari', value: `${userFetched.bet_value}`, inline: true}) + .setTimestamp() + .setColor(Colors.Aqua); + await interaction.reply({embeds: [profilEmbed]}); + break; + case BET_COMMAND_NAME: + break; + default: + console.log(`Commande [${commandName}] inconnue`); + break; } } else if (interaction.isModalSubmit()) { const pollToCreate = interaction.components; @@ -54,7 +98,7 @@ client.on(Events.InteractionCreate, async interaction => { const introEmbed = new EmbedBuilder() .setTitle("Les paris sont ouverts !") .setDescription("Vous pouvez parier vos points, ils ne passeront pas en dessous de 50") - .setColor(Colors.Aqua); + .setColor(Colors.Aqua) const buttons = new ActionRowBuilder(); let pollDesc = ''; @@ -69,15 +113,24 @@ client.on(Events.InteractionCreate, async interaction => { const pollEmbed = new EmbedBuilder() .setTitle(`Sondage: ${pollQuestionObj.value}`) .setDescription(pollDesc) - .setColor(Colors.Green); + .setColor(Colors.Green) + .setFooter({text: 'https://guams.fr', iconURL: 'https://guams.fr/icon.webp'}); insertPollIntoDB(sender.id, interaction.customId, guildFromWhereUserSendedInteraction.id, pollQuestionObj.value); await interaction.reply({embeds: [introEmbed, pollEmbed], components: [buttons]}); } - }); +async function createGuildAndUserIfNecessary(guildId, user) { + if (!await guildExistsInDB(guildId)) { + insertGuildIntoDB(guildId); + } + if (!await userExistsInDB(user.id, guildId)) { + insertUserIntoDB(user.id, guildId, user.username); + } +} + function buildPollModal() { const pollId = uuidv4();