passage d'un bot "homemade" avec des JSON écrit à discord.js #1

Merged
Guams merged 8 commits from refonte into main 2025-06-29 09:41:09 +02:00
2 changed files with 50 additions and 5 deletions
Showing only changes of commit 6217203967 - Show all commits

View File

@ -33,6 +33,21 @@ async function findVoteIdByVoteOptionAndPollId(voteOption, pollId) {
return response.rows[0];
}
async function findUserPoints(userId, guildId) {
const response = await DATABASE_CLIENT.query(
"SELECT points FROM member WHERE user_id=$1 AND guild_id=$2",
[userId, guildId]
);
return response.rows[0];
}
async function changeNextBetValueForUser(userId, guildId, amount) {
await DATABASE_CLIENT.query(
"UPDATE member SET bet_value=$1 WHERE user_id=$2 AND guild_id=$3",
[amount, userId, guildId]
);
}
async function findBetValueMultiplierForPoll(pollId) {
const response = await DATABASE_CLIENT.query(
"SELECT vote_id, (1 + (1 - (SUM(vote_member.bet_value) / total.sum))) AS cote " +
@ -171,3 +186,5 @@ exports.findBetValueMultiplierForPoll = findBetValueMultiplierForPoll;
exports.findUsersAndBetValueByVoteId = findUsersAndBetValueByVoteId;
exports.addPointsToUser = addPointsToUser;
exports.closePoll = closePoll;
exports.findUserPoints = findUserPoints;
exports.changeNextBetValueForUser = changeNextBetValueForUser;

View File

@ -21,19 +21,34 @@ const {
findUserVoteForPoll,
findBetValueForUser,
bet,
insertVoteOptions, findVoteIdByVoteOptionAndPollId, findBetValueMultiplierForPoll, findUsersAndBetValueByVoteId,
addPointsToUser, closePoll
insertVoteOptions,
findVoteIdByVoteOptionAndPollId,
findBetValueMultiplierForPoll,
findUsersAndBetValueByVoteId,
addPointsToUser,
closePoll,
findUserPoints,
changeNextBetValueForUser
} = require('./database');
const {TOKEN} = require('../config.json');
const {v4: uuidv4} = require('uuid');
const {POLL_COMMAND_NAME, PROFIL_COMMAND_NAME, BET_COMMAND_NAME, CLOSE_COMMAND_NAME} = require("./constants");
const {ButtonStyle} = require("discord-api-types/v10");
const {ButtonStyle, ActivityType} = require("discord-api-types/v10");
const {MessageFlags} = require("discord-api-types/v10");
const client = new Client({intents: [GatewayIntentBits.Guilds]});
client.once(Events.ClientReady, async readyClient => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
client.user.setPresence({
status: "dnd",
activities: [
{
name: "son argent flamber",
type: ActivityType.Watching,
}
]
})
await DATABASE_CLIENT.connect();
});
@ -112,7 +127,6 @@ client.on(Events.InteractionCreate, async interaction => {
}
const userFetched = await findUserById(concernedUser.id, interaction.guildId);
const profilEmbed = new EmbedBuilder()
.setTitle(`Profil de ${concernedUser.username}`)
.setThumbnail(`https://cdn.discordapp.com/avatars/${concernedUser.id}/${concernedUser.avatar}`)
@ -126,6 +140,20 @@ client.on(Events.InteractionCreate, async interaction => {
break;
}
case BET_COMMAND_NAME: {
const amountForNextBet = interaction.options.data[0].value;
const senderPoints = await findUserPoints(interaction.user.id, interaction.guildId);
if (senderPoints >= amountForNextBet) {
await changeNextBetValueForUser(interaction.user.id, interaction.guildId, amountForNextBet);
await interaction.reply({
flags: MessageFlags.Ephemeral,
content: `${interaction.user.username}, ton prochain pari vaudra **${amountForNextBet}**`
});
} else {
await interaction.reply({
flags: MessageFlags.Ephemeral,
content: `${interaction.user.id}, solde insuffisant !`
});
}
break;
}
default: {