Advertisement
Guest User

Untitled

a guest
Dec 6th, 2017
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports.run = async (bot, message, args) => {
  2.     //Const vars
  3.     const {PubgAPI, PubgAPIErrors, REGION, MATCH} = require('pubg-api-redis');
  4.     const SEASON = {
  5.         EA2017pre1: '2017-pre1',
  6.         EA2017pre2: '2017-pre2',
  7.         EA2017pre3: '2017-pre3',
  8.         EA2017pre4: '2017-pre4',
  9.         EA2017pre5: '2017-pre5',
  10.         EA2017pre6: '2017-pre6'
  11.     };
  12.     const pubgAPI   = new PubgAPI({apikey: bot.settingscfg.pubgapikey});
  13.     const mysql     = require('mysql');
  14.  
  15.     //Function
  16.     if (bot.servers.get(message.guild.id).pubgRanksDistribute) {
  17.         //Get the configuration file for the servers PUBGsettings
  18.         var fs = require('fs');
  19.         var filename = `/home/TokaHimiko/pubgsettings/${message.guild.id}.json`;
  20.         var pubgsettings = require(filename);
  21.  
  22.         var isLinked = true;
  23.         var playerinfo;
  24.         var playertofind;
  25.         if (pubgsettings.players.length < 1) {
  26.             //If there are no players linked in the server yet.. Defaulting to false.
  27.             isLinked = false;
  28.         }
  29.         if (isLinked) {
  30.             isLinked = false; //Setting value to false as we don't know if the player is in the serverlist yet.
  31.             if (args.length >= 1) {
  32.                 playertofind = message.mentions.users.first().id;
  33.             } else {
  34.                 playertofind = message.author.id;
  35.             }
  36.             pubgsettings.players.forEach((player, i) => {
  37.                 if (player.player.discordid == playertofind) {
  38.                     isLinked = true;
  39.                     playerinfo = player.player;
  40.                 }
  41.             });
  42.         }
  43.         if (isLinked) {
  44.             var con = mysql.createConnection({
  45.                 host: bot.settingscfg.mysqlHost,
  46.                 user: bot.settingscfg.mysqlUser,
  47.                 password: bot.settingscfg.mysqlPass,
  48.                 database: bot.settingscfg.mysqlData
  49.             });
  50.             await con.connect(err => {
  51.                 if (err) console.error(err);
  52.                 con.query(`SELECT * FROM pubgranks WHERE discorduser = '${playerinfo.discordid}' AND discordserver = '${message.guild.id}'`, (err, rows) => {
  53.                     if (err) return console.error(err);
  54.                     if (rows.length <= 0) {
  55.                         con.query(`INSERT INTO pubgranks (discordserver, discorduser, pubgname) VALUES ('${message.guild.id}', '${playerinfo.discordid}', '${playerinfo.pubgname}')`, (err, rows) => {
  56.                             if (err) return console.error(err);
  57.                             console.log("Inserted pubgname in mysql database server.");
  58.                         })
  59.                     } else {
  60.                         console.log("user already exists");
  61.                     }
  62.                 });
  63.             });
  64.  
  65.             let awaitmessage = await message.channel.send("```Fetching rankings for " + playerinfo.pubgname + ". \nThis may take up to 2 minutes.```");
  66.            
  67.             try {
  68.  
  69.                 pubgAPI.getProfileByNickname(playerinfo.pubgname).
  70.                     then(async(profile) =>  {
  71.  
  72.                         const data = profile.content;
  73.                         var contentstats = data.Stats;
  74.                     });
  75.  
  76.             } catch (err) {
  77.                 console.error(err);
  78.                 awaitmessage.delete();
  79.             }
  80.  
  81.             if(stats.length > 0) {
  82.  
  83.                 var {solo, duo, squad, solofpp, duofpp, squadfpp} = false;
  84.                 var {na, eu, as, oc, sa, sea, krjp} = false;
  85.                 var bestRank = 20000000;
  86.  
  87.                 contentstats.forEach((stat, i) => {
  88.                     //EU Checks
  89.                     if (stat.Season === SEASON.EA2017pre6 && stat.Region == 'eu') {
  90.                         eu = true;
  91.                         switch (stat.Match) {
  92.                             case "solo":
  93.                                 solo = false;
  94.                                 break;
  95.                             case "duo":
  96.                                 duo = false;
  97.                                 break;
  98.                             case "squad":
  99.                                 squad = true;
  100.                                 break;
  101.                             case "solo-fpp":
  102.                                 solofpp = false;
  103.                                 break;
  104.                             case "duo-fpp":
  105.                                 duofpp = false;
  106.                                 break;
  107.                             case "squad-fpp":
  108.                                 squadfpp = true;
  109.                                 break;
  110.                         }
  111.                     }
  112.                 });
  113.  
  114.                 var stats;
  115.                 //Fetching stats for each Region!
  116.                 if (eu) {
  117.                     if (solo) {
  118.                         stats = profile.getStats({
  119.                             region: 'eu',
  120.                             season: SEASON.EA2017pre6,
  121.                             match: 'solo'
  122.                         });
  123.                         if (stats.rankData.rating <= bestRank) {
  124.                             bestRank = stats.rankData.rating;
  125.                         }
  126.                     }
  127.                     if (duo) {
  128.                         stats = profile.getStats({
  129.                             region: 'eu',
  130.                             season: SEASON.EA2017pre6,
  131.                             match: 'duo'
  132.                         });
  133.                         if (stats.rankData.rating <= bestRank) {
  134.                             bestRank = stats.rankData.rating;
  135.                         }
  136.                     }
  137.                     if (squad) {
  138.                         stats = profile.getStats({
  139.                             region: 'eu',
  140.                             season: SEASON.EA2017pre6,
  141.                             match: 'squad'
  142.                         });
  143.                         if (stats.rankData.rating <= bestRank) {
  144.                             bestRank = stats.rankData.rating;
  145.                         }
  146.                     }
  147.                     if (solofpp) {
  148.                         stats = profile.getStats({
  149.                             region: 'eu',
  150.                             season: SEASON.EA2017pre6,
  151.                             match: 'solo-fpp'
  152.                         });
  153.                         if (stats.rankData.rating <= bestRank) {
  154.                             bestRank = stats.rankData.rating;
  155.                         }
  156.                     }
  157.                     if (duofpp) {
  158.                         stats = profile.getStats({
  159.                             region: 'eu',
  160.                             season: SEASON.EA2017pre6,
  161.                             match: 'duo-fpp'
  162.                         });
  163.                         if (stats.rankData.rating <= bestRank) {
  164.                             bestRank = stats.rankData.rating;
  165.                         }
  166.                     }
  167.                     if (squadfpp) {
  168.                         stats = profile.getStats({
  169.                             region: 'eu',
  170.                             season: SEASON.EA2017pre6,
  171.                             match: 'squad-fpp'
  172.                         });
  173.                         if (stats.rankData.rating <= bestRank) {
  174.                             bestRank = stats.rankData.rating;
  175.                         }
  176.                     }
  177.                 }
  178.                
  179.                 if (args.length >= 1) {
  180.                     message.channel.send("```" + message.author.username + ", I've updated the rank of " + message.mentions.users.first().username + " accordingly to their best rank available: " + bestRank + "```");
  181.                 } else {
  182.                     message.channel.send("```" + message.author.username + ", I've updated your rank accordingly to your best rank available: " + bestRank + "```");
  183.                 }
  184.                 con.query(`UPDATE pubgranks SET currentRank = "${bestRank}" WHERE discordserver = "${message.guild.id}" AND discorduser = "${playerinfo.discordid}"`, (err, rows) => {
  185.                     if (err) return console.error(err);
  186.                     console.log("Updated the database");
  187.                 });
  188.                 awaitmessage.delete();
  189.  
  190.             }
  191.  
  192.         } else {
  193.             let prefix = await bot.servers.get(message.guild.id).prefix;
  194.             if (args.length >= 1) {
  195.                 message.channel.send(`${message.author}, ${message.mentions.users.first()} has not yet linked their account to this server. `);
  196.             } else {
  197.                 message.channel.send(`You've not yet linked your PUBG name to the bot in this server. Please use ${prefix}pubgaccount <username>`);
  198.             }
  199.         }
  200.     } else {
  201.         message.channel.send(`This server has not enabled this command. Message an server administrator to enable it.`);
  202.     }
  203.  
  204. }
  205.  
  206. module.exports.commandinfo = async (bot, message, args) => {
  207.     let prefix = bot.servers.get(message.guild.id).prefix;
  208.     let botcolor = await message.guild.fetchMember(bot.user.id).displayHexColor;
  209.     var embed = {
  210.         embed: {
  211.             "title": "Help command for <command>",
  212.             "description": "",
  213.             "fields": [
  214.             {
  215.                 "name": "Usage: " + prefix + "",
  216.                 "value": "",
  217.                 "inline": false
  218.             }
  219.             ],
  220.             "color": 3485
  221.             }
  222.     }
  223.     return embed;
  224. }
  225.  
  226. module.exports.help = {
  227.     name: "pubgrole",
  228.     alias: "pupdate",
  229.     permission: "none",
  230.     category: "pubg",
  231.     helpcommand: false
  232. }
  233.  
  234. //Usage:
  235. // name: = command name, for example "help"
  236. // permission: = level of permission, insert one of the flags: https://discord.js.org/#/docs/main/stable/class/Permissions?scrollTo=s-FLAGS
  237. // category: category of the command
  238. // helpcommand: if there is an help command available
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement