passage d'un bot "homemade" avec des JSON écrit à discord.js #1
@ -33,6 +33,21 @@ async function findVoteIdByVoteOptionAndPollId(voteOption, pollId) {
|
|||||||
return response.rows[0];
|
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) {
|
async function findBetValueMultiplierForPoll(pollId) {
|
||||||
const response = await DATABASE_CLIENT.query(
|
const response = await DATABASE_CLIENT.query(
|
||||||
"SELECT vote_id, (1 + (1 - (SUM(vote_member.bet_value) / total.sum))) AS cote " +
|
"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.findUsersAndBetValueByVoteId = findUsersAndBetValueByVoteId;
|
||||||
exports.addPointsToUser = addPointsToUser;
|
exports.addPointsToUser = addPointsToUser;
|
||||||
exports.closePoll = closePoll;
|
exports.closePoll = closePoll;
|
||||||
|
exports.findUserPoints = findUserPoints;
|
||||||
|
exports.changeNextBetValueForUser = changeNextBetValueForUser;
|
36
src/main.js
36
src/main.js
@ -21,19 +21,34 @@ const {
|
|||||||
findUserVoteForPoll,
|
findUserVoteForPoll,
|
||||||
findBetValueForUser,
|
findBetValueForUser,
|
||||||
bet,
|
bet,
|
||||||
insertVoteOptions, findVoteIdByVoteOptionAndPollId, findBetValueMultiplierForPoll, findUsersAndBetValueByVoteId,
|
insertVoteOptions,
|
||||||
addPointsToUser, closePoll
|
findVoteIdByVoteOptionAndPollId,
|
||||||
|
findBetValueMultiplierForPoll,
|
||||||
|
findUsersAndBetValueByVoteId,
|
||||||
|
addPointsToUser,
|
||||||
|
closePoll,
|
||||||
|
findUserPoints,
|
||||||
|
changeNextBetValueForUser
|
||||||
} = 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, PROFIL_COMMAND_NAME, BET_COMMAND_NAME, CLOSE_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, ActivityType} = require("discord-api-types/v10");
|
||||||
const {MessageFlags} = 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]});
|
||||||
|
|
||||||
client.once(Events.ClientReady, async readyClient => {
|
client.once(Events.ClientReady, async readyClient => {
|
||||||
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
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();
|
await DATABASE_CLIENT.connect();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -112,7 +127,6 @@ client.on(Events.InteractionCreate, async interaction => {
|
|||||||
}
|
}
|
||||||
const userFetched = await findUserById(concernedUser.id, interaction.guildId);
|
const userFetched = await findUserById(concernedUser.id, interaction.guildId);
|
||||||
|
|
||||||
|
|
||||||
const profilEmbed = new EmbedBuilder()
|
const profilEmbed = new EmbedBuilder()
|
||||||
.setTitle(`Profil de ${concernedUser.username}`)
|
.setTitle(`Profil de ${concernedUser.username}`)
|
||||||
.setThumbnail(`https://cdn.discordapp.com/avatars/${concernedUser.id}/${concernedUser.avatar}`)
|
.setThumbnail(`https://cdn.discordapp.com/avatars/${concernedUser.id}/${concernedUser.avatar}`)
|
||||||
@ -126,6 +140,20 @@ client.on(Events.InteractionCreate, async interaction => {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case BET_COMMAND_NAME: {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
|
Loading…
Reference in New Issue
Block a user