insert new poll
This commit is contained in:
parent
10c807f611
commit
e47bf9f451
@ -1,4 +1,4 @@
|
||||
const { Client } = require('pg');
|
||||
const {Client} = require('pg');
|
||||
const {DB_NAME, DB_USER, DB_PASSWORD, DB_ADDRESS, DB_PORT} = require('../config.json');
|
||||
|
||||
const DATABASE_CLIENT = new Client({
|
||||
@ -9,17 +9,57 @@ const DATABASE_CLIENT = new Client({
|
||||
database: DB_NAME,
|
||||
});
|
||||
|
||||
async function userExists() {
|
||||
const res = await DATABASE_CLIENT.query("SELECT * FROM member");
|
||||
return res.rows;
|
||||
async function userExistsInDB(userId, guildId) {
|
||||
const response = await DATABASE_CLIENT.query(
|
||||
"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) {
|
||||
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;
|
||||
}
|
||||
|
||||
exports.DATABASE_CLIENT = DATABASE_CLIENT;
|
||||
exports.userExists = userExists;
|
||||
exports.insertPollIntoDB = insertPollIntoDB;
|
||||
async function guildExistsInDB(guildId) {
|
||||
const response = await DATABASE_CLIENT.query(
|
||||
"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;
|
||||
|
||||
|
14
src/main.js
14
src/main.js
@ -8,7 +8,9 @@ const {
|
||||
EmbedBuilder,
|
||||
Colors, ButtonBuilder
|
||||
} = 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 {v4: uuidv4} = require('uuid');
|
||||
const {POLL_COMMAND_NAME} = require("./constants");
|
||||
@ -26,7 +28,15 @@ client.on(Events.InteractionCreate, async interaction => {
|
||||
const {commandName} = interaction;
|
||||
|
||||
if (commandName === POLL_COMMAND_NAME) {
|
||||
await interaction.showModal(buildPollModal());
|
||||
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());
|
||||
}
|
||||
}
|
||||
} else if (interaction.isModalSubmit()) {
|
||||
const pollToCreate = interaction.components;
|
||||
|
Loading…
Reference in New Issue
Block a user