From 826dca89d2b3a17d2b9ee4caf9438f076e18a1a8 Mon Sep 17 00:00:00 2001 From: baz Date: Tue, 19 Dec 2023 20:42:54 +0000 Subject: [PATCH] Create deletegameentry command --- commands/100-games/deleteGameEntry.js | 31 +++++++++++++++++++ databaseHelperFunctions.js | 44 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 commands/100-games/deleteGameEntry.js diff --git a/commands/100-games/deleteGameEntry.js b/commands/100-games/deleteGameEntry.js new file mode 100644 index 0000000..578490b --- /dev/null +++ b/commands/100-games/deleteGameEntry.js @@ -0,0 +1,31 @@ +const { SlashCommandBuilder } = require('discord.js'); +const { deleteBeatenGameId, getUserRegistration, deleteBeatenGameNum, checkGameStorageId } = require('../../databaseHelperFunctions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('deletegameentry') + .setDescription('Delete a game that you have beaten from the 100 game challenge!') + .addNumberOption(option => option.setName('databaseentryid').setDescription('The beat game id.').setMinValue(1)) + .addNumberOption(option => option.setName('beatgamenumber').setDescription('Index of the game to delete in the list of beaten games.').setMinValue(1).setMaxValue(100)), + async execute(interaction) { + const databaseEntryId = interaction.options.getNumber('databaseentryid'); + const beatGameNumber = interaction.options.getNumber('beatgamenumber'); + + if (!databaseEntryId && !beatGameNumber) return interaction.reply('No parameters supplied'); + + const userDatabaseEntry = await getUserRegistration(interaction.user); + let result; + if (databaseEntryId) { + result = await deleteBeatenGameId(databaseEntryId, userDatabaseEntry); + } else if (beatGameNumber) { + result = await deleteBeatenGameNum(beatGameNumber, userDatabaseEntry); + } + + if (result) { + const game = await checkGameStorageId(result.gameId); + return interaction.reply(`${game.name} successfully deleted`); + } + + return interaction.reply('Unable to delete entry / No entry found.'); + }, +}; \ No newline at end of file diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index a5673e5..8ee8a76 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -65,6 +65,17 @@ async function checkGameStorage(game) { return null; } +async function checkGameStorageId(id) { + const g = await Games.findOne({ where: { id: id } }) + .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) => { @@ -99,10 +110,43 @@ async function getBeatenGameCount(user) { return count; } +async function deleteBeatenGameId(id, user) { + const bg = await BeatenGames.findOne({ where: { id: id, userId: user.id } }) + .catch((err) => { + console.log(err); + }); + + if (!bg) return false; + + const entry = bg; + await bg.destroy(); + + return entry; +} + +async function deleteBeatenGameNum(num, user) { + const bg = await BeatenGames.findAll({ where: { userId: user.id } }) + .catch((err) => { + console.log(err); + }); + + if (!bg) return false; + + if (bg.length < num) return false; + + const entry = bg[num - 1]; + await bg[num - 1].destroy(); + + return entry; +} + module.exports = { checkUserRegistration, getUserRegistration, checkGameStorage, createBeatenGameEntry, getBeatenGameCount, + deleteBeatenGameId, + deleteBeatenGameNum, + checkGameStorageId, }; \ No newline at end of file