passage d'un bot "homemade" avec des JSON écrit à discord.js #1
@ -9,17 +9,57 @@ const DATABASE_CLIENT = new Client({
|
|||||||
database: DB_NAME,
|
database: DB_NAME,
|
||||||
});
|
});
|
||||||
|
|
||||||
async function userExists() {
|
async function userExistsInDB(userId, guildId) {
|
||||||
const res = await DATABASE_CLIENT.query("SELECT * FROM member");
|
const response = await DATABASE_CLIENT.query(
|
||||||
return res.rows;
|
"SELECT user_id FROM member WHERE user_id=$1 AND guild_id=$2",
|
||||||
|
[userId, guildId]
|
||||||
|
);
|
||||||
|
return response.rows.length > 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function insertUserIntoDB(userId, guildId, username) {
|
||||||
|
await DATABASE_CLIENT.query(
|
||||||
|
"INSERT INTO member (user_id, guild_id, username) VALUES ($1, $2, $3)",
|
||||||
|
[userId, guildId, username]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function insertPollIntoDB(creatorId, pollId, guildId, questionLabel) {
|
async function insertPollIntoDB(creatorId, pollId, guildId, questionLabel) {
|
||||||
const response = await DATABASE_CLIENT.query("INSERT INTO poll(creator, poll_id, guild_id, question, opened) VALUES ($1, $2, $3, $4, true)", [creatorId, pollId, guildId, questionLabel])
|
const response = await DATABASE_CLIENT.query(
|
||||||
|
"INSERT INTO poll(creator, poll_id, guild_id, question, opened) " +
|
||||||
|
"VALUES ($1, $2, $3, $4, true)",
|
||||||
|
[creatorId, pollId, guildId, questionLabel])
|
||||||
return response.rows;
|
return response.rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.DATABASE_CLIENT = DATABASE_CLIENT;
|
async function guildExistsInDB(guildId) {
|
||||||
exports.userExists = userExists;
|
const response = await DATABASE_CLIENT.query(
|
||||||
exports.insertPollIntoDB = insertPollIntoDB;
|
"SELECT guild_id FROM guild WHERE guild_id=$1",
|
||||||
|
[guildId]
|
||||||
|
);
|
||||||
|
return response.rows.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function insertGuildIntoDB(guildId) {
|
||||||
|
await DATABASE_CLIENT.query(
|
||||||
|
"INSERT INTO guild (guild_id) VALUES ($1)",
|
||||||
|
[guildId]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.DATABASE_CLIENT = DATABASE_CLIENT;
|
||||||
|
exports.userExistsInDB = userExistsInDB;
|
||||||
|
exports.insertPollIntoDB = insertPollIntoDB;
|
||||||
|
exports.guildExistsInDB = guildExistsInDB;
|
||||||
|
exports.insertGuildIntoDB = insertGuildIntoDB;
|
||||||
|
exports.insertUserIntoDB = insertUserIntoDB;
|
||||||
|
exports.isLastPollCreatedByUserOpen = isLastPollCreatedByUserOpen;
|
||||||
|
|
||||||
|
12
src/main.js
12
src/main.js
@ -8,7 +8,9 @@ const {
|
|||||||
EmbedBuilder,
|
EmbedBuilder,
|
||||||
Colors, ButtonBuilder
|
Colors, ButtonBuilder
|
||||||
} = require('discord.js');
|
} = require('discord.js');
|
||||||
const {DATABASE_CLIENT, insertPollIntoDB} = require('./database');
|
const {DATABASE_CLIENT, insertPollIntoDB, guildExistsInDB, insertGuildIntoDB, userExistsInDB, insertUserIntoDB,
|
||||||
|
isLastPollCreatedByUserOpen
|
||||||
|
} = 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} = require("./constants");
|
||||||
@ -26,8 +28,16 @@ client.on(Events.InteractionCreate, async interaction => {
|
|||||||
const {commandName} = interaction;
|
const {commandName} = interaction;
|
||||||
|
|
||||||
if (commandName === POLL_COMMAND_NAME) {
|
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());
|
await interaction.showModal(buildPollModal());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if (interaction.isModalSubmit()) {
|
} else if (interaction.isModalSubmit()) {
|
||||||
const pollToCreate = interaction.components;
|
const pollToCreate = interaction.components;
|
||||||
const sender = interaction.user;
|
const sender = interaction.user;
|
||||||
|
Loading…
Reference in New Issue
Block a user