TheOchulus/commands/100-games/recentbeat.js

48 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-12-27 21:43:40 +01:00
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getCoverURL, getGameJson } = require('../../igdbHelperFunctions.js');
const { getUserRegistration, getRecentBeatenGameEntry, getBeatenGameCount, getPlanningGameCount, getPlayingGameCount } = require('../../databaseHelperFunctions.js');
2023-12-27 21:43:40 +01:00
module.exports = {
data: new SlashCommandBuilder()
.setName('recentbeat')
.setDescription('Get the most recent game you have beat.')
.addUserOption(option => option.setName('user').setDescription('The user to check')),
async execute(interaction) {
let user = interaction.user;
const userOption = interaction.options.getUser('user');
if (userOption) {
user = userOption;
}
const userDatabaseEntry = await getUserRegistration(user);
if (!userDatabaseEntry) return interaction.reply({ content: `Issue checking registration with "${interaction.user.username}".`, ephemeral: true });
const gameDatabaseEntry = await getRecentBeatenGameEntry(userDatabaseEntry.id);
2023-12-27 21:43:40 +01:00
if (!gameDatabaseEntry) return interaction.reply({ content: 'No game found.', ephemeral: true });
const body = `where id = ${ gameDatabaseEntry.igdb_id }; fields *;`;
const res = await getGameJson(body);
if (!res) return interaction.reply({ content: 'No game found.', ephemeral: true });
const game = res[0];
const beatNum = await getBeatenGameCount(userDatabaseEntry);
const planNum = await getPlanningGameCount(userDatabaseEntry);
const playNum = await getPlayingGameCount(userDatabaseEntry);
2023-12-27 21:43:40 +01:00
const embed = new EmbedBuilder()
.setColor(0xFFD700)
.setAuthor({ name: `${user.displayName}'s most recent beat game!`, iconURL: user.avatarURL() })
.setTitle(game.name)
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })
.setTimestamp()
.setDescription(`${interaction.user.displayName} has ${planNum} games planned, they are playing ${playNum} games, they have beaten ${beatNum} games, they have ${100 - beatNum} games remaining.`);
if (game.cover) {
const coverUrl = await getCoverURL(game.cover);
embed.setThumbnail(`${coverUrl}`);
}
2023-12-27 21:43:40 +01:00
return interaction.reply({ embeds: [embed] });
},
};