Advertisement
Guest User

Updated script

a guest
Sep 4th, 2016
108
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.  
  10. var totalNum = 0;
  11. var num = 69;
  12. var suffix = ".";
  13. var trihardCount = "0";
  14. var dispCount = false;
  15. var client;
  16. var prevCount = trihardCount;
  17. var intervalId = null;
  18.  
  19. var options = {
  20.     host: "twitchstats.io",
  21.     port: 443,
  22.     path: "/api/channel/ice_poseidon/initial",
  23.     method: "GET",
  24.     rejectUnauthorized: false
  25. };
  26.  
  27. function numberWithSpaces(x) {
  28.     return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, " ");
  29. }
  30.  
  31. function updateTriHardCount() {
  32.     var req = https.request(options, function(res){
  33.         res.on('data', function(d){
  34.             d = d.toString();
  35.             var index = d.indexOf(',"name":"TriHard"');
  36.             var end = index - 1;
  37.             while (d.charAt(--index) != ':') {}
  38.             trihardCount = d.substr(index+1, end-index);
  39.             req.destroy();
  40.         });
  41.     });
  42.     req.end();
  43.    
  44.     req.on('error', function(e){
  45.         console.error(e);
  46.     });
  47. }
  48.  
  49. function sendTriHards() {
  50.     if (prevCount == trihardCount) {
  51.         suffix += ".";
  52.         if (suffix == "....") {
  53.             suffix = ".";
  54.         }
  55.     } else {
  56.         prevCount = trihardCount;
  57.         suffix = "";
  58.     }
  59.    
  60.     var msg = "TriHard Count: " + numberWithSpaces(trihardCount);
  61.  
  62.     totalNum++;
  63.    
  64.     var seconds = (Date.now() - startTime) / 1000;
  65.     var time = "";
  66.     var weeks = Math.floor(seconds / 604800);
  67.     seconds = seconds - weeks * 604800;
  68.     var days = Math.floor(seconds / 86400);
  69.     seconds = seconds - days * 86400;
  70.     var hours = Math.floor(seconds / 3600);
  71.     seconds = seconds - hours * 3600;
  72.     var minutes = Math.floor(seconds / 60);
  73.     seconds = seconds - minutes * 60;
  74.    
  75.     if (weeks > 0) {
  76.         time += weeks + "w ";
  77.     }
  78.     if (days > 0) {
  79.         time += days + "d ";
  80.     }
  81.     if (hours > 0) {
  82.         time += hours + "h ";
  83.     }
  84.     if (minutes > 0) {
  85.         time += minutes + "m ";
  86.     }
  87.     if (seconds > 0) {
  88.         time += parseFloat(seconds).toFixed(3) + "s";
  89.     }
  90.    
  91.     client.say('#ice_poseidon', msg + suffix);
  92.     console.log("Total TriHards: " + numberWithSpaces(trihardCount) + ". Total TriHards by this bot: " + totalNum + ".\tUptime: " + time + ".");
  93. }
  94.  
  95. function init() {
  96.     var config = {
  97.         channels: ['#ice_poseidon'],
  98.         username: '',
  99.         password: '',
  100.         floodProtection: false,
  101.         floodProtectionDelay: 2000
  102.     };
  103.     console.log("Fetched irc module.");
  104.     client = new irc.Client('irc.chat.twitch.tv', config.username, config);
  105.    
  106.     setInterval(updateTriHardCount, 2000);
  107.    
  108.     client.addListener('registered', function(message){
  109.         if (intervalId == null) {
  110.             startTime = Date.now();
  111.             intervalId = setInterval(sendTriHards, 2000);
  112.         }
  113.     });
  114. }
  115.  
  116. init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement