PUBG tracker script in chat


#21

is there anyway get this script working on discord ?


#22

I have no idea, @IllusionaryOne how different are the script from Discord to Twitch? :slight_smile:


#23

Need to make a copy in Discord and change the $.say(output_string) to $.discord.say(channel, output_string). The channel, you could hard-code for your Discord or come up with a way to make it configurable.


#24

@IllusionaryOne I changed the value still won’t show as it needs to be “registered” as an command in discord. example i change it do $.discord.say(general, output_string) I also tried $.discord.say(channel, output_string). Needs to be coded into a “command” for discord somehow in the script I am not very good with Java.


#25

This uses Rhino (JavaScript) not Java, you would need to look at an existing Discord script to see how we handle and register commands. Unfortunately, I just got back from 2 weeks visiting with in-laws and traveling most of the weekend, I am catching up with work today and cannot walk through how to implement in Discord at this time. My best suggestion really is to look at something simple like scripts/discord/games/8ball.js.


#26

There might be bugs I don’t know how to fix them but so far I got it to work when looking up correct usernames in pubg incorrect names however give me an TypeError: Cannot read property “Rating” from undefined. But I will post what i have so far it does work somewhat. So gives an headstart. Edit: got squad to work @IllusionaryOne looking at 8ball helped little bit.

    (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('discordCommand', function(event) {


        var channel = event.getChannel(),
            command = event.getCommand(),
            mention = event.getMention(),
            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)) {
                $.discord.say(channel, $.discord.userPrefix(mention) + '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. ';
            });

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

#27

There are two conditions that need to be checked.

  1. It looks like if I enter some random name that doesn’t exist, JSON isn’t returned, but HTML:

<div class="alert alert-danger">There was an error loading your profile, admins have been notified! Thanks</div>

  1. You should check the JSON to make sure that it has everything that you want. I do not know the data pattern for PUBG, but I am going to guess that all of the data is not always present in every API call. You should display what is coming back in the JSON and see what is missing that is causing the problem. Perhaps they don’t have solo or duo or squad statistics.

#28

@IllusionaryOne Do you know how to change this to Region EU? or @meJ


#29

I am not aware of how the API works, I simply fixed a couple of bugs once, I do not support this script, unfortunately. That might be something that has to be checked with PUBG.


#30

Hi :slight_smile: I only play on EU and it shows my stats on EU, but how it works, I’m not sure. Try checking the API on https://pubgtracker.com/


#31

How i can use the pubgtracker api directly with a command pls ? When i use “(customapi https://2g.be/twitch/PUBG/command/query=$(querystring)&default_region=eu&default_playlist=squad)”, the bot say : No valid channel given.

Sorry for my english.


#32

That question does not relate to the script. Please open a different Support topic.


#33

Below is my version of the pubg stats by default I have it set to Solo EU

Twitch Chat
PhantomBot\scripts\custom\commands\pubgCommand.js (4.9 KB)
PhantomBot\scripts\lang\custom\commands\commands-pubgCommand.js (194 Bytes)

Discord Chat
PhantomBot\scripts\discord\custom\commands\pubgCommand.js (5.1 KB)

Please keep in mind that the discord version uses the new discord from the nightly build so u will have to change discordChannelCommand to discordCommand if your not using the newest nightly build!

EDIT Fixed type and region matching


#35

that spacing was not needed for discord as the \r\n is a new line so why have a space before the new line??? thanks anyways


#36

did it for twitch chat purpose


#37

Does he have the latest newline supporting version of Discord or not? I seem to recall I patched that in recently, but I could be remembering wrong. I think it was even removed at one point from the Nightly and I put it back in. I could be remembering incorrectly though.

I guess what I mean is…

@Ayysir - are you getting newlines in your output or just your spaces?

EDIT: Read your Twitch post, that makes perfect sense where there are no newlines allowed :slight_smile:

Cheers


#38

well the part you edited was for discord not twitch lol just so you know


#40

the twitch and discord files are both different so thats wierd they use the same settings but the output is totaly different


#41

Remove the \r\n from the twitch version as its useless i forgot i put that in both lol

pubgCommand.js (4.9 KB)


#42

btw the one i edited was the twitch script not the discord one