2023-12-30 02:56:05 +01:00
const { SlashCommandBuilder , EmbedBuilder } = require ( 'discord.js' ) ;
2024-02-11 22:32:47 +01:00
const { getUserRegistration , deleteBeatenGameNum , checkGameStorageId , getRecentBeatenGameEntry , deleteBeatenGameId , getBeatenGameCount , getPlanningGameCount , getPlayingGameCount } = require ( '../../databaseHelperFunctions.js' ) ;
2023-12-30 02:56:05 +01:00
const { getCoverURL , getGameJson } = require ( '../../igdbHelperFunctions.js' ) ;
2023-12-19 21:42:54 +01:00
module . exports = {
data : new SlashCommandBuilder ( )
2024-02-11 22:32:47 +01:00
. setName ( 'deletebeatengame' )
2023-12-19 21:42:54 +01:00
. setDescription ( 'Delete a game that you have beaten from the 100 game challenge!' )
. 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 ) {
2024-01-01 00:30:06 +01:00
await interaction . reply ( { content : 'Searching for user...' , ephemeral : true } ) ;
2023-12-19 21:42:54 +01:00
const beatGameNumber = interaction . options . getNumber ( 'beatgamenumber' ) ;
const userDatabaseEntry = await getUserRegistration ( interaction . user ) ;
let result ;
2023-12-30 02:31:56 +01:00
if ( beatGameNumber ) {
2023-12-19 21:42:54 +01:00
result = await deleteBeatenGameNum ( beatGameNumber , userDatabaseEntry ) ;
}
2023-12-30 02:36:41 +01:00
else {
2024-02-11 20:42:07 +01:00
const recentGame = await getRecentBeatenGameEntry ( userDatabaseEntry . id ) ;
2023-12-30 02:55:51 +01:00
result = await deleteBeatenGameId ( recentGame . id , userDatabaseEntry ) ;
2023-12-30 02:36:41 +01:00
}
2023-12-19 21:42:54 +01:00
if ( result ) {
2023-12-30 02:56:05 +01:00
const gameDatabaseEntry = await checkGameStorageId ( result . gameId ) ;
const body = ` where id = ${ gameDatabaseEntry . igdb _id } ; fields *; ` ;
const res = await getGameJson ( body ) ;
2024-01-01 00:30:06 +01:00
if ( ! res ) return interaction . followUp ( { content : 'No game found.' , ephemeral : true } ) ;
2023-12-30 02:56:05 +01:00
const game = res [ 0 ] ;
2024-02-11 22:32:47 +01:00
const beatNum = await getBeatenGameCount ( userDatabaseEntry ) ;
const planNum = await getPlanningGameCount ( userDatabaseEntry ) ;
const playNum = await getPlayingGameCount ( userDatabaseEntry ) ;
2023-12-30 02:56:05 +01:00
const embed = new EmbedBuilder ( )
. setColor ( 0xFF0000 )
. setAuthor ( { name : ` ${ interaction . user . displayName } deleted a game! ` , iconURL : interaction . user . avatarURL ( ) } )
. setTitle ( ` ${ game . name } deleted! ` )
2024-03-18 21:47:32 +01:00
. setDescription ( ` ${ interaction . user . displayName } has ${ planNum } game(s) planned, they are playing ${ playNum } game(s), they have beaten ${ beatNum } game(s), they have ${ 100 - beatNum } game(s) remaining. ` )
2023-12-30 02:56:05 +01:00
. setFooter ( { text : 'The Ochulus • 100 Games Challenge' , iconURL : interaction . client . user . avatarURL ( ) } )
. setTimestamp ( ) ;
2024-02-14 20:11:28 +01:00
if ( game . cover ) {
const coverUrl = await getCoverURL ( game . cover ) ;
embed . setThumbnail ( ` ${ coverUrl } ` ) ;
}
2024-01-01 00:30:06 +01:00
return interaction . followUp ( { embeds : [ embed ] } ) ;
2023-12-19 21:42:54 +01:00
}
2024-01-01 00:30:06 +01:00
return interaction . followUp ( { content : 'Unable to delete entry / No entry found.' , ephemeral : true } ) ;
2023-12-19 21:42:54 +01:00
} ,
2023-12-30 02:56:05 +01:00
} ;