Create deletegameentry command

This commit is contained in:
baz 2023-12-19 20:42:54 +00:00
parent 8c19d849bf
commit 826dca89d2
2 changed files with 75 additions and 0 deletions

View File

@ -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.');
},
};

View File

@ -65,6 +65,17 @@ async function checkGameStorage(game) {
return null; 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) { async function createBeatenGameEntry(user, game) {
let bg = await BeatenGames.findOne({ where: { userId: user.id, gameId: game.id } }) let bg = await BeatenGames.findOne({ where: { userId: user.id, gameId: game.id } })
.catch((err) => { .catch((err) => {
@ -99,10 +110,43 @@ async function getBeatenGameCount(user) {
return count; 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 = { module.exports = {
checkUserRegistration, checkUserRegistration,
getUserRegistration, getUserRegistration,
checkGameStorage, checkGameStorage,
createBeatenGameEntry, createBeatenGameEntry,
getBeatenGameCount, getBeatenGameCount,
deleteBeatenGameId,
deleteBeatenGameNum,
checkGameStorageId,
}; };