From 093cbc62f4a8397d4695a53ef21b22d115c970c0 Mon Sep 17 00:00:00 2001 From: Cesium Date: Tue, 8 Apr 2025 17:05:02 -0400 Subject: [PATCH] error handling : ) --- commands/utility/profile.js | 16 ++++++++++------ index.js | 7 +++++-- torn.js | 2 +- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/commands/utility/profile.js b/commands/utility/profile.js index c9b72aa..58eb35b 100644 --- a/commands/utility/profile.js +++ b/commands/utility/profile.js @@ -10,16 +10,20 @@ module.exports = { .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") + console.log(`Profile: Looking up "${id}"`) } else { id = interaction.options.getInteger('id'); - console.log(id + " no"); + console.log(`Profile: Looking up "${id}"`) } - userdata = await torn.user.profile(id); - console.log(userdata) + userdata = await torn.user.profile(id).catch(console.error); + if (!userdata.name) { + console.log("Profile: Unable to resolve profile") + await interaction.reply("Failed to get profile data :(").catch(console.error); + return + } + console.log(`Profile: Resolved as "${userdata.name}"`) switch (userdata.status.color) { case 'green': userdata.status.hex = 0x69A829 @@ -47,6 +51,6 @@ module.exports = { { name: `${userdata.last_action.status}`, value: `${userdata.last_action.relative}`, inline: true }, ) - await interaction.reply({ embeds: [userEmbed] }); + await interaction.reply({ embeds: [userEmbed] }).catch(console.error); }, }; diff --git a/index.js b/index.js index 4e40d4e..de1dff0 100644 --- a/index.js +++ b/index.js @@ -8,6 +8,7 @@ const state = require('./state.json'); // the basic discord setup stuff yoinked from their guide const { Client, Collection, Events, GatewayIntentBits, EmbedBuilder, Partials } = require('discord.js'); +const { profile } = require('node:console'); const client = new Client({ intents: [ GatewayIntentBits.Guilds, @@ -86,8 +87,10 @@ client.on(Events.MessageCreate, message => { const regexProfile = /https?:\/\/(?:www\.)?torn\.com\/profiles.*?[?&]XID=(\d+)/; if (message.content.match(regexProfile)) { const profileId = message.content.match(regexProfile)[1] + console.log(`Chat: Detected profile link "${profileId}" in message`); torn.user.profile(profileId).then(data => { if (data.name) { // copied from commands/utility/profile.js + console.log(`Chat: Resolved as "${data.name}"`) switch (data.status.color) { case 'green': data.status.hex = 0x69A829 @@ -107,7 +110,7 @@ client.on(Events.MessageCreate, message => { .setColor(data.status.hex) .setTitle(`${data.name} [${data.player_id}]`) .setURL(`https://torn.com/profiles.php?XID=${data.player_id}`) - .setImage(data.profile_image) + .setThumbnail(data.profile_image) .setDescription(data.rank) .addFields( { name: data.status.description, value: data.status.details }, @@ -116,7 +119,7 @@ client.on(Events.MessageCreate, message => { { name: `${data.last_action.status}`, value: `${data.last_action.relative}`, inline: true }, ); message.reply({ embeds: [userEmbed] }) - } + } else console.log("Chat: Unable to resolve profile") }); } }); diff --git a/torn.js b/torn.js index e2512e5..9a1ccb6 100644 --- a/torn.js +++ b/torn.js @@ -41,7 +41,7 @@ module.exports.self = { const response = await fetch(url); const data = await response.json(); config.tornid = data.player_id; - console.log('retreived player id: ' + data.player_id) + console.log(`Torn: Retrieved default ID as "${data.player_id}"`) return(data.player_id); } else return config.tornid; }