Advertisement
Guest User

Untitled

a guest
Jul 4th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Twitch Chat Commands
  2. /timeout <username> <time in seconds> -Times out the specified user for x number of seconds
  3.  
  4. /timeout <username> – times out the selected user for ten minutes
  5.  
  6. /timeout <username> 1 – purges the chat of the user. (Actually it times out the user for one second.)
  7.  
  8. /ban <username> – Bans the specified username
  9.  
  10. /mods – Calls up list of channel moderators
  11.  
  12. /slow – Turns slow mode on. I would prefer to be the only one using this command.
  13.  
  14. /slowoff – Turns slow mode off.
  15.  
  16. /clear – Clears the chat. We try to use this command sparingly and opt for the ‘purge' command.
  17.  
  18. **************** IRC Shit ******************
  19. https://node-irc.readthedocs.org/en/latest/API.html#client
  20.  
  21. */
  22. console.log("BOT STARTING...");
  23.  
  24. console.log("LOADING SETTINGS...");
  25. var s = require("./settings.js");
  26. var settings = s.settings;
  27. console.log("LOADED SETTINGS!");
  28.  
  29. console.log("LOADING LIBRARIES...");
  30. var irc = require("irc"),
  31.     fs = require("fs"),
  32.     jf = require("jsonfile"),
  33.     util = require('util');
  34. console.log("LOADED LIBRARIES!");
  35.  
  36.  
  37.  
  38. // CREATING BOT
  39.  
  40. console.log("CREATING BOT...");
  41. var _channels = [];
  42. for(var i = 0; i < settings.channels.length; i++)
  43. {
  44.     _channels.push(settings.channels[i].Name);
  45. }
  46. var bot = new irc.Client(settings.server, settings.botName,
  47. {
  48.     channels:
  49.     [
  50.         _channels + " " + settings.password
  51.     ],
  52.     debug: false,
  53.     password: settings.password,
  54.     username: settings.botName
  55. });
  56. console.log("BOT CREATED!");
  57.  
  58. function System(settings)
  59. {
  60.     this.settings = settings;
  61.  
  62.     this.contextContainsNoLink = text => {
  63.         // var re = /(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/gi;
  64.         // var re = /([a-z0-9_-]+\.)+[a-z]{2,4}\/[^ ]*|(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])|(\s|^)((https?:\/\/|www\.)+[a-z0-9_.\/?=&-]+)/gi;
  65.         var re = /(([a-z]+:\/\/)?(([a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel|tv|local|internal|xxx))(:[0-9]{1,5})?(\/[a-z0-9_\-\.~]+)*(\/([a-z0-9_\-\.]*)(\?[a-z0-9+_\-\.%=&amp;]*)?)?(#[a-zA-Z0-9!$&'()*+.=-_~:@\/?]*)?)(\s+|$)/gi;
  66.        
  67.         if(text.match(re) != null)
  68.         {
  69.             if(text.indexOf('youtube.com') == -1)
  70.             {
  71.                 return false;
  72.             }
  73.             else
  74.             {
  75.                 return true;
  76.             }
  77.         }
  78.         else
  79.         {
  80.             return true;
  81.         }
  82.     }
  83.    
  84.     this.botIsCalled = text =>
  85.     {
  86.         var calledName = text.split(" ")[0];
  87.        
  88.         for(var i = 0; i < this.settings.botNicks.length; i++)
  89.         {
  90.             if(this.settings.botNicks[i] == calledName.toLowerCase())
  91.             {
  92.                 return true;
  93.             }
  94.         }
  95.        
  96.         return false;
  97.     }
  98.    
  99.     this.getChannel = (callback) =>
  100.     {
  101.         for(var i = 0; i < this.settings.channels.length; i++)
  102.         {
  103.             if(callback(this.settings.channels[i]))
  104.             {
  105.                 return this.settings.channels[i];
  106.             }
  107.         }
  108.        
  109.         return null;
  110.     }
  111.    
  112.     this.isAdmin = user =>
  113.     {
  114.         for(var i = 0; i < this.settings.admins.length; i++)
  115.         {
  116.             if(this.settings.admins[i] == user)
  117.             {
  118.                 return true;
  119.             }
  120.         }
  121.        
  122.         return false;
  123.     }
  124. }
  125.  
  126. var System = new System(settings);
  127.  
  128.  
  129.  
  130. function Blacklist(data)
  131. {
  132.     this.data = data;
  133.    
  134.     this.containsInContext = data => {
  135.         var newData = data.split(" ");
  136.         for(var i = 0; i < newData.length; i++)
  137.         {
  138.             if(this.exists(newData[i].toLowerCase()))
  139.             {
  140.                 return true;
  141.             }
  142.         }
  143.        
  144.         return false;
  145.     }
  146.    
  147.     this.getData = () => {
  148.         return this.data;
  149.     }
  150.    
  151.     this.exists = data => {
  152.         for(var i = 0; i < this.data.length; i++)
  153.         {
  154.             if(this.data[i] == data)
  155.             {
  156.                 return true;
  157.             }
  158.         }
  159.        
  160.         return false;
  161.     }
  162.    
  163.     this.add = word => {
  164.         this.data.push(word);
  165.         jf.writeFileSync("bl/words.json", this.data);
  166.     }
  167.    
  168.     this.remove = word => {
  169.         var newData = [];
  170.         for(var i = 0; i < this.data.length; i++)
  171.         {
  172.             if(this.data[i] != word)
  173.             {
  174.                 newData.push(this.data[i]);
  175.             }
  176.         }
  177.        
  178.         this.data = newData;
  179.        
  180.         jf.writeFileSync("bl/words.json", newData);
  181.     }
  182. }
  183.  
  184. var wordBlacklist = new Blacklist(jf.readFileSync("bl/words.json"));
  185.  
  186. // Listen for any message, say to him/her in the room
  187. bot.addListener("message", function (from, channel, text, message)
  188. {  
  189.     if(text[0] == "!")
  190.     {
  191.         var c = System.getChannel(x => x.Name == channel);
  192.         if(c != null)
  193.         {
  194.             var textSplit = text.split(" ");
  195.             var command = textSplit[0];
  196.             var cmd = command.substr(1, command.length-1);
  197.                    
  198.             switch(cmd)
  199.             {
  200.                 case "blacklist":
  201.                                        
  202.                     if(textSplit.length == 4 && System.isAdmin(from.toLowerCase()))
  203.                     {
  204.                         switch(textSplit[1].toLowerCase())
  205.                         {
  206.                             case 'word':
  207.                                 switch(textSplit[2].toLowerCase())
  208.                                 {
  209.                                     case 'add':
  210.                                         var word = textSplit[3].toLowerCase();
  211.                                        
  212.                                         if(!wordBlacklist.exists(word))
  213.                                         {
  214.                                             wordBlacklist.add(word);
  215.                                            
  216.                                             bot.say(channel, "Wort \""+word+"\" hinzugefügt!");
  217.                                         }
  218.                                     break;
  219.                                    
  220.                                     case 'remove':
  221.                                     case 'rem':
  222.                                         var word = textSplit[3].toLowerCase();
  223.                                        
  224.                                         if(wordBlacklist.exists(word))
  225.                                         {
  226.                                             wordBlacklist.remove(word);
  227.                                            
  228.                                             bot.say(channel, "Wort \""+word+"\" entfernt!");
  229.                                         }
  230.                                     break;
  231.                                 }
  232.                             break;
  233.                         }
  234.                     }
  235.                 break;
  236.                
  237.                 case 'clear':
  238.                     if(System.isAdmin(from.toLowerCase()))
  239.                     {
  240.                         bot.say(channel, "/clear");
  241.                     }
  242.                 break;
  243.             }
  244.         }
  245.     }
  246.     else if(wordBlacklist.containsInContext(text) && !System.isAdmin(from))
  247.     {
  248.         bot.say(channel, "/timeout "+from+" 1");
  249.         bot.say(channel, settings.texts.blacklist[0].replace("%name%", from));
  250.     }
  251.     else if(!System.contextContainsNoLink(text) && !System.isAdmin(from))
  252.     {
  253.         bot.say(channel, "/timeout "+from+" 1");
  254.         bot.say(channel, settings.texts.blacklist[1].replace("%name%", from));
  255.     }
  256.     else if(System.botIsCalled(text))
  257.     {
  258.         if(System.isAdmin(from))
  259.         {
  260.             var t = text.split(" ");
  261.             var cmd = "";
  262.             for(var i = 1; i < t.length; i++)
  263.             {
  264.                 cmd += t[i];
  265.             }
  266.            
  267.             bot.say(channel, cmd);
  268.         }
  269.     }
  270. });
  271.  
  272. // Listen for joins
  273. bot.addListener("join", (channel, who) =>
  274. {
  275.     var c = System.getChannel(x => x.Name == channel);
  276.     if(c != null)
  277.     {
  278.         c.Viewers = c.Viewers + 1;
  279.     }
  280. });
  281.  
  282. bot.addListener("part", (channel, nick, reason, message) =>
  283. {
  284.  
  285. });
  286.  
  287. bot.addListener("quit", (nick, reason, channels, message) =>
  288. {
  289.  
  290. });
  291.  
  292.  
  293. // BOT CONNECTING
  294.  
  295. console.log("BOT CONNECTING...");
  296. bot.addListener("connect",  () => {
  297.     bot.say(settings.channels[0].Name, settings.texts.system[0]
  298.         .replace("%bot%", settings.botName)
  299.         .replace("%channel%", settings.channels[0].Name)
  300.     );
  301.    
  302.     console.log("BOT CONNECTED");
  303. });
  304.  
  305. console.log("BOT STARTED!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement