2023-12-01 00:46:40 +01:00
const { SlashCommandBuilder , EmbedBuilder } = require ( 'discord.js' ) ;
2023-12-12 00:01:49 +01:00
const { getCoverURL , getGameJson , getCompanyInfo , getGenres , getFranchise , getReleaseDates } = require ( '../../igdbHelperFunctions.js' ) ;
2023-12-01 00:46:40 +01:00
module . exports = {
data : new SlashCommandBuilder ( )
. setName ( 'gamedetails' )
. setDescription ( 'Get the details of a given game.' )
. addStringOption ( option => option . setName ( 'gamename' ) . setDescription ( 'The name of the game.' ) )
. addNumberOption ( option => option . setName ( 'gameid' ) . setDescription ( 'The IGDB game id.' ) . setMinValue ( 0 ) ) ,
2023-12-07 00:40:07 +01:00
async execute ( interaction ) {
2024-03-28 22:12:28 +01:00
await interaction . deferReply ( ) ;
2023-12-01 00:46:40 +01:00
const gamename = interaction . options . getString ( 'gamename' ) ;
const gameid = interaction . options . getNumber ( 'gameid' ) ;
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 } ) ;
2023-12-01 00:46:40 +01:00
2023-12-06 00:47:36 +01:00
let body = '' ;
2023-12-01 00:46:40 +01:00
if ( gameid ) {
2023-12-06 00:47:36 +01:00
body = body . concat ( 'where id = ' , gameid , '; ' ) ;
2023-12-07 00:40:07 +01:00
body = body . concat ( 'fields *;' ) ;
2023-12-01 00:46:40 +01:00
} else if ( gamename ) {
2023-12-06 00:47:36 +01:00
body = body . concat ( 'search "' , gamename , '"; ' ) ;
2023-12-07 00:40:07 +01:00
body = body . concat ( 'fields *; limit 25; where (category = 0 | category = 4) & version_parent = null;' ) ;
2023-12-01 00:46:40 +01:00
}
2023-12-06 00:47:36 +01:00
const games = await getGameJson ( body ) ;
2023-12-27 22:10:22 +01:00
if ( ! games [ 0 ] ) return interaction . followUp ( { content : 'No game found.' , ephemeral : true } ) ;
2023-12-06 00:47:36 +01:00
2023-12-27 21:49:01 +01:00
await games . sort ( ( a , b ) => parseInt ( b . total _rating _count ) - parseInt ( a . total _rating _count ) ) ;
2023-12-06 00:47:36 +01:00
const game = games [ 0 ] ;
const coverUrl = await getCoverURL ( game . cover ) ;
2023-12-12 00:01:49 +01:00
let release _date ;
if ( game . first _release _date ) {
release _date = new Intl . DateTimeFormat ( 'en-GB' , { dateStyle : 'full' } ) . format ( game . first _release _date * 1000 ) ;
} else if ( game . release _dates ) {
release _date = await getReleaseDates ( game . release _dates [ 0 ] ) ;
}
2023-12-06 00:47:36 +01:00
2023-12-07 00:40:07 +01:00
const genres = [ ] ;
for ( const genreId of game . genres ) {
const genre = await getGenres ( genreId ) ;
genres . push ( genre ) ;
}
2023-12-12 00:01:49 +01:00
// Build Embed
2023-12-07 00:45:23 +01:00
const embed = new EmbedBuilder ( ) ;
embed . setColor ( 0x6441a5 ) ;
embed . setTitle ( ` ${ game . name } ` ) ;
embed . setURL ( ` ${ game . url } ` ) ;
2024-03-28 21:56:56 +01:00
if ( game . cover ) {
embed . setThumbnail ( ` ${ coverUrl } ` ) ;
}
if ( game . summary ) {
embed . addFields ( { name : 'Description' , value : ` ${ game . summary . length > 1024 ? game . summary . substring ( 0 , 1024 ) : game . summary } ` } ) ;
}
2023-12-07 00:48:50 +01:00
if ( game . involved _companies ) {
const companies = [ ] ;
for ( const company of game . involved _companies ) {
const info = await getCompanyInfo ( company ) ;
2023-12-12 00:01:49 +01:00
if ( info . name ) {
companies . push ( info . name ) ;
}
2023-12-07 00:48:50 +01:00
}
embed . addFields ( { name : 'Developers' , value : ` ${ companies . join ( ', ' ) } ` , inline : true } ) ;
}
2023-12-07 00:45:23 +01:00
embed . addFields ( { name : 'Release Date' , value : ` ${ release _date } ` , inline : true } ) ;
embed . addFields ( { name : 'Genres' , value : ` ${ genres . join ( ', ' ) } ` , inline : true } ) ;
if ( game . total _rating ) {
embed . addFields ( { name : 'Rating' , value : ` ${ game . total _rating . toFixed ( 0 ) } / 100, ${ game . total _rating _count } ratings ` , inline : true } ) ;
}
embed . setFooter ( { text : 'The Ochulus • 100 Games Challenge' , iconURL : interaction . client . user . avatarURL ( ) } ) ;
embed . setTimestamp ( ) ;
2023-12-06 00:47:36 +01:00
2023-12-07 00:40:07 +01:00
if ( game . franchises ) {
const franchise = await getFranchise ( game . franchises ) ;
embed . addFields ( { name : 'Franchise' , value : ` ${ franchise } ` , inline : true } ) ;
}
2023-12-12 00:01:49 +01:00
embed . addFields ( { name : 'ID' , value : ` ${ game . id } ` , inline : true } ) ;
2023-12-06 00:47:36 +01:00
return interaction . followUp ( { embeds : [ embed ] } ) ;
2023-12-01 00:46:40 +01:00
} ,
} ;