diff --git a/commands/100-games/beatlist.js b/commands/100-games/beatlist.js new file mode 100644 index 0000000..16f605e --- /dev/null +++ b/commands/100-games/beatlist.js @@ -0,0 +1,46 @@ +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); +const { getUserRegistration, getGames, checkGameStorageId } = require('../../databaseHelperFunctions.js'); +const { getGameJson } = require('../../igdbHelperFunctions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('beatlist') + .setDescription('Show the list of games you have beaten.') + .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 "${user.username}".`, ephemeral: true }); + + await interaction.reply({ content: `Searching for ${user.username}...`, ephemeral: true }); + + const beatenGamesDatabaseEntries = await getGames(userDatabaseEntry.id); + if (!beatenGamesDatabaseEntries || beatenGamesDatabaseEntries.length == 0) return interaction.followUp({ content: 'No games found.', ephemeral: true }); + + let desc = ''; + + for (let i = 0; i < beatenGamesDatabaseEntries.length; i++) { + const gameid = await checkGameStorageId(beatenGamesDatabaseEntries[i].gameId); + const res = await getGameJson(`where id = ${ gameid.igdb_id }; fields *;`); + const game = res[0]; + + desc = desc.concat('#', (i + 1), ' \t', game.name, '\n'); + } + + const embed = new EmbedBuilder() + .setColor(0x6441a5) + .setAuthor({ name: `${user.displayName}`, iconURL: user.avatarURL() }) + .setTitle(`${interaction.user.username}'s beaten games`) + .setDescription(desc) + .setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() }) + .setTimestamp(); + + return interaction.followUp({ embeds: [embed] }); + }, +}; \ No newline at end of file diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index 9088eb0..75cc403 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -172,6 +172,17 @@ async function getRecentGameEntry(userId) { return false; } +async function getGames(id) { + const beatenGameEntry = await BeatenGames.findAll({ where: { userId: id } }) + .catch((err) => { + console.log(err); + }); + + if (beatenGameEntry) return beatenGameEntry; + + return false; +} + module.exports = { checkUserRegistration, getUserRegistration, @@ -183,4 +194,5 @@ module.exports = { checkGameStorageId, getLeaderboardEntries, getRecentGameEntry, + getGames, }; \ No newline at end of file