From 9e5aa394a861b9dd1384cbef5d564476b7e2f481 Mon Sep 17 00:00:00 2001 From: baz Date: Mon, 18 Mar 2024 21:12:19 +0000 Subject: [PATCH] Add randomplannedgame command --- commands/100-games/randomplannedgame.js | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 commands/100-games/randomplannedgame.js diff --git a/commands/100-games/randomplannedgame.js b/commands/100-games/randomplannedgame.js new file mode 100644 index 0000000..c48f86d --- /dev/null +++ b/commands/100-games/randomplannedgame.js @@ -0,0 +1,53 @@ +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); +const { getUserRegistration, getPlanningGames, checkGameStorageId } = require('../../databaseHelperFunctions'); +const { getGameJson, getCoverURL } = require('../../igdbHelperFunctions'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('randomplannedgame') + .setDescription('Get a random planned game') + .addUserOption(option => option.setName('user').setDescription('The user to check')), + async execute(interaction) { + await interaction.reply({ content: 'Searching for user...', ephemeral: true }); + + let user = interaction.user; + const userOption = interaction.options.getUser('user'); + + if (userOption) { + user = userOption; + } + + const userDatabaseEntry = await getUserRegistration(user); + if (!userDatabaseEntry) return interaction.followUp({ content: `Issue checking registration with "${user.username}".`, ephemeral: true }); + + const embed = new EmbedBuilder() + .setColor(0x6441a5) + .setAuthor({ name: `${user.displayName}'s Random Planned Game`, iconURL: user.avatarURL() }) + .setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() }); + + const plannedGames = await getPlanningGames(userDatabaseEntry.id); + let desc = ''; + + if (!plannedGames || plannedGames.length == 0) { + desc = `${user.displayName} currently has no planned games.`; + } else { + const randomInt = Math.floor(Math.random() * (plannedGames.length)); + const randomEntry = plannedGames[randomInt]; + const randomGame = await checkGameStorageId(randomEntry.gameId); + const body = `where id = ${ randomGame.igdb_id }; fields *;`; + const res = await getGameJson(body); + if (!res) return interaction.reply({ content: 'No game found.', ephemeral: true }); + const game = res[0]; + + embed.setTitle('THE OCHULUS HAS SPOKEN'); + if (game.cover) { + const coverUrl = await getCoverURL(game.cover); + embed.setThumbnail(`${coverUrl}`); + } + desc = `It has chosen **${game.name}**, glory be to The Ochulus!`; + } + + embed.setDescription(desc); + return interaction.followUp({ embeds: [embed] }); + }, +}; \ No newline at end of file