From 7823dcd5495f22f6b120bbf85c5b1aa30531a902 Mon Sep 17 00:00:00 2001 From: baz Date: Mon, 18 Dec 2023 23:53:12 +0000 Subject: [PATCH] Add new databaseHelperFunctions --- databaseHelperFunctions.js | 84 +++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index a1dff62..a5673e5 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -1,5 +1,4 @@ -const { SlashCommandBuilder } = require('discord.js'); -const { Users } = require ('./dbObjects.js'); +const { Users, Games, BeatenGames } = require ('./dbObjects.js'); async function checkUserRegistration(user) { @@ -23,6 +22,87 @@ async function checkUserRegistration(user) { return false; } +async function getUserRegistration(user) { + + let u = await Users.findOne({ where: { discord_id: user.id } }) + .catch((err) => { + console.log(err); + }); + + if (u) return u; + + await Users.create({ discord_id: user.id, username: user.username }) + .then((data) => { + u = data; + }) + .catch((err) => { + console.log(err); + }); + + if (u) return u; + + return null; +} + +async function checkGameStorage(game) { + let g = await Games.findOne({ where: { igdb_id: game.id, name: game.name } }) + .catch((err) => { + console.log(err); + }); + + if (g) return g; + + await Games.create({ igdb_id: game.id, name: game.name }) + .then((data) => { + g = data; + }) + .catch((err) => { + console.log(err); + }); + + if (g) return g; + + return null; +} + +async function createBeatenGameEntry(user, game) { + let bg = await BeatenGames.findOne({ where: { userId: user.id, gameId: game.id } }) + .catch((err) => { + console.log(err); + }); + + if (bg) return false; + + await BeatenGames.create({ userId: user.id, gameId: game.id }) + .then((data) => { + bg = data; + }) + .catch((err) => { + console.log(err); + }); + + if (bg) return true; + + return false; +} + +async function getBeatenGameCount(user) { + const u = await Users.findOne({ where: { id: user.id } }) + .catch((err) => { + console.log(err); + }); + + if (!u) return -1; + + const count = await u.countBeatenGames(); + + return count; +} + module.exports = { checkUserRegistration, + getUserRegistration, + checkGameStorage, + createBeatenGameEntry, + getBeatenGameCount, }; \ No newline at end of file