Simple Username History

Discussion in 'CommandHelper Scripts' started by ThgilFoDrol, Mar 23, 2016.

  1. ThgilFoDrol

    ThgilFoDrol New Member

    Hey all! I found out about CommandHelper recently, and made a simple wrapper to look up a user's previous names and the dates they were changed via someone's web api. I didn't have anything convenient on hand to look up past usernames, so I made this for that purpose.

    Here's the command for it- placing it here if anybody wants to use it:

    PHP:

    *:/namehistory= >>>
      @p = player(); # store the player
      #-----BEGIN CONFIG-----#
      @permnode = 'randomnamegoeshere.namehistory'; # permission node the user must have to be able to use this command
      @ptag = '&c[Namehistory]'; # prefix that each line from this command starts with (stands for /p/refix /tag/)
      @mcol = '&7'; # colour for the output messages (stands for /m/essage /col/our)
      @kcol = '&6'; # colour for important pieces of information (stands for /k/eyword /col/our)
      #------END CONFIG------#
      if (has_permission(@p, @permnode)){ # Player has the required permission
        msg(colorize(@ptag[email protected]mcol.' To use the experimental '[email protected]kcol.'/namehistory'[email protected]mcol.' command, you must append the '[email protected]kcol.'-x'[email protected]mcol.' flag to the end of the command.'));
        msg(colorize(@ptag[email protected]mcol.' Current flags: '));
        msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol.'-x'[email protected]mcol.' Enables the use of the experimental '[email protected]kcol.'/namehistory'[email protected]mcol.' command.'));
        msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol.'-r'[email protected]mcol.' Prints the raw output instead of the formatted output. Not compatible with '[email protected]kcol.'-u'[email protected]mcol.'. Defaults to formatted.'));
        msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol.'-u'[email protected]mcol.' or '[email protected]kcol.'-uuid'[email protected]mcol.' Displays their history and their UUID. Defaults to false.'));
        msg(colorize(@ptag[email protected]mcol.' Usage: '[email protected]kcol.'/namehistory <username> [flag1] [flag2] [flag3] ... [flagx]'));
        die(colorize(@ptag[email protected]mcol.' Usage: '[email protected]kcol.'/namehistory ThgilFoDrol -x'));
       } else{ # they do not have the required permission node
        msg(colorize(@ptag[email protected]mcol.' You do not have permission to use this command.'));
       }
    <<<

    *:/namehistory $username $ = >>>
      @p = player(); # store the player with a variable
      #-----BEGIN CONFIG-----#
      @permnode = 'randomnamegoeshere.namehistory'; # permission node the user must have to be able to use this command
      @ptag = '&c[Namehistory]'; # prefix that each line from this command starts with (stands for /p/refix /tag/)
      @mcol = '&7'; # colour for the output messages (stands for /m/essage /col/our)
      @kcol = '&6'; # colour for important pieces of information (stands for /k/eyword /col/our)
      @dateformat = 'yyyy.MM.dd \'at\' HH:mm:ss z';
      #------END CONFIG------#
      if (has_permission(@p, @permnode)){ # Player has the required permission
        @arrflags = parse_args($); # turn extra arguments after the initial one (for username) into an array
          if (array_contains(@arrflags, '-x')){ # experimental flag is found (-x) | for now, this is the only way to use this command    
           http_request('http://api.mcusername.net/pastuser/'.$username, closure(@response){ # send the request
               @rawoutput = @response['body']; # store the response
               msg(colorize(@ptag[email protected]mcol.' DEBUG: '[email protected]rawoutput));
                if(equals_ic(@rawoutput, 'Not Premium')){
                    msg(colorize(@ptag[email protected]mcol.' The user '[email protected]kcol.$username[email protected]mcol.' does not appear to exist.'));
                }
                if(equals_ic(@rawoutput, 'null')){
                    die(colorize(@ptag[email protected]mcol.' The API server returned an error while processing the name history lookup for '[email protected]kcol.$username[email protected]mcol.'.'));
                }
                @rawoutput = replace(@rawoutput, '[', ''); # delete the initial square bracket
                @rawoutput = replace(@rawoutput, ']', ''); # delete the concluding square bracket
                @rawoutput = replace(@rawoutput, '}.{', ' '); # space out the entries
                @rawoutput = replace(@rawoutput, '{', ' '); # remove leftover curly brackets
                @rawoutput = replace(@rawoutput, '},', ' ');
                @rawoutput = replace(@rawoutput, '}', ' ');
                @arrrawoutput = parse_args(@rawoutput); # turn it into an array    
               if(array_contains(@arrflags, '-r')){ # raw output was requested
                   for(assign(@i, 0), @i < array_size(@arrrawoutput), @i++){ # iterate through all the entries
                   msg(@arrrawoutput[@i]);
                   }
                  die();
                }
                @displayuuid = false;
                if(array_contains(@arrflags, '-u') || array_contains(@arrflags, '-uuid')){ # uuid was requested
                   @displayuuid = true;
                }
                @patorigname = '"name":"(\\w{3,24})"';
                @patfuturename = '"name":"(\\w{3,24})","changedToAt":(\\d{8,16})';
                for(assign(@i, 0), @i < array_size(@arrrawoutput), @i++){ # iterate through all the entries
                   if (@i != 0){ # not the first entry
                       @arrrawoutputlastindex = array_size(@arrrawoutput) - 1;
                        if (@i == 1 && @i != @arrrawoutputlastindex){ # not the first name change
                           @arrresult = reg_match(@patfuturename, @arrrawoutput[@i]);
                            msg(colorize(@ptag[email protected]mcol.' Previous Usernames:'));
                            msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol[email protected]arrresult[1][email protected]mcol.', changed at '[email protected]kcol.simple_date(@dateformat, @arrresult[2])));
                        } else if (@i == @arrrawoutputlastindex){ # is final name change
                           @arrresult = reg_match(@patfuturename, @arrrawoutput[@i]);
                            msg(colorize(@ptag[email protected]mcol.' Current Username:'));
                            msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol[email protected]arrresult[1][email protected]mcol.', changed at '[email protected]kcol.simple_date(@dateformat, @arrresult[2])));
                        } else{ # is neither first nor last name change
                           @arrresult = reg_match(@patfuturename, @arrrawoutput[@i]);
                            msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol[email protected]arrresult[1][email protected]mcol.', changed at '[email protected]kcol.simple_date(@dateformat, @arrresult[2])));
                        }
                    } else{ # is the first entry
                       @arrresult = reg_match(@patorigname, @arrrawoutput[@i]);
                        msg(colorize(@ptag[email protected]mcol.' Username History Lookup for '[email protected]kcol.$username));
                        if (@displayuuid){ # if true, display the target player's uuid
                           try( # may fail according to wiki
                               msg(colorize(@ptag[email protected]mcol.' UUID: '[email protected]kcol.puuid($username)))
                            ,#catch
                               msg(colorize(@ptag[email protected]mcol.' You requested the UUID, but it could not be resolved.'));
                            )                    
                        }
                        msg(colorize(@ptag[email protected]mcol.' Original Name: '[email protected]kcol[email protected]arrresult[1]));                  
                    }
                    if (array_size(@arrrawoutput) == 1){ # only received one entry
                       msg(colorize(@ptag[email protected]mcol.' No name changes were found.'));
                    }
                }
            });
          } else{ # send information
           msg(colorize(@ptag[email protected]mcol.' To use the experimental '[email protected]kcol.'/namehistory'[email protected]mcol.' command, you must append the '[email protected]kcol.'-x'[email protected]mcol.' flag to the end of the command.'));
           msg(colorize(@ptag[email protected]mcol.' Current flags: '));
           msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol.'-x'[email protected]mcol.' Enables the use of the experimental '[email protected]kcol.'/namehistory'[email protected]mcol.' command.'));
           msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol.'-r'[email protected]mcol.' Prints the raw output instead of the formatted output. Not compatible with '[email protected]kcol.'-u'[email protected]mcol.'. Defaults to formatted.'));
           msg(colorize(@ptag[email protected]mcol.' -> '[email protected]kcol.'-u'[email protected]mcol.' or '[email protected]kcol.'-uuid'[email protected]mcol.' Displays their history and their UUID. Defaults to false.'));
           msg(colorize(@ptag[email protected]mcol.' Usage: '[email protected]kcol.'/namehistory <username> [flag1] [flag2] [flag3]'));
           die(colorize(@ptag[email protected]mcol.' Example: '[email protected]kcol.'/namehistory ThgilFoDrol -x -uuid'));
          }
       } else{ # they do not have the required permission node
          msg(colorize(@ptag[email protected]mcol.' You do not have permission to use this command.'));
       }
    <<<
     
    Still new to commandhelper; sorry if there are poorly optimized components of the script.
    Last edited: Mar 23, 2016
  2. MCmacjuul

    MCmacjuul Member

    Can't you use json_decode() to transform the retrieved data into an array?
  3. ThgilFoDrol

    ThgilFoDrol New Member

    I'll try it out in a bit. I wasn't sure of the capabilities of the json decoder or how json works at all, so I opted for something more familiar first. Thanks for the tip!
  4. ThgilFoDrol

    ThgilFoDrol New Member

    So it's been a while, and the API I used to get around Mojang's rate limiting has long died. I've since written a name history command that uses the Mojang API instead, and did away with the string replace witchery I screwed around with in the OP.

    Edit: I tried pasting it here, but it removed the indents at the front of each line. Here's a link to it instead.
    https://github.com/thgilfodrol/NoobcraftGC-CH/blob/master/namehistory/namehistory.msa