Advertisement
Pyorot

LondonPogoMap/Discord Bot v2.2

Apr 4th, 2017
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const rom_code = ''; // redacted
  2. const version = 'v2.2';
  3. var rom = {channels: {pysstatus: '298589684492140546'}};
  4. var iv = Array.apply(null, new Array(252)).map(Number.prototype.valueOf,1);
  5. // var r = Array.apply(null, new Array(252)).map(Number.prototype.valueOf,100);
  6. // var home_lat = 51.507406; var home_lng = -0.127675;
  7. var known_despawns = []; // keeps track of Pokemon already notified
  8. var first_run = 0; // the bot skips Pokemon already on the map when it's started
  9. var overload = 0; // tracks Discord notification overload
  10.  
  11.  
  12. // 1) converts timecodes into legible times (call with no parameter for time now)
  13. function time(timecode) {
  14.     if (typeof timecode == 'undefined') {var date = new Date();} else {var date = new Date(timecode);};
  15.     var hours = date.getHours();
  16.     var minutes = "0" + date.getMinutes();
  17.     var seconds = "0" + date.getSeconds();
  18.     return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
  19. }
  20.  
  21.  
  22. // 2) converts Pokemon spawns into text notifications for Discord
  23. function tellD(pokemon) {
  24.     iv_percent = Math.round(((pokemon.attack + pokemon.defence + pokemon.stamina)/45)*100);
  25.  
  26.     // tries to retrieve postcode and suburb from OpenStreetMap
  27.     urlrg = 'https://nominatim.openstreetmap.org/reverse?format=json&lat='+pokemon.center.lat+'&lon='+pokemon.center.lng
  28.     var xhs = new XMLHttpRequest();
  29.     xhs.open('GET', urlrg, false);
  30.     xhs.onload = function() {if (xhs.status != 200) {notifyD("Failure: OSM; "+xhs.status, rom.channels.pysstatus)}};
  31.     xhs.send();
  32.     try {var address = JSON.parse(xhs.response).address; var suburb = address.suburb; var postcode = address.postcode;} catch(error) {}
  33.     if (typeof postcode == 'undefined') {var postcode = "?"; };
  34.     if (typeof suburb == 'undefined') {var suburb = ""; } else {var suburb = ' ' + suburb};
  35.     if (postcode.indexOf(' ') != -1) {postcode = postcode.substr(0,postcode.indexOf(' '));};
  36.  
  37.     // tries to retrieve more-accurate postcode from postcodes.io, and replaces OSM postcode if successful, adds ? if not
  38.     urlps = 'https://api.postcodes.io/postcodes?lat='+pokemon.center.lat+'&lon='+pokemon.center.lng
  39.     var xht = new XMLHttpRequest();
  40.     xht.open('GET', urlps, false);
  41.     xht.onload = function() {if (xht.status != 200) {notifyD("Failure: postcodes.io; "+xht.status,rom.channels.pysstatus)}};
  42.     xht.send();
  43.     try {postcode = JSON.parse(xht.response).result[0].outcode;} catch(error) {postcode += '?'; };
  44.  
  45.     url = 'http://www.google.com/maps/place/' + pokemon.center.lat + ',' + pokemon.center.lng;
  46.  
  47.     var name = pokeDict[pokemon.id].name;
  48.     if (pokemon.id == 201 && pokemon.form != 0) {name = name + ' ' + String.fromCharCode(pokemon.form + 64)}
  49.  
  50.     var expiry_time = time(pokemon.despawn*1000);
  51.     var rem_time = timeToString(pokemon.remainingTime());
  52.  
  53.     return name+' | '+iv_percent+'% | '+postcode+suburb+' | '+rem_time+' (until '+expiry_time+') | '+url;
  54. }
  55.  
  56.  
  57. // 3) converts Pokemon spawns into map image url
  58. function StatIm(pokemon) {
  59.     return 'https://maps.googleapis.com/maps/api/staticmap?markers='+pokemon.center.lat+','+pokemon.center.lng+'&zoom=15&size=400x400&sensor=false&key='+rom.keys.google;
  60. }
  61.  
  62.  
  63. // 4) sends messages to Discord
  64. function notifyD(content, channel, embed_url) {
  65.     var xhr = new XMLHttpRequest();
  66.     xhr.open('POST', "https://discordapp.com/api/channels/"+channel+"/messages", false);
  67.     xhr.setRequestHeader('Authorization', 'Bot '+rom.keys.bot);
  68.     xhr.setRequestHeader('Content-Type', 'application/json');
  69.     xhr.onload = function() {
  70.         if (xhr.status != 200) {
  71.             console.log('Failed to post; ' + xhr.status);
  72.             if (xhr.status == 429) {overload++};
  73.         }
  74.     };
  75.     var sender = '{"content":"'+content+'"';
  76.     if (!!embed_url) {sender += ',"embed": {"image": {"url":"'+embed_url+'"}}}'} else {sender += '}'};
  77.     xhr.send(sender);
  78.     if (!!embed_url) {console.log('[I] ['+channel+'] '+content)} else {console.log('['+channel+'] '+content)};
  79. }
  80.  
  81.  
  82. // 5) polls map for new desired Pokemon and returns list of Pokemon to notify
  83. function poll() {
  84.     output = [];
  85.     for (i = 0, len = this.pokemons.length; i < len; i++) {
  86.         test = this.pokemons[i];
  87.         test_iv = (test.attack + test.defence + test.stamina) - 45;
  88.         if (test_iv >= iv[test.id]) {
  89.             // test_x = Math.abs(test.center.lat - home_lat)*110.574;
  90.             // test_y = Math.abs(test.center.lng - home_lng)*69.298;
  91.             // if (test_x*test_x + test_y*test_y < r[test.id]*r[test.id]) {
  92.             if (known_despawns.indexOf(test.despawn) == -1) {
  93.                 known_despawns.push(test.despawn);
  94.                 test.iv = test_iv;
  95.                 output.push(test);
  96.             };
  97.             //}
  98.         };
  99.     };
  100.     return output
  101. }
  102.  
  103.  
  104. // 6) for each Pokemon in list, runs tellD and StatIm, then selects channels and posts to them
  105. function mail(list) {
  106.     if (list.length != 0) {
  107.         item = list.pop();
  108.         var notif = tellD(item); var urlim = StatIm(item);
  109.         // channel selection and posting
  110.         if (!!rom.allTarget[item.id]) {
  111.             notifyD(notif, rom.channels[rom.allTarget[item.id]], urlim);
  112.             if (!!rom.secondTarget[item.id]) {
  113.                 if (!rom.secondIV[item.id] || (!!rom.secondIV[item.id] && item.iv >= rom.secondIV[item.id])) {
  114.                     notifyD(notif, rom.channels[rom.secondTarget[item.id]], urlim);
  115.                 };
  116.             };
  117.         };
  118.         var highIVfilter = 0; if (!!rom.highIV[item.id]) {highIVfilter = rom.highIV[item.id]};
  119.         if (item.iv >= highIVfilter) {notifyD(notif, rom.channels[rom.highchannel], urlim)};
  120.         // continue through list with delay = postdelay
  121.         setTimeout(function(){mail(list)}, rom.postdelay*1000);
  122.     }
  123. }
  124.  
  125.  
  126. // A) updates ROM from internet and refreshes Discord connection every 15 minutes
  127. function load() {
  128.     // updates ROM
  129.     var xhr = new XMLHttpRequest;
  130.     xhr.open('GET', 'https://jsonblob.com/api/jsonBlob/'+rom_code, false);
  131.     xhr.onload = function() {
  132.         if (xhr.status == 200) {
  133.             try {rom = JSON.parse(xhr.response)} catch(error) {notifyD("Failed to parse ROM", rom.channels.pysstatus)}
  134.         } else {
  135.             notifyD("Failed to fetch ROM; "+xhr.status, rom.channels.pysstatus)
  136.         }
  137.     };
  138.     xhr.send();
  139.     // refreshes Discord connection
  140.     var ws = new WebSocket('wss://gateway.discord.gg');
  141.     gateway = JSON.stringify({
  142.         "op": 2,
  143.         "d": {
  144.             "token": rom.keys.bot,
  145.             "properties": {"$os": "linux", "$browser": "sometestingbrowser", "$device": "sometestingdevice", "$referrer": "", "$referring_domain": "",},
  146.             "compress": true,
  147.             "large_threshold": 250,
  148.         }
  149.     });
  150.     setTimeout(function(){ws.send(gateway)}, 2 * 1000);
  151.     // updates bot IV filter based on ROM
  152.     for (i = 1, len = iv.length; i < len; i++) {
  153.         if (!!rom.allTarget[i]) {
  154.             iv[i] = -48;
  155.             if (!!rom.allIV[i]) {iv[i] = rom.allIV[i]};
  156.         } else {
  157.             iv[i] = 0;
  158.             if (!!rom.highIV[i]) {iv[i] = rom.highIV[i]};
  159.         }
  160.     }
  161.     // checks overload and posts OK status
  162.     var stat = version+' | '+rom.changelog+' | '+time()+' | '+known_despawns.length;
  163.     if (overload != 0) {stat += (' | overload: ' + overload); overload = 0;};
  164.     setTimeout(function(){notifyD(stat, rom.channels.pysstatus)}, 4 * 1000);
  165. }
  166. load(); loader = setInterval(load, 20*60*1000);
  167.  
  168.  
  169. // B) clears known despawns every 24 hours
  170. function refresh() {known_despawns = []; first_run = 0;};
  171. refresher = setInterval(refresh, 24*60*60*1000);
  172.  
  173.  
  174. // C) polls map and mails Pokemon every 30 seconds
  175. var timer = setInterval(function() {
  176.     list = poll();
  177.     if (first_run == 1) {mail(list)};
  178.     first_run = 1;
  179.     console.log('Ran at '+time());
  180. }, 30 * 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement