error handling : )

This commit is contained in:
2025-04-08 17:05:02 -04:00
parent 315967f885
commit 093cbc62f4
3 changed files with 16 additions and 9 deletions

View File

@@ -10,16 +10,20 @@ module.exports = {
.setDescription('User ID')), .setDescription('User ID')),
async execute(interaction) { async execute(interaction) {
let id let id
console.log(interaction.options.getInteger('id'))
if (!interaction.options.getInteger('id')) { if (!interaction.options.getInteger('id')) {
id = await torn.self.id() id = await torn.self.id()
console.log(id + " yes") console.log(`Profile: Looking up "${id}"`)
} else { } else {
id = interaction.options.getInteger('id'); id = interaction.options.getInteger('id');
console.log(id + " no"); console.log(`Profile: Looking up "${id}"`)
} }
userdata = await torn.user.profile(id); userdata = await torn.user.profile(id).catch(console.error);
console.log(userdata) 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) { switch (userdata.status.color) {
case 'green': case 'green':
userdata.status.hex = 0x69A829 userdata.status.hex = 0x69A829
@@ -47,6 +51,6 @@ module.exports = {
{ name: `${userdata.last_action.status}`, value: `${userdata.last_action.relative}`, inline: true }, { 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);
}, },
}; };

View File

@@ -8,6 +8,7 @@ const state = require('./state.json');
// the basic discord setup stuff yoinked from their guide // the basic discord setup stuff yoinked from their guide
const { Client, Collection, Events, GatewayIntentBits, EmbedBuilder, Partials } = require('discord.js'); const { Client, Collection, Events, GatewayIntentBits, EmbedBuilder, Partials } = require('discord.js');
const { profile } = require('node:console');
const client = new Client({ const client = new Client({
intents: [ intents: [
GatewayIntentBits.Guilds, GatewayIntentBits.Guilds,
@@ -86,8 +87,10 @@ client.on(Events.MessageCreate, message => {
const regexProfile = /https?:\/\/(?:www\.)?torn\.com\/profiles.*?[?&]XID=(\d+)/; const regexProfile = /https?:\/\/(?:www\.)?torn\.com\/profiles.*?[?&]XID=(\d+)/;
if (message.content.match(regexProfile)) { if (message.content.match(regexProfile)) {
const profileId = message.content.match(regexProfile)[1] const profileId = message.content.match(regexProfile)[1]
console.log(`Chat: Detected profile link "${profileId}" in message`);
torn.user.profile(profileId).then(data => { torn.user.profile(profileId).then(data => {
if (data.name) { // copied from commands/utility/profile.js if (data.name) { // copied from commands/utility/profile.js
console.log(`Chat: Resolved as "${data.name}"`)
switch (data.status.color) { switch (data.status.color) {
case 'green': case 'green':
data.status.hex = 0x69A829 data.status.hex = 0x69A829
@@ -107,7 +110,7 @@ client.on(Events.MessageCreate, message => {
.setColor(data.status.hex) .setColor(data.status.hex)
.setTitle(`${data.name} [${data.player_id}]`) .setTitle(`${data.name} [${data.player_id}]`)
.setURL(`https://torn.com/profiles.php?XID=${data.player_id}`) .setURL(`https://torn.com/profiles.php?XID=${data.player_id}`)
.setImage(data.profile_image) .setThumbnail(data.profile_image)
.setDescription(data.rank) .setDescription(data.rank)
.addFields( .addFields(
{ name: data.status.description, value: data.status.details }, { 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 }, { name: `${data.last_action.status}`, value: `${data.last_action.relative}`, inline: true },
); );
message.reply({ embeds: [userEmbed] }) message.reply({ embeds: [userEmbed] })
} } else console.log("Chat: Unable to resolve profile")
}); });
} }
}); });

View File

@@ -41,7 +41,7 @@ module.exports.self = {
const response = await fetch(url); const response = await fetch(url);
const data = await response.json(); const data = await response.json();
config.tornid = data.player_id; 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); return(data.player_id);
} else return config.tornid; } else return config.tornid;
} }