Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Discord = require('discord.js');
- let bot = new Discord.Client();
- let observedChannelNames = ["tro-100", "tro-95", "tro-90", "tro-endgame", "tro-rare", "tro-0", "tro-unown"];
- const fs = require('fs');
- const pokemonList = JSON.parse(fs.readFileSync('pokemonList.json', 'utf8'));
- const tokentok = JSON.parse(fs.readFileSync('auth.json', 'utf8'));
- const token = tokentok.token;
- const baseConfig = {
- minIV: 0,
- maxIV: 100,
- minLVL: 1,
- maxLVL: 30,
- minTime: 0,
- maxDistance: 250
- };
- const userConfig = {
- "219125851239874560": {
- config: {
- minIV: 0,
- maxIV: 100,
- minLVL: 1,
- maxLVL: 30,
- minTime: 0,
- maxDistance: 250,
- latitude: 54.4057578,
- longitude: 18.5599494
- },
- list: [
- {
- name: "Dratini",
- minIV: 0,
- maxIV: 100,
- minLVL: 1,
- maxLVL: 30,
- minTime: 15 * 60,
- maxDistance: 5
- },
- {
- name: "Dragonair",
- minIV: 0,
- maxIV: 100,
- minLVL: 1,
- maxLVL: 30,
- minTime: 0,
- maxDistance: 250
- },
- {
- name: "Dragonite",
- minIV: 0,
- maxIV: 100,
- minLVL: 1,
- maxLVL: 30,
- minTime: 0,
- maxDistance: 250
- }
- ]
- }
- };
- let channels = {};
- let calculateDistance = function (lat1, lon1, lat2, lon2) {
- let R = 6371; // km
- let dLat = toRad(lat2 - lat1);
- let dLon = toRad(lon2 - lon1);
- let lat11 = toRad(lat1);
- let lat22 = toRad(lat2);
- let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
- Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat11) * Math.cos(lat22);
- let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
- let d = R * c;
- return parseFloat(d.toFixed(2));
- };
- // Converts numeric degrees to radians
- function toRad(Value) {
- return Value * Math.PI / 180;
- }
- let parseMessage = function (message) {
- let embed = message.embeds[0];
- let title = embed.title;
- let titlePattern = /([0-9]{1,2}) lvl, ([0-9]{2,4}) CP, do ([0-9:]*)/i;
- let description = embed.description;
- let descriptionPattern = /Atak: ([0-9]{1,2}), Def: ([0-9]{1,2}), Sta: ([0-9]{1,2}), ([a-z ]*) \/ ([a-z ]*)(?:[\s])(?:\s+[a-zę :]*)([0-9:]*)(?:\s[(a-zł: ]*)([0-9]*)m ([0-9]*)s(?:[\s)at]*)([0-9 a-z]*)/i;
- let embedUrl = embed.url;
- let mapUrlPattern = /http:\/\/maps\.google\.com\/maps\?q=([0-9.-]*),([0-9.-]*)/i;
- let thumbnailUrl = embed.thumbnail.url;
- let thumbnailPattern = /([0-9]+)/;
- let pokemonNumber = parseInt(thumbnailUrl.match(thumbnailPattern)[1]);
- let titleArray = title.match(titlePattern);
- let pokemonLevel = parseInt(titleArray[1]);
- let pokemonCombatPower = parseInt(titleArray[2]);
- let descriptionArray = description.match(descriptionPattern);
- let pokemonAttack = parseInt(descriptionArray[1]);
- let pokemonDefence = parseInt(descriptionArray[2]);
- let pokemonStamina = parseInt(descriptionArray[3]);
- let fastAttack = descriptionArray[4];
- let chargeAttack = descriptionArray[5];
- let despawnTime = descriptionArray[6];
- let minutesLeft = descriptionArray[7];
- let secondsLeft = descriptionArray[8];
- let location = descriptionArray[9];
- let pokemonIV = ((pokemonAttack + pokemonDefence + pokemonStamina) * 100 / 45).toFixed(1);
- let mapUrlArray = embedUrl.match(mapUrlPattern);
- let pokemonLatitude = mapUrlArray[1];
- let pokemonLongitude = mapUrlArray[2];
- return {
- number: pokemonNumber,
- name: pokemonList[pokemonNumber],
- level: pokemonLevel,
- combatPower: pokemonCombatPower,
- attack: pokemonAttack,
- defence: pokemonDefence,
- stamina: pokemonStamina,
- IV: pokemonIV,
- fastAttack: fastAttack,
- chargeAttack: chargeAttack,
- timeLeft: 60 * minutesLeft + secondsLeft,
- despawnTime: despawnTime,
- location: location,
- latitude: pokemonLatitude,
- longitude: pokemonLongitude,
- title: `${pokemonList[pokemonNumber]} ${pokemonIV}% ${pokemonLevel}lvl ${minutesLeft}:${secondsLeft} left`,
- description: embed.description,
- url: embed.url,
- thumbnail: {url: embed.thumbnail.url},
- image: {url: embed.image.url}
- };
- };
- bot.on("ready", () => {
- for (let userID in userConfig) {
- bot.fetchUser(userID).then(channel => {
- channels[userID] = channel;
- });
- }
- bot.on("message", message => {
- let channelName = message.channel.name;
- if (observedChannelNames.indexOf(channelName) !== -1) {
- if (message.author.username === "PokeAlarm") {
- return;
- }
- let pokemonData = parseMessage(message);
- let embd = new Discord.RichEmbed({
- title: pokemonData.title,
- description: pokemonData.description,
- url: pokemonData.url,
- thumbnail: pokemonData.thumbnail,
- image: pokemonData.image
- });
- for (let userID in userConfig) {
- let config = Object.assign({}, userConfig[userID].config, baseConfig);
- userConfig[userID].list.forEach(pokemon => {
- let pokemonConfig = Object.assign({}, pokemon, config);
- if (pokemonConfig.name !== pokemonData.name) {
- console.log(`name ${pokemonData.name} doesn't match ${pokemonConfig.name}`);
- return;
- }
- if (pokemonConfig.minIV > pokemonData.IV) {
- console.log(`IV ${pokemonData.IV} is smaller than ${pokemonConfig.minIV}`);
- return;
- }
- if (pokemonConfig.maxIV < pokemonData.IV) {
- console.log(`IV ${pokemonData.IV} is greater than ${pokemonConfig.maxIV}`);
- return;
- }
- if (pokemonConfig.minLVL > pokemonData.level) {
- console.log(`LVL ${pokemonData.level} is smaller than ${pokemonConfig.minLVL}`);
- return;
- }
- if (pokemonConfig.maxLVL < pokemonData.level) {
- console.log(`LVL ${pokemonData.level} is greater than ${pokemonConfig.maxLVL}`);
- return;
- }
- if (pokemonConfig.minTime > pokemonData.timeLeft) {
- console.log(`time ${pokemonData.timeLeft} is smaller than ${pokemonConfig.minTime}`);
- return;
- }
- let distance = calculateDistance(pokemonConfig.latitude, pokemonConfig.longitude, pokemonData.latitude, pokemonData.longitude);
- if (distance > pokemonConfig.maxDistance) {
- console.log(`distance ${distance} is greater than ${pokemonConfig.maxDistance}`);
- return;
- }
- let channel = channels[userID];
- channel.send(embd);
- });
- }
- }
- });
- });
- bot.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement