passage d'un bot "homemade" avec des JSON écrit à discord.js #1
@ -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;
|
||||||
|
83
src/main.js
83
src/main.js
@ -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);
|
||||||
if (commandName === POLL_COMMAND_NAME) {
|
switch (commandName) {
|
||||||
if (!await guildExistsInDB(interaction.guildId)) {
|
case POLL_COMMAND_NAME:
|
||||||
insertGuildIntoDB(interaction.guildId);
|
if (await isLastPollCreatedByUserOpen(interaction.user.id)) {
|
||||||
}
|
|
||||||
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());
|
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);
|
||||||
|
|
||||||
|
|
||||||
|
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()) {
|
} 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();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user