From 8a5d55920b9b673600e7db76a32ddbe97e26cba3 Mon Sep 17 00:00:00 2001 From: baz Date: Sat, 13 Jul 2024 22:03:16 +0100 Subject: [PATCH] Add changelog command --- commands/100-games/changelog.js | 50 +++++++++++++++++++++++++++++++++ databaseHelperFunctions.js | 12 ++++++++ 2 files changed, 62 insertions(+) create mode 100644 commands/100-games/changelog.js diff --git a/commands/100-games/changelog.js b/commands/100-games/changelog.js new file mode 100644 index 0000000..84a5452 --- /dev/null +++ b/commands/100-games/changelog.js @@ -0,0 +1,50 @@ +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); +const { getUserRegistration, getChangelog, checkGameStorageId } = require('../../databaseHelperFunctions.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('changelog') + .setDescription('Show your recent activity') + .addUserOption(option => option.setName('user').setDescription('The user to check')), + async execute(interaction) { + await interaction.deferReply(); + + let user = interaction.user; + const userOption = interaction.options.getUser('user'); + + if (userOption) { + user = userOption; + } + + const userDatabaseEntry = await getUserRegistration(user); + if (!userDatabaseEntry) return interaction.editReply({ content: `Issue checking registration with "${user.username}".`, ephemeral: true }); + + const changelogEntries = await getChangelog(userDatabaseEntry.id); + let desc = ''; + + for (const entry of changelogEntries) { + const game = await checkGameStorageId(entry.gameId); + + if (entry.oldStatus && entry.newStatus) { + // Status changed + desc = desc.concat(`:arrow_right: moved **${game.name}** from **${entry.oldStatus}** to **${entry.newStatus}** *(${entry.createdAt.toLocaleDateString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/')})*\n`); + } else if (!entry.oldStatus && entry.newStatus) { + // Created entry + desc = desc.concat(`:white_check_mark: added **${game.name}** to **${entry.newStatus}** *(${entry.createdAt.toLocaleDateString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/')})*\n`); + } else if (entry.oldStatus && !entry.newStatus) { + // Deleted Entry + desc = desc.concat(`:x: deleted **${game.name}** from **${entry.oldStatus}** *(${entry.createdAt.toLocaleDateString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/')})*\n`); + } + } + + const embed = new EmbedBuilder() + .setColor(0x6441a5) + .setThumbnail(user.avatarURL()) + .setTitle(`${user.displayName}'s Changelog`) + .setDescription(desc) + .setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() }) + .setTimestamp(); + + return interaction.editReply({ embeds: [embed] }); + }, +}; \ No newline at end of file diff --git a/databaseHelperFunctions.js b/databaseHelperFunctions.js index eb7659d..97435ce 100644 --- a/databaseHelperFunctions.js +++ b/databaseHelperFunctions.js @@ -391,6 +391,17 @@ async function backupDatabase() { } } +async function getChangelog(id) { + const changelogEntries = await Changelog.findAll({where: {userId: id}, order: [ [ 'updatedAt', 'DESC' ]] }) + .catch((err) => { + console.log(err); + }); + + if (changelogEntries) return changelogEntries; + + return false; +} + module.exports = { checkUserRegistration, getUserRegistration, @@ -423,4 +434,5 @@ module.exports = { getGames, getAllBeatenGames, backupDatabase, + getChangelog, }; \ No newline at end of file