diff --git a/commands/100-games/globalbeatlist.js b/commands/100-games/globalbeatlist.js new file mode 100644 index 0000000..b676ee2 --- /dev/null +++ b/commands/100-games/globalbeatlist.js @@ -0,0 +1,41 @@ +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); +const { getAllBeatenGames, checkGameStorageId, getUserFromId } = require('../../databaseHelperFunctions.js'); +const { getGameJson } = require('../../igdbHelperFunctions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('globalbeatlist') + .setDescription('Show a list of all games beaten for the 100 games challenge in chronological order.'), + async execute(interaction) { + await interaction.reply({ content: 'Searching for games...', ephemeral: true }); + + const user = interaction.user; + + const beatenGamesDatabaseEntries = await getAllBeatenGames(); + let desc = ''; + + if (!beatenGamesDatabaseEntries || beatenGamesDatabaseEntries.length == 0) { + desc = 'No games beaten yet.'; + } else { + 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]; + const userentry = await getUserFromId(beatenGamesDatabaseEntries[i].userId); + const date = beatenGamesDatabaseEntries[i].updatedAt.toLocaleDateString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/'); + desc = desc.concat('**', date, '**: \t', game.name, ' \t*(', userentry.username, ')*\n'); + } + } + + const embed = new EmbedBuilder() + .setColor(0x6441a5) + .setAuthor({ name: `${user.displayName}`, iconURL: user.avatarURL() }) + .setThumbnail(interaction.client.user.avatarURL()) + .setTitle('Global Beat List') + .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 2fd2a51..4690b9b 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -35,6 +35,17 @@ async function getUserRegistration(user) { return null; } +async function getUserFromId(id) { + const u = await Users.findOne({ where: { id: id } }) + .catch((err) => { + console.log(err); + }); + + if (u) return u; + + return null; +} + async function checkGameStorage(game) { let g = await Games.findOne({ where: { igdb_id: game.id, name: game.name } }) .catch((err) => { @@ -292,6 +303,17 @@ async function getGames(id, status) { return false; } +async function getAllBeatenGames() { + const gameEntries = await LoggedGames.findAll({ where: { status: 'beat' }, order: [ [ 'updatedAt', 'ASC' ]] }) + .catch((err) => { + console.log(err); + }); + + if (gameEntries) return gameEntries; + + return false; +} + async function backupDatabase() { const date = new Date().toJSON().slice(0, 10); @@ -306,6 +328,7 @@ async function backupDatabase() { module.exports = { checkUserRegistration, getUserRegistration, + getUserFromId, checkGameStorage, createPlanningGameEntry, createPlayingGameEntry, @@ -331,5 +354,6 @@ module.exports = { getPlayingGames, getBeatenGames, getGames, + getAllBeatenGames, backupDatabase, }; \ No newline at end of file