2024-02-11 21:23:19 +01:00
const { SlashCommandBuilder , EmbedBuilder } = require ( 'discord.js' ) ;
const { getCoverURL , getGameJson } = require ( '../../igdbHelperFunctions.js' ) ;
const { checkGameStorage , getUserRegistration , createPlanningGameEntry , getBeatenGameCount , getPlanningGameCount , getPlayingGameCount } = require ( '../../databaseHelperFunctions.js' ) ;
module . exports = {
data : new SlashCommandBuilder ( )
. setName ( 'plangame' )
. setDescription ( 'Log a game that you plan beat towards the 100 game challenge!' )
. addStringOption ( option => option . setName ( 'gamename' ) . setDescription ( 'The name of the game.' ) . setRequired ( true ) )
2024-07-13 18:10:13 +02:00
. addNumberOption ( option => option . setName ( 'gameid' ) . setDescription ( 'The IGDB game id.' ) . setMinValue ( 0 ) )
. addStringOption ( option => option . setName ( 'date' ) . setDescription ( 'The date to be logged. (YYYY/MM/DD)' ) ) ,
2024-02-11 21:23:19 +01:00
async execute ( interaction ) {
2024-03-28 22:12:28 +01:00
await interaction . deferReply ( ) ;
2024-02-11 21:23:19 +01:00
const userDatabaseEntry = await getUserRegistration ( interaction . user ) ;
2024-03-28 22:12:28 +01:00
if ( ! userDatabaseEntry ) return interaction . editReply ( { content : ` Issue checking registration with " ${ interaction . user . username } ". ` , ephemeral : true } ) ;
2024-02-11 21:23:19 +01:00
const gamename = interaction . options . getString ( 'gamename' ) ;
const gameid = interaction . options . getNumber ( 'gameid' ) ;
2024-07-13 18:10:13 +02:00
const date = interaction . options . getString ( 'date' ) ;
2024-02-11 21:23:19 +01:00
2024-03-28 22:12:28 +01:00
if ( ! gamename && ! gameid ) return interaction . editReply ( { content : 'No gamename or gameid supplied, please supply an option to register a game!' , ephemeral : true } ) ;
2024-02-11 21:23:19 +01:00
let body = '' ;
if ( gameid ) {
body = body . concat ( 'where id = ' , gameid , '; ' ) ;
} else if ( gamename ) {
body = body . concat ( 'search "' , gamename , '"; ' ) ;
body = body . concat ( 'limit 25; where (category = 0 | category = 4 | category = 8 | category = 9) & version_parent = null;' ) ;
}
body = body . concat ( 'fields *;' ) ;
2024-02-14 20:48:39 +01:00
let res = await getGameJson ( body ) ;
res = res . filter ( entry => entry . status !== 6 ) ;
res . sort ( ( a , b ) => parseInt ( b . total _rating _count ) - parseInt ( a . total _rating _count ) ) ;
2024-02-11 21:23:19 +01:00
2024-03-28 22:12:28 +01:00
if ( ! res [ 0 ] ) return interaction . editReply ( { content : 'No game found for the options supplied.' , ephemeral : true } ) ;
2024-02-11 21:23:19 +01:00
const game = res [ 0 ] ;
const gameDatabaseEntry = await checkGameStorage ( game ) ;
2024-07-13 18:10:13 +02:00
let gameDate = new Date ( ) ;
if ( date ) {
const parsedDate = new Date ( date ) ;
if ( ! isNaN ( parsedDate . getTime ( ) ) ) {
gameDate = parsedDate ;
}
else {
gameDate = new Date ( ) ;
}
}
await createPlanningGameEntry ( userDatabaseEntry , gameDatabaseEntry , gameDate ) ;
2024-02-11 21:23:19 +01:00
const beatNum = await getBeatenGameCount ( userDatabaseEntry ) ;
const planNum = await getPlanningGameCount ( userDatabaseEntry ) ;
const playNum = await getPlayingGameCount ( userDatabaseEntry ) ;
const embed = new EmbedBuilder ( )
2024-02-11 22:06:43 +01:00
. setColor ( 0x43ABEC )
2024-02-11 21:23:19 +01:00
. setAuthor ( { name : ` ${ interaction . user . displayName } is planning to beat a game! ` , iconURL : interaction . user . avatarURL ( ) } )
. setTitle ( ` ${ game . name } ! ` )
. setFooter ( { text : 'The Ochulus • 100 Games Challenge' , iconURL : interaction . client . user . avatarURL ( ) } )
. setTimestamp ( ) ;
2024-03-18 21:47:32 +01:00
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 } ) ;
2024-03-01 18:03:30 +01:00
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
await interaction . editReply ( { embeds : [ embed ] } ) ;
2024-02-11 21:23:19 +01:00
} ,
} ;