TheOchulus/commands/100-games/gameDetails.js

94 lines
3.8 KiB
JavaScript
Raw Normal View History

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)),
async execute(interaction) {
2023-12-01 00:46:40 +01:00
const gamename = interaction.options.getString('gamename');
const gameid = interaction.options.getNumber('gameid');
if (!gamename && !gameid) return interaction.reply('No gamename or gameid supplied, please supply an option to register a game!');
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
await interaction.reply(`Searching for ${gameid}...`);
body = body.concat('where id = ', gameid, '; ');
body = body.concat('fields *;');
2023-12-01 00:46:40 +01:00
} else if (gamename) {
2023-12-06 00:47:36 +01:00
await interaction.reply(`Searching for ${gamename}...`);
body = body.concat('search "', gamename, '"; ');
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);
if (!games[0]) return interaction.followUp('No game found.');
2023-12-12 00:01:49 +01:00
await games.sort((a, b) => {
return (a.total_rating_count == null) - (b.total_rating_count == null) || +(a.total_rating_count > b.total_rating_count) || -(a.total_rating_count < b.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
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}`);
embed.setThumbnail(`${coverUrl}`);
embed.addFields({ name: 'Description', value: `${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
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
},
};