Advertisement
Pyorot

LondonPogoMap/Discord Bot v1

Apr 1st, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. bot_token = '0'; // redacted
  2. google_key = '0'; // redacted
  3.  
  4. // converts timecodes into legible times (call with no parameter for time now)
  5. function time(timecode) {
  6.     if (typeof timecode == 'undefined') {var date = new Date();} else {var date = new Date(timecode);};
  7.     var hours = date.getHours();
  8.     var minutes = "0" + date.getMinutes();
  9.     var seconds = "0" + date.getSeconds();
  10.     return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
  11. }
  12.  
  13. // converts Pokemon spawns into text notifications for Discord
  14. function tellD(pokemon) {
  15.     iv_percent = Math.round(((pokemon.attack + pokemon.defence + pokemon.stamina)/45)*100);
  16.  
  17.     // tries to retrieve postcode and suburb from OpenStreetMap
  18.     urlrg = 'https://nominatim.openstreetmap.org/reverse?format=json&lat='+pokemon.center.lat+'&lon='+pokemon.center.lng
  19.     var xhs = new XMLHttpRequest(); xhs.open('GET', urlrg, false); xhs.send();
  20.     address = JSON.parse(xhs.response).address;
  21.     suburb = address.suburb; postcode = address.postcode;
  22.     if (typeof postcode == 'undefined') {postcode = "?"; };
  23.     if (typeof suburb == 'undefined') {suburb = ""; } else {suburb = ' ' + suburb};
  24.     if (postcode.indexOf(' ') != -1) {postcode = postcode.substr(0,postcode.indexOf(' '));};
  25.  
  26.     // tries to retrieve more-accurate postcode from postcodes.io, and replaces OSM postcode if successful, adds ? if not
  27.     urlps = 'https://api.postcodes.io/postcodes?lat='+pokemon.center.lat+'&lon='+pokemon.center.lng
  28.     var xht = new XMLHttpRequest(); xht.open('GET', urlps, false); xht.send();
  29.     try {postcode = JSON.parse(xht.response).result[0].outcode;}
  30.         catch(error) {postcode = postcode + '?'; };
  31.  
  32.     url = 'http://www.google.com/maps/place/' + pokemon.center.lat + ',' + pokemon.center.lng;
  33.  
  34.     var expiry_time = time(pokemon.despawn*1000);
  35.     var rem_time = timeToString(pokemon.remainingTime());
  36.  
  37.     return pokeDict[pokemon.id].name+' | '+iv_percent+'% | '+postcode+suburb+' | '+rem_time+' (until '+expiry_time+') | '+url;
  38. }
  39.  
  40. // converts Pokemon spawns into map image url
  41. function StatIm(pokemon) {
  42.     return 'https://maps.googleapis.com/maps/api/staticmap?markers='+pokemon.center.lat+','+pokemon.center.lng+'&zoom=15&size=400x400&sensor=false&key='+google_key;
  43. }
  44.  
  45. // sends text to Discord
  46. function notifyD(content, channel) {
  47.     var xhr = new XMLHttpRequest();
  48.     xhr.open('POST', "https://discordapp.com/api/channels/"+channel+"/messages", true);
  49.     xhr.setRequestHeader('Authorization', 'Bot '+bot_token);
  50.     xhr.setRequestHeader('Content-Type', 'application/json');
  51.     xhr.send('{"content":"'+content+'"}');
  52.     console.log(content);
  53. }
  54.  
  55. // sends text + embedded image to Discord
  56. function notifyImD(content, embed_url, channel) {
  57.     var xhr = new XMLHttpRequest();
  58.     xhr.open('POST', "https://discordapp.com/api/channels/"+channel+"/messages", true);
  59.     xhr.setRequestHeader('Authorization', 'Bot '+bot_token);
  60.     xhr.setRequestHeader('Content-Type', 'application/json');
  61.     xhr.send('{"content":"'+content+'","embed": {"image": {"url":"'+embed_url+'"}}}');
  62.     console.log('[I] '+content);
  63. }
  64.  
  65. // opens connection with Discord
  66. var ws = new WebSocket('wss://gateway.discord.gg');
  67. gateway = JSON.stringify({
  68.  "op": 2,
  69.  "d": {
  70.  "token": bot_token,
  71.  "properties": {"$os": "linux", "$browser": "sometestingbrowser", "$device": "sometestingdevice", "$referrer": "", "$referring_domain": "",},
  72.  "compress": true,
  73.  "large_threshold": 250,
  74.  }
  75. });
  76. setTimeout(function(){ws.send(gateway)}, 2 * 1000);
  77.  
  78. // channel IDs
  79. const pysgeneral =    '293028598024110080'
  80. const pyschannel1 =   '293838131407486980'
  81. const pyschannel2 =   '296066428694429697'
  82. const pyschannel3 =   '296965358533869568'
  83. const pyschannel4 =   '296986176567640064'
  84. const pglgeneral =    '260119001018007552'
  85. const pgldev =        '276041020402040833'
  86. const pgllarvitar =   '283156333015072768'
  87. const pglbig =        '268886255952068619'
  88. const pgldefenders =  '268886358762717194'
  89. const pgldexfillers = '268886304509394944'
  90. const pglperfect =    '260507173296013312'
  91.  
  92. // IV filters
  93. iv = Array.apply(null, new Array(252)).map(Number.prototype.valueOf,0);
  94. iv[143] = -45; iv[149] = -45; iv[181] = -45; iv[201] = -45; iv[242] = -45; iv[248] = -45;
  95. iv[179] = -45; iv[180] = -45;
  96. iv[246] = -45; iv[247] = -45;
  97. iv[113] = -2;
  98.  
  99. // radius filters
  100. r = Array.apply(null, new Array(252)).map(Number.prototype.valueOf,100);
  101. home_lat = 51.507406; home_lng = -0.127675;
  102.  
  103. // channel assignments (for channels targetting particular species)
  104. target = Array.apply(null, new Array(252)).map(Number.prototype.valueOf,0);
  105. for (i = 0, len = target.length; i < len; i++) {
  106.     if (new Set([143, 149, 181, 201, 242, 248]).has(i)) {target[i] = pglbig}
  107.     else if (new Set([179, 180]).has(i)) {target[i] = pyschannel4}
  108.     else if (new Set([246, 247]).has(i)) {target[i] = pgllarvitar}
  109. }
  110.  
  111. var known_despawns = []; // keeps track of Pokemon already notified
  112. var first_run = 0; // the bot skips Pokemon already on the map when it's started
  113. setTimeout(function(){notifyD('Start', pysgeneral)}, 4 * 1000);
  114.  
  115. // loop (from here to end of code)
  116. var timer = setInterval(function() {
  117.  
  118. b = [];
  119.  
  120. // polls map for desired Pokemon
  121. for (i = 0, len = this.pokemons.length; i < len; i++) {
  122.     test = this.pokemons[i];
  123.     test_iv = (test.attack + test.defence + test.stamina) - 45;
  124.     if (test_iv >= iv[test.id]) {
  125.         test_x = Math.abs(test.center.lat - home_lat)*110.574;
  126.         test_y = Math.abs(test.center.lng - home_lng)*69.298;
  127.         if (test_x*test_x + test_y*test_y < r[test.id]*r[test.id]) {
  128.             test.iv = test_iv;
  129.             b.push(test);
  130.         }
  131.     }          
  132. }
  133.  
  134. // sends out notifications for polled Pokemon
  135. for (i = 0, len = b.length; i < len; i++) {
  136.     if (known_despawns.indexOf(b[i].despawn) != -1) { continue; };
  137.     known_despawns.push(b[i].despawn);
  138.     if (first_run == 0) { continue; };
  139.     notif = tellD(b[i]);
  140.     if (notif == null) {console.log('Skipped'); continue; };
  141.     if (target[b[i].id] != 0) {notifyImD(notif, StatIm(b[i]), target[b[i].id])}
  142.     if (b[i].iv == 0 || (b[i].iv >= -2 && b[i].id == 113)) {notifyImD(notif, StatIm(b[i]), pglperfect)}
  143. }
  144.  
  145. first_run = 1;
  146. console.log('Ran at '+time())
  147. }, 30 * 1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement