Advertisement
Guest User

Update

a guest
Sep 4th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var https = require('https');
  2. var irc = require('irc');
  3.  
  4. if (!Date.now) {
  5.     Date.now = function() { return new Date().getTime(); }
  6. }
  7.  
  8. var startTime;
  9. var num = 45;
  10. var totalNum = 0;
  11. var suffix = "";
  12. var trihardCount = "0";
  13. var dispCount = false;
  14. var client;
  15. var prevCount = trihardCount;
  16. var intervalId = null;
  17. var updateCountIntervalId = null;
  18.  
  19. var bannedCheck = 0;
  20. var bannedTime;
  21.  
  22. var options = {
  23.     host: "twitchstats.io",
  24.     port: 443,
  25.     path: "/api/channel/ice_poseidon/initial",
  26.     method: "GET",
  27.     rejectUnauthorized: false
  28. };
  29.  
  30. function numberWithSpaces(x) {
  31.     return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
  32. }
  33.  
  34. function updateTriHardCount() {
  35.     var req = https.request(options, function(res){
  36.         res.on('data', function(d){
  37.             d = d.toString();
  38.             var index = d.indexOf(',"name":"TriHard"');
  39.             var end = index - 1;
  40.             while (d.charAt(--index) != ':') {}
  41.             trihardCount = d.substr(index+1, end-index);
  42.             req.destroy();
  43.         });
  44.     });
  45.     req.end();
  46.    
  47.     req.on('error', function(e){
  48.         console.error(e);
  49.     });
  50. }
  51.  
  52. function sendTriHards() {
  53.     if (prevCount == trihardCount) {
  54.         suffix += ".";
  55.         if (suffix == "....") {
  56.             suffix = ".";
  57.         }
  58.     } else {
  59.         prevCount = trihardCount;
  60.         suffix = "";
  61.     }
  62.    
  63.     var msg = "";
  64.    
  65.     for (var i = 0; i < 18; i++)
  66.         msg += " TriHard";
  67.    
  68.     msg += " ───────────────────────────────── ";
  69.     msg += " - - - - - - - - - - - - - COUNT: " + numberWithSpaces(trihardCount) + " - - - - - - - - - - - - - ";
  70.     msg += " ───────────────────────────────── ";
  71.    
  72.     for (var i = 18; i < num; i++)
  73.         msg += " TriHard";
  74.  
  75.     totalNum += num;
  76.    
  77.     var seconds = (Date.now() - startTime) / 1000;
  78.     var time = "";
  79.     var weeks = Math.floor(seconds / 604800);
  80.     seconds = seconds - weeks * 604800;
  81.     var days = Math.floor(seconds / 86400);
  82.     seconds = seconds - days * 86400;
  83.     var hours = Math.floor(seconds / 3600);
  84.     seconds = seconds - hours * 3600;
  85.     var minutes = Math.floor(seconds / 60);
  86.     seconds = seconds - minutes * 60;
  87.    
  88.     if (weeks > 0) {
  89.         time += weeks + "w ";
  90.     }
  91.     if (days > 0) {
  92.         time += days + "d ";
  93.     }
  94.     if (hours > 0) {
  95.         time += hours + "h ";
  96.     }
  97.     if (minutes > 0) {
  98.         time += minutes + "m ";
  99.     }
  100.     if (seconds > 0) {
  101.         time += parseFloat(seconds).toFixed(3) + "s";
  102.     }
  103.    
  104.     console.log("saying: " + msg + " " + suffix);
  105.     client.say('#ice_poseidon', msg + " " + suffix);
  106.     console.log("Posted TriHard x " + num + ". Total TriHards: " + numberWithSpaces(trihardCount) + ". Total TriHards by this bot: " + totalNum + ".\tUptime: " + time + ".");
  107. }
  108.  
  109. function init() {
  110.     var config = {
  111.         channels: ['#ice_poseidon'],
  112.         username: '',
  113.         password: 'oauth:'
  114.     };
  115.     console.log("Fetched irc module.");
  116.     client = new irc.Client('irc.chat.twitch.tv', config.username, config);
  117.    
  118.     updateCountIntervalId = setInterval(updateTriHardCount, 2000);
  119.    
  120.     client.addListener('registered', function(message){
  121.         if (intervalId == null) {
  122.             updateTriHardCount();
  123.             startTime = Date.now();
  124.             intervalId = setInterval(sendTriHards, 2500);
  125.         } else {
  126.             if (bannedCheck == 0)
  127.                 bannedTime = Date.now();
  128.             if (Date.now() - bannedTime > 60000)
  129.                 bannedCheck = 0;
  130.             bannedCheck++;
  131.             if (bannedCheck == 5) {
  132.                 clearInterval(intervalId);
  133.                 clearInterval(updateCountIntervalId);
  134.                 client.disconnect();
  135.                 console.log("Probably banned...");
  136.             }
  137.         }
  138.     });
  139. }
  140.  
  141. init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement