2024-02-11 22:32:55 +01:00
const { SlashCommandBuilder , EmbedBuilder } = require ( 'discord.js' ) ;
2024-07-16 22:08:07 +02:00
const { getUserRegistration , deletePlanningGameNum , checkGameStorageId , getRecentPlanningGameEntry , deletePlanningGameId , getPlayingGameCount , getBeatenGameCount , getPlanningGameCount , getPlanningGames } = require ( '../../databaseHelperFunctions.js' ) ;
2024-02-11 22:32:55 +01:00
const { getCoverURL , getGameJson } = require ( '../../igdbHelperFunctions.js' ) ;
module . exports = {
data : new SlashCommandBuilder ( )
. setName ( 'deleteplannedgame' )
. setDescription ( 'Delete a game that you were planning to play!' )
2024-07-16 22:08:07 +02:00
. addNumberOption ( option => option . setName ( 'currentgamenumber' ) . setDescription ( 'Index of the game to delete in the list of planned games.' ) . setMinValue ( 1 ) )
. addBooleanOption ( option => option . setName ( 'deleteallgames' ) . setDescription ( 'Deletes all the games currently planned.' ) ) ,
2024-02-11 22:32:55 +01:00
async execute ( interaction ) {
2024-03-28 22:12:28 +01:00
await interaction . deferReply ( ) ;
2024-02-11 22:32:55 +01:00
const beatGameNumber = interaction . options . getNumber ( 'currentgamenumber' ) ;
2024-07-16 22:08:07 +02:00
const deleteAllGames = interaction . options . getBoolean ( 'deleteallgames' ) ;
2024-02-11 22:32:55 +01:00
const userDatabaseEntry = await getUserRegistration ( interaction . user ) ;
let result ;
2024-07-16 22:08:07 +02:00
if ( deleteAllGames ) {
const databaseEntries = await getPlanningGames ( userDatabaseEntry . id ) ;
for ( let i = databaseEntries . length ; i > 0 ; i -- ) {
await deletePlanningGameNum ( i , userDatabaseEntry ) ;
}
const embed = new EmbedBuilder ( )
. setColor ( 0xFF0000 )
. setAuthor ( { name : ` ${ interaction . user . displayName } deleted all planned games! ` , iconURL : interaction . user . avatarURL ( ) } )
. setTitle ( 'Everything deleted!' )
. setFooter ( { text : 'The Ochulus • 100 Games Challenge' , iconURL : interaction . client . user . avatarURL ( ) } )
. setTimestamp ( ) ;
const beatNum = await getBeatenGameCount ( userDatabaseEntry ) ;
const planNum = await getPlanningGameCount ( userDatabaseEntry ) ;
const playNum = await getPlayingGameCount ( userDatabaseEntry ) ;
embed . addFields ( { name : 'Planned' , value : ` ${ planNum } game(s) ` , inline : true } ) ;
embed . addFields ( { name : 'Now Playing' , value : ` ${ playNum } game(s) ` , inline : true } ) ;
embed . addFields ( { name : 'Beaten' , value : ` ${ beatNum } /100 ( ${ 100 - beatNum } game(s) remaining) ` , inline : true } ) ;
return interaction . editReply ( { embeds : [ embed ] } ) ;
}
2024-02-11 22:32:55 +01:00
if ( beatGameNumber ) {
result = await deletePlanningGameNum ( beatGameNumber , userDatabaseEntry ) ;
}
else {
const recentGame = await getRecentPlanningGameEntry ( userDatabaseEntry . id ) ;
result = await deletePlanningGameId ( recentGame . id , userDatabaseEntry ) ;
}
if ( result ) {
const gameDatabaseEntry = await checkGameStorageId ( result . gameId ) ;
const body = ` where id = ${ gameDatabaseEntry . igdb _id } ; fields *; ` ;
const res = await getGameJson ( body ) ;
2024-03-28 22:12:28 +01:00
if ( ! res ) return interaction . editReply ( { content : 'No game found.' , ephemeral : true } ) ;
2024-02-11 22:32:55 +01:00
const game = res [ 0 ] ;
const beatNum = await getBeatenGameCount ( userDatabaseEntry ) ;
const planNum = await getPlanningGameCount ( userDatabaseEntry ) ;
const playNum = await getPlayingGameCount ( userDatabaseEntry ) ;
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. ` )
2024-02-11 22:32:55 +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-03-28 22:12:28 +01:00
return interaction . editReply ( { embeds : [ embed ] } ) ;
2024-02-11 22:32:55 +01:00
}
2024-03-28 22:12:28 +01:00
return interaction . editReply ( { content : 'Unable to delete entry / No entry found.' , ephemeral : true } ) ;
2024-02-11 22:32:55 +01:00
} ,
} ;