PUBG tracker script in chat


#1

Hello there everyone.

@Karonte84 made a PUBGtracker script that should probably be working, but I can’t get it working and I’m no good with javascript. The old post can be located here: https://archive.phantombot.tv/topic/2358/pubgtracker-script

Is there anyone who has a similar script or know how to get this working? I get no response when typing !pubs in chat on twitter.

And do you know if I can type !pubs to anyone and the bot will fetch the stat for that user?


#2

Based on the archive post, I assume you didn’t go and obtain your own API key?

Go to:
https://pubgtracker.com/

Register for an account. Then you will create an API key:

https://pubgtracker.com/site-api/create

You will be required to fill out a form. You will be given an API key, it will look like:

abcdefgh-1234-abcd-1234-abcdefghijklmo

That goes into the script where {‘key’} is. Be sure to remove the extra braces. It should be ‘key_value’:
h.put(‘TRN-Api-Key’, ‘abcdefgh-1234-abcd-1234-abcdefghijklmo’);

Replace {nickname} with the name to lookup.

Also, check the level of the permission on the commands, it is set to 1 right now:
$.registerChatCommand(’./commands/pub.js’, ‘pubs’, 1);

Change to whatever default you want in chat, you can always change with !permcom later.

Delete this line, I don’t even see the command defined in this file, it still works with it, but, serves no purpose that I can see:
$.registerChatCommand(’./commands/pub.js’, ‘vote’, 1);

When I set the script to a user ID that was valid:

[06-17-2017 @ 01:17:10.847 MDT] [CHAT] Solo: Rank #1 with 2,532.04 points. Duo: Rank #16955 with 1,727.26 points.

Be sure to remove this line:
$.say(’ stats ’ + JSON.stringify(stats))

It is a debug statement and prints out the entire JSON object to chat.


#3

Alright thank you :slight_smile:

I already got the api key and it wouldn’t load. But that was maybe because of the key_value as you mentioned.
Now it is still not responding.
Maybe I’m not suppose to add this to the script/systems folder? I’ve moved it to handlers and it is loaded in the bot, and enabled.

I’ve also done the necessary adjustments you mentioned.

Am I correct to believe that I should type !pubs in chat? Also can I add a $1 instead of {nickname} and have it look up other users aswell?


#4

It should be in scripts/commands, according to the author. When I tried it, I revamped it to run in my scripts/custom directory though.

Did you make sure to change the permission on the command from 1 to perhaps 6?


#5

Moved it to /commands and changed it to 6 now. Still nothing :confused:

I’ve made a paste of the script: https://pastebin.com/0EEnXnNV


#6

Not sure then, I didn’t originally author the script, unfortunately.

pubs
[06-17-2017 @ 01:51:27.260 MDT] [CHAT] usage: !pubs [username]
pubs SexyPIG
[06-17-2017 @ 01:51:33.268 MDT] [CHAT] Solo: Rank #2 with 2,532.04 points. Duo: Rank #17084 with 1,727.26 points.

Below is what I am using, from scripts/custom. Also make sure that the module is enabled.

Beyond this, as I didn’t write the script, I cannot really provide much more support.

(function() {
    function _getJSON(url){
        var HttpRequest = Packages.com.gmt2001.HttpRequest;
        var HashMap = Packages.java.util.HashMap;
        var h = new HashMap();
        h.put('TRN-Api-Key', 'my api key');
        var responseData = HttpRequest.getData(HttpRequest.RequestType.GET, encodeURI(url), '', h);
        return responseData.content;
    }
    function getRanks(json) {
        var stats = json.Stats;
        var results = {};
        for (i in stats) {
            var t = stats[i].Match;
            var r = {};
            lstats = stats[i].Stats
            for (k in lstats) {
                r[lstats[k].label] = lstats[k];
            }
            results[t] = r;
        }
        return results;
    }
    $.bind('command', function(event) {
        var sender = event.getSender(),
            command = event.getCommand(),
            args = event.getArgs(),
            action = args[0],
            subAction = args[1];
        if (command.equalsIgnoreCase('pubs')) {
            if (args[0] === undefined) {
                $.say('usage: !pubs [username]');
                return;
            }
            var json = JSON.parse(_getJSON("https://pubgtracker.com/api/profile/pc/" + args[0]));
            var stats = getRanks(json);
            $.say('Solo: Rank #' + stats['solo']['Rating']['rank'] + ' with ' +
                  stats['solo']['Rating']['displayValue'] + ' points. Duo: Rank #' +
                  stats['duo']['Rating']['rank'] + ' with ' +
                  stats['duo']['Rating']['displayValue'] + ' points.');
        }
    });
    $.bind('initReady', function() {
        if ($.bot.isModuleEnabled('./custom/pub.js')) {
            $.registerChatCommand('./custom/pub.js', 'pubs', 6);
        }
    });
})();

#7

Ok, I’ve moved it to custom now. Pasted exactly what you wrote, and added my own api key. It does not even respond to !pubs
Without any nick behind. I’m thinking it wont load properly or something.
./custom/pubg.js is enabled now. When i head over to commands and permission pubs is set to viewer. And I am registered as admin.


#8

I did notice it took several seconds to get back data for meJOo. It appeared to do nothing, but after a while came back. I was able to run a few other commands before it returned.


#9

Did you remove the other copy of the script? You only have the new copy under custom? Also, if you changed the filename to pubg.js, it will not work. The filename has to be pub.js as indicated in the API calls:

    if ($.bot.isModuleEnabled('./custom/pub.js')) {
        $.registerChatCommand('./custom/pub.js', 'pubs', 6);

If you change the name or path, update the API calls.


#10

aaah silly me. What a noob mistake :frowning:

You got it working :smiley: Thank you so very much for the support, you are doing an awesome job :heart_eyes:


#11

Stuff happens. If you are working now, then hot dog. Good luck in your PUBG adventures!

Cheers


#12

Side note, I moved to custom so that when the bot is updated you can just take your custom directory over to a new area if you want. This way you aren’t looking all over for custom scripts that you add. I recommend adding any and all custom items to a custom directory if at all possible to make upgrades easier in the future. :slight_smile:

Cheers again!


#13

Will do, and thanks :slight_smile:


#14

Sorry for ‘starting’ this thread again, but I also seem to have issues with this script.
I’ve copied @IllusionaryOne script above, added my API key but that doesn’t seem to work for me.
I get no responses in chat from the bot when using either !pubs nor !pubs (name)

I’ve added the script to /scripts/commands - Named it pub.js


#15

My copy of the script must be in scripts/custom. Then enabled.


#16

Ah, of course. Knew I was doing something wrong… I just couldn’t figure out what, lol.
Thanks!

EDIT:
The custom folder should be manually created, right?


#17

Yes, PhantomBot doesn’t ship with it automatically created.


#18

Figured it out and got it working.
Thanks! :slight_smile:


#19

Hi.

Everything works excellent and I’ve added squad stats as well. But this returns a very messy setup in the chat so we thought about the possibility to separate the different stats.

so that people can type
!pubg name (show’s all three)
!pubg name solo (for solo stats only)
!pubg name duo (for duo stats only)
!pubg name squad (for squad stats only)

Any idea how I can add this to the script?

(function() {
    function _getJSON(url){
        var HttpRequest = Packages.com.gmt2001.HttpRequest;
        var HashMap = Packages.java.util.HashMap;
        var h = new HashMap();
        h.put('TRN-Api-Key', 'MY-KEY');
        var responseData = HttpRequest.getData(HttpRequest.RequestType.GET, encodeURI(url), '', h);
        return responseData.content;
    }
    function getRanks(json) {
        var stats = json.Stats;
        var results = {};
        for (i in stats) {
            var t = stats[i].Match;
            var r = {};
            lstats = stats[i].Stats
            for (k in lstats) {
                r[lstats[k].label] = lstats[k];
            }
            results[t] = r;
        }
        return results;
    }
    $.bind('command', function(event) {
        var sender = event.getSender(),
            command = event.getCommand(),
            args = event.getArgs(),
            action = args[0],
            subAction = args[1];
        if (command.equalsIgnoreCase('pubg')) {
            if (args[0] === undefined) {
                $.say('usage: !pubg [username]');
                return;
            }
            var json = JSON.parse(_getJSON("https://pubgtracker.com/api/profile/pc/" + args[0]));
            var stats = getRanks(json);
            $.say('Solo: Rank #' + stats['solo']['Rating']['rank'] + ' with ' +
                  stats['solo']['Rating']['displayValue'] + ' points. Duo: Rank #' +
                  stats['duo']['Rating']['rank'] + ' with ' +
                  stats['duo']['Rating']['displayValue'] + ' points. Squad: Rank #'  +
                  stats['squad']['Rating']['rank'] + ' with ' +
                  stats['squad']['Rating']['displayValue'] + ' points. ');
        }
    });
    $.bind('initReady', function() {
        if ($.bot.isModuleEnabled('./custom/pub.js')) {
            $.registerChatCommand('./custom/pub.js', 'pubg', 6);
        }
    });
})();

#20

Hello again

Got a friend helping me some as I don’t know that much about javascript. We got it working and here is the finished script with the possibility to type !pubg [solo/duo/squad] :slight_smile:

(function() {
    function _getJSON(url){
        var HttpRequest = Packages.com.gmt2001.HttpRequest;
        var HashMap = Packages.java.util.HashMap;
        var h = new HashMap();
        h.put('TRN-Api-Key', 'MY-KEY');
        var responseData = HttpRequest.getData(HttpRequest.RequestType.GET, encodeURI(url), '', h);
        return responseData.content;
    }
    function getRanks(json) {
        var stats = json.Stats;
        var results = {};
        for (i in stats) {
            var t = stats[i].Match;
            var r = {};
            lstats = stats[i].Stats
            for (k in lstats) {
                r[lstats[k].label] = lstats[k];
            }
            results[t] = r;
        }
        return results;
    }
    function capitalizeFirstLetter(string) {
        return string.charAt(0).toUpperCase() + string.slice(1);
    }
    $.bind('command', function(event) {


        var sender = event.getSender(),
            command = event.getCommand(),
            args = event.getArgs(),
            username = args[0] === undefined ? undefined : String(args[0]),
            statsType = args[1] === undefined ? undefined : String(args[1]),
            allowedTypes = ['solo', 'duo', 'squad'];

        if (command.equalsIgnoreCase('pubg')) {
            // Check arguments


                if (username === undefined || (statsType !== undefined && allowedTypes.indexOf(statsType) === -1)) {
                $.say('usage: !pubg <username> [' + allowedTypes.join('|') + ']');
                return;
            }

            // Get JSON and parse stats
            var json = JSON.parse(_getJSON("https://pubgtracker.com/api/profile/pc/" + username));
            var stats = getRanks(json);

            // Decide which stats types to print
            var displayTypes;
            if (statsType === undefined) {
                displayTypes = allowedTypes;
            } else {
                displayTypes = [statsType];
            }

            // Iterate over stats types and add formatted string to output
            var output = '';
            displayTypes.forEach(function(type) {
                output += capitalizeFirstLetter(type) + ': ' +
                          'Rank #' + stats[type]['Rating']['rank'] +
                          ' with ' + stats[type]['Rating']['displayValue'] + ' points. ';
            });

            $.say(output);
        }
    });
    $.bind('initReady', function() {
        if ($.bot.isModuleEnabled('./custom/pub.js')) {
            $.registerChatCommand('./custom/pub.js', 'pubg', 6);
        }
    });
})();