Skip to content
TORNLIFE More

Discord bot problem (node)

Started by Kole [2106784] on in API Development.

8 replies · 115 views · thread synced · 4 days ago · View on torn.com
About this thread

Posts archived: 9 / 9 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
9
Discussion span
→
People posting
3
Likes on archived posts
1
Authority score
51 / 100
Historical score
27 / 100
Story score
32 / 100
Engagement score
48 / 100

Mentioned in this thread

Player 1234 [1234] ×2

Kole [2106784]

Hi, I'm building a bot to pull info from the api and came across a problem with how status is displayed

From the json file, it's

 "status": [ "In hospital for 1 hrs 14 mins ", "Hospitalized by Person" ],

Which is fine as long as the link is being displayed on Torn, but for discord obviously it won't work.

My code is something like

let embed = new Discord.RichEmbed()
...
.addField("Status", entry.status)
...

message.channel.send(embed);
});

Which works fine, but in discord the link doesn't work, as such

 [image: i.imgur.com]

So, my question is.. is anyone familiar enough with Node to point me in the right direction to either parse that how I want, or be able to format at least the link to a working link for discord?

Mentions: Person [1234]

Pi77Bull [2082618]

Not pretty but it works:

$.getJSON("https://api.torn.com/user/###?selections=basic&key=###", (data) => {
    let arr = data.status[1].split("XID=")[1].split('">');
    let id = arr[0];
    let name = arr[1].slice(0, -4);
});

Then you can use the ID to create the profile link.

(Edit: obviously minus the JQuery part, you will only need the string formatting ^^)
Kole [2106784]

That works great for pulling the id and name to build my own link, so awesome and thanks for that.

But in this situation I'm still going to run into problems with being unable to use the info pulled from json /if/ it provides it's own link to whomever did something to that player.

So any way to use split to cut out any bits that give a link (after the other info I need like "Okay" status or "mugged bla bla"?
Pi77Bull [2082618]

I don't know if I understood that correctly.

(neglecting escaping)

str = "Attacked by >a href="profiles.php?XID=###">###>/a";

If str is the string and you just want the the beginning (in this case "Attacked by") you could do:

arr = str.split(" by ");

which would return this array:

["Attacked", ">a href="profiles.php?XID=###">###>/a"]

Now you have the first part without any link (arr[0]) and you can get the ID and name the same way as in my first comment though you could simplify that like this:

attackerInfo = arr[1].slice(26, -4).split('">');  //return array with ID and name of attacker

(edit: I can't get the links to display correctly)
Kole [2106784]

I think you have the jist of what I'm saying, but just in case so that we're clear.

When the command is run to give info about a player in discord, it will return their current stats.

If ok -> "Okay"

If OD -> "In hospital for 3 days and 10 hours Overdosed on Xanax"

If hospitalized / stealthed -> "In hospital for 4 hours and 7 minutes Hospitalized by someone"

If hospitalized -> "In hospital for 1 hour and 15 minutes Hospitalized by `a href="profiles.php?XID=1234">Player`"

So all of these will be perfectly fine using the default json data except an unstealthed mug/attack/hosp.. because it shows the user who did it, as a link that won't work outside of torn (why did they even format it that way? The url should have been a complete https:// link default... come on guys.).

So ideally, I need to be able to use the default json data just like it is.. /except/ change it to show a proper link in discord IF the status gives one.

The brick wall I'm running into is how it would know when to edit out the default link and use mine, for only those times where I have to fix the link.

Mentions: Player [1234]

Pi77Bull [2082618]

I agree with you. It's not really useful to have an HTML object in a string. It creates more problems than it solves.

So yeah, now that you can get the ID of the attacker you can just create a string that contains the complete url like this:

let url = "https://www.torn.com/profiles.php?XID=" + ID;
Kole [2106784]

Well I ended up getting it to work, but I have a feeling that with the following code, if a user's status does not include a url it will throw an error. I'll have to wait until the person is out of hospital to test it.

I used
let arr = entry.status[1].split("XID=") [1].split('">');
let id = arr[0];
let name = arr[1].slice(0, -4);

let status1 = entry.status[0]; // to get just the first part of the data ie; Okay or In hospital for x
let status2 = entry.status[1].split('
let status3 = `[${name}](https://www.torn.com/profiles.php?XID=${id}#/)`; // to add the name as a link (using markdown) to discord properly

Then in the embed I have
.addField("Status", status1 + "n" + status2 + status3)

Which results in

[image: i.imgur.com]

I have a feeling that will break if the status changes to anything without a url, but I'm not sure how else to do it..
Pi77Bull [2082618]

There are many ways how you could check if it contains a url. Here are a couple:

if (JSONData.status[1].split("XID=").length => 1) {}  //str will be split at "XID=" but when it doesn't exist it can't be split anywhere which results in the array having only 1 item

if (JSONData.status[1].includes("XID")) {}  //this function checks if a string is included in another string and return true or false

if (!JSONData.status[1].includes("someone")) {}  //checks if string does NOT include "someone"

Hope I could help.