Compare commits

...

2 Commits

2 changed files with 121 additions and 7 deletions

View File

@ -1,5 +1,5 @@
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getCoverURL, getGameJson } = require('../../igdbHelperFunctions.js'); const { getCoverURL, getGameJson, getCompanyInfo, getGenres, getFranchise } = require('../../igdbHelperFunctions.js');
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
@ -7,6 +7,7 @@ module.exports = {
.setDescription('Get the details of a given game.') .setDescription('Get the details of a given game.')
.addStringOption(option => option.setName('gamename').setDescription('The name of the game.')) .addStringOption(option => option.setName('gamename').setDescription('The name of the game.'))
.addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)), .addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)),
async execute(interaction) { async execute(interaction) {
const gamename = interaction.options.getString('gamename'); const gamename = interaction.options.getString('gamename');
const gameid = interaction.options.getNumber('gameid'); const gameid = interaction.options.getNumber('gameid');
@ -18,12 +19,12 @@ module.exports = {
if (gameid) { if (gameid) {
await interaction.reply(`Searching for ${gameid}...`); await interaction.reply(`Searching for ${gameid}...`);
body = body.concat('where id = ', gameid, '; '); body = body.concat('where id = ', gameid, '; ');
body = body.concat('fields *;');
} else if (gamename) { } else if (gamename) {
await interaction.reply(`Searching for ${gamename}...`); await interaction.reply(`Searching for ${gamename}...`);
body = body.concat('search "', gamename, '"; '); body = body.concat('search "', gamename, '"; ');
}
body = body.concat('fields *; limit 25; where (category = 0 | category = 4) & version_parent = null;'); body = body.concat('fields *; limit 25; where (category = 0 | category = 4) & version_parent = null;');
}
const games = await getGameJson(body); const games = await getGameJson(body);
@ -37,21 +38,37 @@ module.exports = {
const release_date = new Intl.DateTimeFormat('en-GB', { dateStyle: 'full' }).format(game.first_release_date * 1000); const release_date = new Intl.DateTimeFormat('en-GB', { dateStyle: 'full' }).format(game.first_release_date * 1000);
const companies = [];
for (const company of game.involved_companies) {
const info = await getCompanyInfo(company);
companies.push(info.name);
}
const genres = [];
for (const genreId of game.genres) {
const genre = await getGenres(genreId);
genres.push(genre);
}
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor(0x6441a5) .setColor(0x6441a5)
.setTitle(`${game.name}`) .setTitle(`${game.name}`)
.setURL(`${game.url}`) .setURL(`${game.url}`)
.setThumbnail(`${coverUrl}`) .setThumbnail(`${coverUrl}`)
.addFields({ name: 'Description', value: `${game.summary}` }) .addFields({ name: 'Description', value: `${game.summary}` })
.addFields({ name: 'Developers', value: `${game.involved_companies.join(', ')}`, inline: true }) .addFields({ name: 'Developers', value: `${companies.join(', ')}`, inline: true })
.addFields({ name: 'Release Date', value: `${release_date}`, inline: true }) .addFields({ name: 'Release Date', value: `${release_date}`, inline: true })
.addFields({ name: 'Genres', value: `${game.genres.join(', ')}`, inline: true }) .addFields({ name: 'Genres', value: `${genres.join(', ')}`, inline: true })
.addFields({ name: 'Rating', value: `${game.total_rating.toFixed(0)} / 100, ${game.total_rating_count } ratings`, inline: true }) .addFields({ name: 'Rating', value: `${game.total_rating.toFixed(0)} / 100, ${game.total_rating_count } ratings`, inline: true })
.addFields({ name: 'Franchise', value: `${game.franchises}`, inline: true })
.addFields({ name: 'ID', value: `${game.id}`, inline: true }) .addFields({ name: 'ID', value: `${game.id}`, inline: true })
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() }) .setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })
.setTimestamp(); .setTimestamp();
if (game.franchises) {
const franchise = await getFranchise(game.franchises);
embed.addFields({ name: 'Franchise', value: `${franchise}`, inline: true });
}
return interaction.followUp({ embeds: [embed] }); return interaction.followUp({ embeds: [embed] });
}, },
}; };

View File

@ -96,9 +96,106 @@ async function getReleaseDates(id) {
return date; return date;
} }
async function getCompanyInfo(id) {
let involved_company;
await fetch(
'https://api.igdb.com/v4/involved_companies',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${id}; fields *;`,
})
.then(response => response.json())
.then(response => {
involved_company = response[0];
})
.catch(err => {
return console.error(err);
});
let developer;
await fetch(
'https://api.igdb.com/v4/companies',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${involved_company.company}; fields *;`,
})
.then(response => response.json())
.then(response => {
developer = response[0];
})
.catch(err => {
console.error(err);
});
return developer;
}
async function getGenres(id) {
let genre;
await fetch(
'https://api.igdb.com/v4/genres',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${id}; fields *;`,
})
.then(response => response.json())
.then(response => {
genre = response[0];
})
.catch(err => {
console.error(err);
});
return genre.name;
}
async function getFranchise(id) {
let franchise;
await fetch(
'https://api.igdb.com/v4/franchises',
{ method: 'POST',
headers: {
'Accept': 'application/json',
'Client-ID': `${process.env.igdbClientId}`,
'Authorization': `Bearer ${process.env.igdbAccessToken}`,
},
body: `where id = ${id}; fields *;`,
})
.then(response => response.json())
.then(response => {
franchise = response[0];
})
.catch(err => {
console.error(err);
});
return franchise.name;
}
module.exports = { module.exports = {
getCoverURL, getCoverURL,
getPlatformID, getPlatformID,
getGameJson, getGameJson,
getReleaseDates, getReleaseDates,
getCompanyInfo,
getGenres,
getFranchise,
}; };