2023-11-28 00:40:52 +01:00
|
|
|
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
|
2023-12-01 00:37:39 +01:00
|
|
|
const { getGameJson } = require('../../igdbHelperFunctions.js');
|
2023-11-28 00:40:52 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
data: new SlashCommandBuilder()
|
2023-12-01 00:19:08 +01:00
|
|
|
.setName('searchgames')
|
2023-11-28 00:40:52 +01:00
|
|
|
.setDescription('Searches the igdb database for matching games.')
|
|
|
|
.addStringOption(option => option.setName('gamename').setDescription('The name of the game').setRequired(true)),
|
|
|
|
|
|
|
|
async execute(interaction) {
|
|
|
|
const gamename = interaction.options.getString('gamename');
|
2024-03-28 22:12:28 +01:00
|
|
|
await interaction.deferReply();
|
2023-11-28 00:40:52 +01:00
|
|
|
|
2023-11-28 23:06:19 +01:00
|
|
|
let games = await searchGamesWithMinimumReview(gamename);
|
2023-11-28 00:40:52 +01:00
|
|
|
|
2023-11-28 23:06:19 +01:00
|
|
|
if (games.length == 0) games = await searchGamesWithoutMinimumReview(gamename);
|
|
|
|
|
2024-03-28 22:12:28 +01:00
|
|
|
if (games.length == 0) return interaction.editReply({ content: 'No games found.', ephemeral: true });
|
2023-11-28 00:40:52 +01:00
|
|
|
|
|
|
|
await games.sort((a, b) => parseInt(b.total_rating_count) - parseInt(a.total_rating_count));
|
|
|
|
|
|
|
|
let description = '';
|
|
|
|
|
|
|
|
for (const game of games) {
|
2024-02-14 23:29:53 +01:00
|
|
|
if (game.first_release_date && (game.category == 0 || game.category == 4 || game.category == 8 || game.category == 9)) {
|
2023-11-28 23:05:02 +01:00
|
|
|
const release_date = new Intl.DateTimeFormat('en-GB', { dateStyle: 'long' }).format(game.first_release_date * 1000);
|
2023-11-28 00:40:52 +01:00
|
|
|
description = description.concat(`- **${game.name}** (*${release_date}*) - ID: ${game.id} \n`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (description == '') description = 'No games found.';
|
|
|
|
|
|
|
|
const embed = new EmbedBuilder()
|
|
|
|
.setTitle(`"${gamename}" Search Results`)
|
|
|
|
.setDescription(`${description.slice(0, 1998)}`)
|
|
|
|
.setColor(0x6441a5);
|
|
|
|
|
2024-03-28 22:12:28 +01:00
|
|
|
await interaction.editReply({ embeds: [embed] });
|
2023-11-28 00:40:52 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2023-11-28 23:05:29 +01:00
|
|
|
async function searchGamesWithMinimumReview(gamename) {
|
|
|
|
let body = `search "${gamename}"; `;
|
2023-12-27 23:28:09 +01:00
|
|
|
body = await body.concat('fields *; limit 25; where (category = 0 | category = 4 | category = 8 | category = 9) & version_parent = null & total_rating_count > 0;');
|
2023-11-28 23:05:29 +01:00
|
|
|
|
|
|
|
const games = await getGameJson(body);
|
|
|
|
|
|
|
|
return games;
|
|
|
|
}
|
|
|
|
|
2023-11-28 23:05:43 +01:00
|
|
|
async function searchGamesWithoutMinimumReview(gamename) {
|
|
|
|
let body = `search "${gamename}"; `;
|
2024-02-14 23:29:53 +01:00
|
|
|
body = await body.concat('fields *; limit 25; where (category = 0 | category = 4 | category = 8 | category = 9) & version_parent = null;');
|
2023-11-28 23:05:43 +01:00
|
|
|
|
|
|
|
const games = await getGameJson(body);
|
|
|
|
|
|
|
|
return games;
|
2023-11-28 00:40:52 +01:00
|
|
|
}
|