TheOchulus/commands/100-games/beatGame.js

58 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-11-27 00:02:34 +01:00
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getCoverURL, getGameJson } = require('../../igdbHelperFunctions.js');
const { checkGameStorage, getUserRegistration, createBeatenGameEntry, getBeatenGameCount } = require('../../databaseHelperFunctions.js');
2023-11-18 01:35:28 +01:00
2023-11-27 00:02:34 +01:00
module.exports = {
data: new SlashCommandBuilder()
.setName('beatgame')
.setDescription('Log a game that you have beat towards the 100 game challenge!')
.addStringOption(option => option.setName('gamename').setDescription('The name of the game.'))
2023-12-27 22:34:10 +01:00
.addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)),
2023-11-27 00:02:34 +01:00
async execute(interaction) {
2023-12-19 00:53:52 +01:00
const userDatabaseEntry = await getUserRegistration(interaction.user);
2023-12-27 22:10:22 +01:00
if (!userDatabaseEntry) return interaction.reply({ content: `Issue checking registration with "${interaction.user.username}".`, ephemeral: true });
2023-12-19 00:53:52 +01:00
2023-11-27 00:02:34 +01:00
const gamename = interaction.options.getString('gamename');
const gameid = interaction.options.getNumber('gameid');
2023-11-28 23:04:08 +01:00
2023-12-27 22:10:22 +01:00
if (!gamename && !gameid) return interaction.reply({ content: 'No gamename or gameid supplied, please supply an option to register a game!', ephemeral: true });
2023-11-28 23:04:08 +01:00
let body = '';
2023-11-27 00:02:34 +01:00
if (gameid) {
2023-11-28 23:04:08 +01:00
body = body.concat('where id = ', gameid, '; ');
2023-11-27 00:02:34 +01:00
} else if (gamename) {
2023-11-28 23:04:08 +01:00
body = body.concat('search "', gamename, '"; ');
2023-12-19 20:47:26 +01:00
body = body.concat('limit 25; where (category = 0 | category = 4) & version_parent = null;');
2023-11-27 00:02:34 +01:00
}
2023-11-28 23:04:08 +01:00
2023-11-27 00:02:34 +01:00
body = body.concat('fields *;');
2023-11-28 23:04:08 +01:00
const res = await getGameJson(body);
2023-12-27 22:10:22 +01:00
if (!res[0]) return interaction.reply({ content: 'No game found for the options supplied.', ephemeral: true});
2023-11-28 23:04:08 +01:00
2023-12-19 20:47:26 +01:00
const game = res[0];
const release_date = game.first_release_date;
2023-12-27 22:10:22 +01:00
if (!release_date || (release_date * 1000) > Date.now()) return interaction.reply({ content: `${game.name} is not yet released.`, ephemeral: true });
2023-12-19 20:47:26 +01:00
const gameDatabaseEntry = await checkGameStorage(game);
2023-12-27 22:10:22 +01:00
if (!(await createBeatenGameEntry(userDatabaseEntry, gameDatabaseEntry))) return interaction.reply({ content: `${game.name} already beaten.`, ephemeral: true });
2023-12-19 00:53:52 +01:00
const num = await getBeatenGameCount(userDatabaseEntry);
2023-12-19 20:47:26 +01:00
const coverUrl = await getCoverURL(game.cover);
2023-11-28 23:04:08 +01:00
2023-11-27 00:02:34 +01:00
const embed = new EmbedBuilder()
.setColor(0xFFD700)
.setAuthor({ name: `${interaction.user.displayName} beat a new game!`, iconURL: interaction.user.avatarURL() })
2023-12-19 20:47:26 +01:00
.setTitle(`${game.name} beaten!`)
2023-11-27 00:02:34 +01:00
.setThumbnail(`${coverUrl}`)
2023-12-19 00:53:52 +01:00
.setDescription(`${interaction.user.displayName} has beaten ${num} games, they have ${100 - num} games remaining.`)
2023-11-27 00:02:34 +01:00
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })
.setTimestamp();
2023-11-28 23:04:08 +01:00
2023-11-27 00:02:34 +01:00
return interaction.reply({ embeds: [embed] });
},
};