added profil command

This commit is contained in:
guams 2025-06-28 19:25:03 +02:00
parent 25e9bb6f74
commit 192035b1e7
2 changed files with 88 additions and 18 deletions

View File

@ -17,12 +17,28 @@ async function userExistsInDB(userId, guildId) {
return response.rows.length > 0; 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) { async function isLastPollCreatedByUserOpen(userId) {
const response = await DATABASE_CLIENT.query( const response = await DATABASE_CLIENT.query(
"SELECT opened FROM poll WHERE creator=$1 AND opened=true", "SELECT opened FROM poll WHERE creator=$1 AND opened=true",
[userId] [userId]
); );
return response.rows.length > 0; return !response.rows.length > 0;
} }
async function insertUserIntoDB(userId, guildId, username) { async function insertUserIntoDB(userId, guildId, username) {
@ -62,4 +78,5 @@ 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.findUserById = findUserById;

View File

@ -8,13 +8,15 @@ const {
EmbedBuilder, EmbedBuilder,
Colors, ButtonBuilder Colors, ButtonBuilder
} = require('discord.js'); } = require('discord.js');
const {DATABASE_CLIENT, insertPollIntoDB, guildExistsInDB, insertGuildIntoDB, userExistsInDB, insertUserIntoDB, const {
isLastPollCreatedByUserOpen DATABASE_CLIENT, insertPollIntoDB, guildExistsInDB, insertGuildIntoDB, userExistsInDB, insertUserIntoDB,
isLastPollCreatedByUserOpen, getPollCreatedByUserAndOpened, findUserById
} = require('./database'); } = require('./database');
const {TOKEN} = require('../config.json'); const {TOKEN} = require('../config.json');
const {v4: uuidv4} = require('uuid'); 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 {ButtonStyle} = require("discord-api-types/v10");
const {MessageFlags} = require("discord-api-types/v10");
const client = new Client({intents: [GatewayIntentBits.Guilds]}); const client = new Client({intents: [GatewayIntentBits.Guilds]});
@ -26,17 +28,59 @@ client.once(Events.ClientReady, async readyClient => {
client.on(Events.InteractionCreate, async interaction => { client.on(Events.InteractionCreate, async interaction => {
if (interaction.isCommand()) { if (interaction.isCommand()) {
const {commandName} = interaction; 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)) { const profilEmbed = new EmbedBuilder()
insertGuildIntoDB(interaction.guildId); .setTitle(`Profil de ${concernedUser.username}`)
} .setThumbnail(`https://cdn.discordapp.com/avatars/${concernedUser.id}/${concernedUser.avatar}`)
if (!await userExistsInDB(interaction.user.id, interaction.guildId)) { .setFooter({text: 'https://guams.fr', iconURL: 'https://guams.fr/icon.webp'})
insertUserIntoDB(interaction.user.id, interaction.guildId, interaction.user.username); .addFields(
} {name: 'Solde du compte', value: `${userFetched.points}`, inline: true},
if (isLastPollCreatedByUserOpen(interaction.user.id)) { {name: 'Valeur du prochain pari', value: `${userFetched.bet_value}`, inline: true})
await interaction.showModal(buildPollModal()); .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()) { } else if (interaction.isModalSubmit()) {
const pollToCreate = interaction.components; const pollToCreate = interaction.components;
@ -54,7 +98,7 @@ client.on(Events.InteractionCreate, async interaction => {
const introEmbed = new EmbedBuilder() const introEmbed = new EmbedBuilder()
.setTitle("Les paris sont ouverts !") .setTitle("Les paris sont ouverts !")
.setDescription("Vous pouvez parier vos points, ils ne passeront pas en dessous de 50") .setDescription("Vous pouvez parier vos points, ils ne passeront pas en dessous de 50")
.setColor(Colors.Aqua); .setColor(Colors.Aqua)
const buttons = new ActionRowBuilder(); const buttons = new ActionRowBuilder();
let pollDesc = ''; let pollDesc = '';
@ -69,15 +113,24 @@ client.on(Events.InteractionCreate, async interaction => {
const pollEmbed = new EmbedBuilder() const pollEmbed = new EmbedBuilder()
.setTitle(`Sondage: ${pollQuestionObj.value}`) .setTitle(`Sondage: ${pollQuestionObj.value}`)
.setDescription(pollDesc) .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); insertPollIntoDB(sender.id, interaction.customId, guildFromWhereUserSendedInteraction.id, pollQuestionObj.value);
await interaction.reply({embeds: [introEmbed, pollEmbed], components: [buttons]}); 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() { function buildPollModal() {
const pollId = uuidv4(); const pollId = uuidv4();