From 060a30328325ebb53341ae1c8eb735410301eab5 Mon Sep 17 00:00:00 2001 From: Cesium Date: Sun, 6 Apr 2025 08:29:06 -0400 Subject: [PATCH] profile command --- commands/utility/profile.js | 52 +++++++++++++++++++++++++++++++++++++ index.js | 1 + torn.js | 29 ++++++++++++++++++++- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 commands/utility/profile.js diff --git a/commands/utility/profile.js b/commands/utility/profile.js new file mode 100644 index 0000000..c9b72aa --- /dev/null +++ b/commands/utility/profile.js @@ -0,0 +1,52 @@ +const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); +const torn = require('../../torn.js'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('profile') + .setDescription('Get your Torn profile') + .addIntegerOption(option => + option.setName('id') + .setDescription('User ID')), + async execute(interaction) { + let id + console.log(interaction.options.getInteger('id')) + if (!interaction.options.getInteger('id')) { + id = await torn.self.id() + console.log(id + " yes") + } else { + id = interaction.options.getInteger('id'); + console.log(id + " no"); + } + userdata = await torn.user.profile(id); + console.log(userdata) + switch (userdata.status.color) { + case 'green': + userdata.status.hex = 0x69A829 + break + case 'orange': + userdata.status.hex = 0xF6B200 + break + case 'red': + userdata.status.hex = 0xF78483 + break + case 'blue': + userdata.status.hex = 0x4A91B2 + } + + const userEmbed = new EmbedBuilder() + .setColor(userdata.status.hex) + .setTitle(`${userdata.name} [${userdata.player_id}]`) + .setURL(`https://torn.com/profiles.php?XID=${userdata.player_id}`) + .setImage(userdata.profile_image) + .setDescription(userdata.rank) + .addFields( + { name: userdata.status.description, value: userdata.status.details }, + { name: 'Level', value: `${userdata.level}`, inline: true }, + { name: 'Age', value: `${userdata.age} days`, inline: true }, + { name: `${userdata.last_action.status}`, value: `${userdata.last_action.relative}`, inline: true }, + ) + + await interaction.reply({ embeds: [userEmbed] }); + }, +}; diff --git a/index.js b/index.js index b94eed5..f15e81b 100644 --- a/index.js +++ b/index.js @@ -44,6 +44,7 @@ const commandFolders = fs.readdirSync(foldersPath); const command = require(filePath); if ('data' in command && 'execute' in command) { client.commands.set(command.data.name, command); + console.log(`Registered command "${command.data.name}"`); } else { console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`); } diff --git a/torn.js b/torn.js index 3338686..e2512e5 100644 --- a/torn.js +++ b/torn.js @@ -18,4 +18,31 @@ module.exports.api = async (url) => { const response = await fetch(`${url}&key=${config.torn}`); const data = await response.json(); return(data); -}; \ No newline at end of file +}; + +module.exports.user = { + async basic(user) { + const response = await fetch(`https://api.torn.com/user/${user}?selections=basic&key=${config.torn}`); + const data = await response.json(); + return(data); + }, + async profile(user) { + const response = await fetch(`https://api.torn.com/user/${user}?selections=profile&key=${config.torn}`); + const data = await response.json(); + return(data); + } +}; + + +module.exports.self = { + async id() { + if (!config.tornid) { + const url = `https://api.torn.com/user/?selections=basic&key=${config.torn}` + const response = await fetch(url); + const data = await response.json(); + config.tornid = data.player_id; + console.log('retreived player id: ' + data.player_id) + return(data.player_id); + } else return config.tornid; + } +} \ No newline at end of file