Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require('discord.js');
  2. let bot = new Discord.Client();
  3. let observedChannelNames = ["tro-100", "tro-95", "tro-90", "tro-endgame", "tro-rare", "tro-0", "tro-unown"];
  4.  
  5. const fs = require('fs');
  6. const pokemonList = JSON.parse(fs.readFileSync('pokemonList.json', 'utf8'));
  7. const tokentok = JSON.parse(fs.readFileSync('auth.json', 'utf8'));
  8. const token = tokentok.token;
  9.  
  10. const baseConfig = {
  11.     minIV: 0,
  12.     maxIV: 100,
  13.     minLVL: 1,
  14.     maxLVL: 30,
  15.     minTime: 0,
  16.     maxDistance: 250
  17. };
  18.  
  19. const userConfig = {
  20.     "219125851239874560": {
  21.         config: {
  22.             minIV: 0,
  23.             maxIV: 100,
  24.             minLVL: 1,
  25.             maxLVL: 30,
  26.             minTime: 0,
  27.             maxDistance: 250,
  28.             latitude: 54.4057578,
  29.             longitude: 18.5599494
  30.         },
  31.         list: [
  32.             {
  33.                 name: "Dratini",
  34.                 minIV: 0,
  35.                 maxIV: 100,
  36.                 minLVL: 1,
  37.                 maxLVL: 30,
  38.                 minTime: 15 * 60,
  39.                 maxDistance: 5
  40.             },
  41.             {
  42.                 name: "Dragonair",
  43.                 minIV: 0,
  44.                 maxIV: 100,
  45.                 minLVL: 1,
  46.                 maxLVL: 30,
  47.                 minTime: 0,
  48.                 maxDistance: 250
  49.             },
  50.             {
  51.                 name: "Dragonite",
  52.                 minIV: 0,
  53.                 maxIV: 100,
  54.                 minLVL: 1,
  55.                 maxLVL: 30,
  56.                 minTime: 0,
  57.                 maxDistance: 250
  58.             }
  59.         ]
  60.     }
  61. };
  62. let channels = {};
  63.  
  64. let calculateDistance = function (lat1, lon1, lat2, lon2) {
  65.     let R = 6371; // km
  66.     let dLat = toRad(lat2 - lat1);
  67.     let dLon = toRad(lon2 - lon1);
  68.     let lat11 = toRad(lat1);
  69.     let lat22 = toRad(lat2);
  70.     let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
  71.         Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat11) * Math.cos(lat22);
  72.     let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  73.     let d = R * c;
  74.     return parseFloat(d.toFixed(2));
  75. };
  76.  
  77. // Converts numeric degrees to radians
  78. function toRad(Value) {
  79.     return Value * Math.PI / 180;
  80. }
  81.  
  82. let parseMessage = function (message) {
  83.  
  84.     let embed = message.embeds[0];
  85.  
  86.     let title = embed.title;
  87.     let titlePattern = /([0-9]{1,2}) lvl, ([0-9]{2,4}) CP, do ([0-9:]*)/i;
  88.  
  89.     let description = embed.description;
  90.     let descriptionPattern = /Atak: ([0-9]{1,2}), Def: ([0-9]{1,2}), Sta: ([0-9]{1,2}), ([a-z ]*) \/ ([a-z ]*)(?:[\s])(?:\s+[a-:]*)([0-9:]*)(?:\s[(a-: ]*)([0-9]*)m ([0-9]*)s(?:[\s)at]*)([0-9 a-z]*)/i;
  91.  
  92.     let embedUrl = embed.url;
  93.     let mapUrlPattern = /http:\/\/maps\.google\.com\/maps\?q=([0-9.-]*),([0-9.-]*)/i;
  94.  
  95.     let thumbnailUrl = embed.thumbnail.url;
  96.     let thumbnailPattern = /([0-9]+)/;
  97.     let pokemonNumber = parseInt(thumbnailUrl.match(thumbnailPattern)[1]);
  98.  
  99.     let titleArray = title.match(titlePattern);
  100.  
  101.     let pokemonLevel = parseInt(titleArray[1]);
  102.     let pokemonCombatPower = parseInt(titleArray[2]);
  103.  
  104.     let descriptionArray = description.match(descriptionPattern);
  105.     let pokemonAttack = parseInt(descriptionArray[1]);
  106.     let pokemonDefence = parseInt(descriptionArray[2]);
  107.     let pokemonStamina = parseInt(descriptionArray[3]);
  108.  
  109.     let fastAttack = descriptionArray[4];
  110.     let chargeAttack = descriptionArray[5];
  111.     let despawnTime = descriptionArray[6];
  112.     let minutesLeft = descriptionArray[7];
  113.     let secondsLeft = descriptionArray[8];
  114.     let location = descriptionArray[9];
  115.  
  116.     let pokemonIV = ((pokemonAttack + pokemonDefence + pokemonStamina) * 100 / 45).toFixed(1);
  117.  
  118.     let mapUrlArray = embedUrl.match(mapUrlPattern);
  119.  
  120.     let pokemonLatitude = mapUrlArray[1];
  121.     let pokemonLongitude = mapUrlArray[2];
  122.  
  123.     return {
  124.         number: pokemonNumber,
  125.         name: pokemonList[pokemonNumber],
  126.         level: pokemonLevel,
  127.         combatPower: pokemonCombatPower,
  128.         attack: pokemonAttack,
  129.         defence: pokemonDefence,
  130.         stamina: pokemonStamina,
  131.         IV: pokemonIV,
  132.         fastAttack: fastAttack,
  133.         chargeAttack: chargeAttack,
  134.         timeLeft: 60 * minutesLeft + secondsLeft,
  135.         despawnTime: despawnTime,
  136.         location: location,
  137.         latitude: pokemonLatitude,
  138.         longitude: pokemonLongitude,
  139.         title: `${pokemonList[pokemonNumber]} ${pokemonIV}% ${pokemonLevel}lvl ${minutesLeft}:${secondsLeft} left`,
  140.         description: embed.description,
  141.         url: embed.url,
  142.         thumbnail: {url: embed.thumbnail.url},
  143.         image: {url: embed.image.url}
  144.     };
  145. };
  146.  
  147. bot.on("ready", () => {
  148.  
  149.     for (let userID in userConfig) {
  150.         bot.fetchUser(userID).then(channel => {
  151.             channels[userID] = channel;
  152.         });
  153.     }
  154.  
  155.  
  156.     bot.on("message", message => {
  157.         let channelName = message.channel.name;
  158.         if (observedChannelNames.indexOf(channelName) !== -1) {
  159.             if (message.author.username === "PokeAlarm") {
  160.                 return;
  161.             }
  162.             let pokemonData = parseMessage(message);
  163.             let embd = new Discord.RichEmbed({
  164.                 title: pokemonData.title,
  165.                 description: pokemonData.description,
  166.                 url: pokemonData.url,
  167.                 thumbnail: pokemonData.thumbnail,
  168.                 image: pokemonData.image
  169.             });
  170.  
  171.             for (let userID in userConfig) {
  172.                 let config = Object.assign({}, userConfig[userID].config, baseConfig);
  173.                 userConfig[userID].list.forEach(pokemon => {
  174.                     let pokemonConfig = Object.assign({}, pokemon, config);
  175.                     if (pokemonConfig.name !== pokemonData.name) {
  176.                         console.log(`name ${pokemonData.name} doesn't match ${pokemonConfig.name}`);
  177.                        return;
  178.                    }
  179.                    if (pokemonConfig.minIV > pokemonData.IV) {
  180.                        console.log(`IV ${pokemonData.IV} is smaller than ${pokemonConfig.minIV}`);
  181.                        return;
  182.                    }
  183.                    if (pokemonConfig.maxIV < pokemonData.IV) {
  184.                        console.log(`IV ${pokemonData.IV} is greater than ${pokemonConfig.maxIV}`);
  185.                        return;
  186.                    }
  187.                    if (pokemonConfig.minLVL > pokemonData.level) {
  188.                        console.log(`LVL ${pokemonData.level} is smaller than ${pokemonConfig.minLVL}`);
  189.                        return;
  190.                    }
  191.                    if (pokemonConfig.maxLVL < pokemonData.level) {
  192.                        console.log(`LVL ${pokemonData.level} is greater than ${pokemonConfig.maxLVL}`);
  193.                        return;
  194.                    }
  195.                    if (pokemonConfig.minTime > pokemonData.timeLeft) {
  196.                        console.log(`time ${pokemonData.timeLeft} is smaller than ${pokemonConfig.minTime}`);
  197.                        return;
  198.                    }
  199.                    let distance = calculateDistance(pokemonConfig.latitude, pokemonConfig.longitude, pokemonData.latitude, pokemonData.longitude);
  200.                    if (distance > pokemonConfig.maxDistance) {
  201.                        console.log(`distance ${distance} is greater than ${pokemonConfig.maxDistance}`);
  202.                        return;
  203.                    }
  204.                    let channel = channels[userID];
  205.                    channel.send(embd);
  206.                });
  207.            }
  208.        }
  209.    });
  210. });
  211.  
  212.  
  213. bot.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement