From abd3c1031643cec4f36149bc887eb1f7957e0dfc Mon Sep 17 00:00:00 2001 From: baz Date: Wed, 14 May 2025 23:17:25 +0100 Subject: [PATCH] Update game details and random game embeds --- commands/100-games/gameDetails.js | 34 ++++++++++++++++++++++++++----- commands/100-games/randomgame.js | 34 ++++++++++++++++++++++++++----- igdbHelperFunctions.js | 28 ++++++++++++++++++++++++- 3 files changed, 85 insertions(+), 11 deletions(-) diff --git a/commands/100-games/gameDetails.js b/commands/100-games/gameDetails.js index 4a3c1e2..77440d3 100644 --- a/commands/100-games/gameDetails.js +++ b/commands/100-games/gameDetails.js @@ -1,5 +1,5 @@ const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); -const { getCoverURL, getGameJson, getGenres, getFranchise, getReleaseDates, getInvolvedCompanies, getCompanies } = require('../../igdbHelperFunctions.js'); +const { getCoverURL, getGameJson, getGenres, getFranchise, getReleaseDates, getInvolvedCompanies, getCompanies, getTimeToBeat } = require('../../igdbHelperFunctions.js'); module.exports = { data: new SlashCommandBuilder() @@ -87,11 +87,10 @@ module.exports = { const publishers = []; for (const company of companies) { - if (company.developed) { if (company.developed.find(item => item === game.id)) { - developers.push(company.name); + developers.push(`[${company.name}](${company.url})`); } } @@ -99,7 +98,7 @@ module.exports = { if (company.published) { if (company.published.find(item => item === game.id)) { - publishers.push(company.name); + publishers.push(`[${company.name}](${company.url})`); } } } @@ -131,7 +130,32 @@ module.exports = { if (game.franchises) { const franchise = await getFranchise(game.franchises); - embed.addFields({ name: 'Franchise', value: `${franchise}`, inline: true }); + embed.addFields({ name: 'Franchise', value: `[${franchise.name}](${franchise.url})`, inline: true }); + } + + const gameTimeToBeat = await getTimeToBeat(game.id); + + if (gameTimeToBeat) { + const timings = []; + + if (gameTimeToBeat.hastily) { + const hours = Math.floor(gameTimeToBeat.hastily / 3600); + timings.push(`Hastily: ${hours}hr`); + } + + if (gameTimeToBeat.normally) { + const hours = Math.floor(gameTimeToBeat.normally / 3600); + timings.push(`Normally: ${hours}hr`); + } + + if (gameTimeToBeat.completely) { + const hours = Math.floor(gameTimeToBeat.completely / 3600); + timings.push(`Completely: ${hours}hr`); + } + + if (timings.length > 0) { + embed.addFields({ name: 'Time to Beat', value: `${timings.join('\n')}`, inline: true }); + } } embed.addFields({ name: 'ID', value: `${game.id}`, inline: true }); diff --git a/commands/100-games/randomgame.js b/commands/100-games/randomgame.js index 1c603a6..736391b 100644 --- a/commands/100-games/randomgame.js +++ b/commands/100-games/randomgame.js @@ -1,5 +1,5 @@ const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); -const { getCoverURL, getGameJson, getInvolvedCompanies, getCompanies, getGenres, getReleaseDates, getFranchise } = require('../../igdbHelperFunctions.js'); +const { getCoverURL, getGameJson, getInvolvedCompanies, getCompanies, getGenres, getReleaseDates, getFranchise, getTimeToBeat } = require('../../igdbHelperFunctions.js'); module.exports = { data: new SlashCommandBuilder() @@ -77,11 +77,10 @@ module.exports = { const publishers = []; for (const company of companies) { - if (company.developed) { if (company.developed.find(item => item === game.id)) { - developers.push(company.name); + developers.push(`[${company.name}](${company.url})`); } } @@ -89,7 +88,7 @@ module.exports = { if (company.published) { if (company.published.find(item => item === game.id)) { - publishers.push(company.name); + publishers.push(`[${company.name}](${company.url})`); } } } @@ -120,7 +119,32 @@ module.exports = { if (game.franchises) { const franchise = await getFranchise(game.franchises); - embed.addFields({ name: 'Franchise', value: `${franchise}`, inline: true }); + embed.addFields({ name: 'Franchise', value: `[${franchise.name}](${franchise.url})`, inline: true }); + } + + const gameTimeToBeat = await getTimeToBeat(game.id); + + if (gameTimeToBeat) { + const timings = []; + + if (gameTimeToBeat.hastily) { + const hours = Math.floor(gameTimeToBeat.hastily / 3600); + timings.push(`Hastily: ${hours}hr`); + } + + if (gameTimeToBeat.normally) { + const hours = Math.floor(gameTimeToBeat.normally / 3600); + timings.push(`Normally: ${hours}hr`); + } + + if (gameTimeToBeat.completely) { + const hours = Math.floor(gameTimeToBeat.completely / 3600); + timings.push(`Completely: ${hours}hr`); + } + + if (timings.length > 0) { + embed.addFields({ name: 'Time to Beat', value: `${timings.join('\n')}`, inline: true }); + } } embed.addFields({ name: 'ID', value: `${game.id}`, inline: true }); diff --git a/igdbHelperFunctions.js b/igdbHelperFunctions.js index 88c014b..08b6d71 100644 --- a/igdbHelperFunctions.js +++ b/igdbHelperFunctions.js @@ -233,7 +233,32 @@ async function getFranchise(id) { console.error(err); }); - return franchise.name; + return franchise; +} + +async function getTimeToBeat(id) { + + let gameTimeToBeats; + + await fetch( + 'https://api.igdb.com/v4/game_time_to_beats', + { method: 'POST', + headers: { + 'Accept': 'application/json', + 'Client-ID': `${process.env.igdbClientId}`, + 'Authorization': `Bearer ${process.env.igdbAccessToken}`, + }, + body: `where game_id = ${id}; fields *;`, + }) + .then(response => response.json()) + .then(response => { + gameTimeToBeats = response[0]; + }) + .catch(err => { + console.error(err); + }); + + return gameTimeToBeats; } module.exports = { @@ -246,4 +271,5 @@ module.exports = { getCompanyInfo, getGenres, getFranchise, + getTimeToBeat, }; \ No newline at end of file