Guest User

Untitled

a guest
Jan 1st, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var bot = {};
  2.  
  3. bot.opts = {
  4.  
  5.     initiator: '//',
  6.     separator: ' ',
  7.  
  8.     inputId: 'input',
  9.     sendId: 'sayit-button',
  10.     containerId: 'chat',
  11.  
  12.     minTimeMS: 5000,
  13.     updateInterval: 100,
  14. }
  15.  
  16. bot.init = function(){
  17.  
  18.     bot.inputElement = document.getElementById( bot.opts.inputId );
  19.     bot.sendElement = document.getElementById( bot.opts.sendId );
  20.     bot.chatElement = document.getElementById( bot.opts.containerId );
  21.  
  22.     bot.observer = new MutationObserver( function( mutations ){
  23.  
  24.         mutations.map( function( mutation ){
  25.  
  26.             var added = mutation.addedNodes;
  27.             if( added.length > 0 ){
  28.  
  29.                 for( var i = 0; i < added.length; ++i ){
  30.  
  31.                     if( added[ i ].nodeName === 'DIV' )
  32.                         bot.evaluateNode( added[ i ] );
  33.                 }
  34.             }
  35.         });
  36.     });
  37.  
  38.     bot.observer.observe( bot.chatElement, { childList: true, subtree: true } );
  39.  
  40.     bot.lastUser = 'towc';
  41.     bot.lastUse = Date.now();
  42.     bot.lastSent = Date.now();
  43.     bot.awaken = Date.now();
  44.  
  45.     bot.messages = [];
  46.  
  47.     window.setInterval( bot.update, bot.opts.updateInterval );
  48. }
  49. bot.update = function(){
  50.  
  51.     var time = Date.now();
  52.  
  53.     if( time - bot.lastSent > bot.opts.minTimeMS && bot.messages.length > 0 ){
  54.  
  55.         bot.inputElement.value = ' - ' + bot.messages.shift();
  56.         bot.sendElement.click();
  57.  
  58.         bot.lastSent = time;
  59.     }
  60. }
  61. bot.evaluateNode = function( node ){
  62.  
  63.     var user = bot.lastUser,
  64.         message = '',
  65.         relevant = true;
  66.  
  67.     if( node.classList.contains( 'message' ) ){
  68.  
  69.         if( node.classList.contains( 'pending' ) ){
  70.  
  71.             // this is completely broken because jQuery frigging throws an error and the mutation observer breaks, I think
  72.  
  73.             if( node.childNodes.length > 1 ){
  74.  
  75.                 var milliseconds = ( +node.childNodes[ 1 ].childNodes[ 0 ].split( ' ' )[ 9 ] ) * 1000 + 1000,
  76.                     retryElement = node.childNodes[ 1 ].childNodes[ 1 ];
  77.  
  78.                 window.setTimeout( (function( el ){ return function(){ el.click(); } })( retryElement ), milliseconds );
  79.             }
  80.  
  81.         } else {
  82.  
  83.             user = node.parentElement.parentElement.childNodes[ 0 ].childNodes[ 2 ].textContent,
  84.             message = node.childNodes[ 1 ].textContent;
  85.         }
  86.  
  87.     } else relevant = false;
  88.  
  89.     if( relevant )
  90.         bot.receive( user, message );
  91. }
  92. bot.send = function( message ){
  93.  
  94.     bot.messages.push( message );
  95. }
  96. bot.receive = function( user, message ){
  97.  
  98.     if( message.indexOf( bot.opts.initiator ) === 0 ){
  99.  
  100.         bot.evaluate( user, message );
  101.  
  102.         bot.lastUser = user;
  103.         bot.lastUse = Date.now();
  104.     }
  105. }
  106. bot.evaluate = function( user, message ){
  107.  
  108.     var array = message.split( bot.opts.initiator ),
  109.         cmdName = '',
  110.         cmdArgs = [];
  111.  
  112.     array.shift();
  113.     array = array.join( bot.opts.initiator ).split( bot.opts.separator );
  114.  
  115.     cmdName = array.shift();
  116.     cmdArgs = array;
  117.  
  118.     var command = bot.getCommand( cmdName );
  119.  
  120.     if( command.isCommand )
  121.         command.execute( cmdArgs, user );
  122.     else bot.send( 'command not found.\nType //commands for a list of commands' );
  123.  
  124. }
  125.  
  126. bot.Command = function( name, description, fn ){
  127.  
  128.     this.name = name;
  129.     this.description = description;
  130.     this.fn = fn;
  131.     this.isCommand = true;
  132.  
  133.     this.lastCall = Date.now();
  134.     this.lastUser = 'none';
  135.     this.lastArgs = [];
  136. }
  137. bot.Command.prototype.execute = function( args, user ){
  138.  
  139.  
  140.     var lastArgs = [];
  141.  
  142.     this.lastCall = Date.now();
  143.     this.lastUser = user;
  144.     args.map( function( arg ){ lastArgs.push( arg ); } );
  145.     this.lastArgs = lastArgs;
  146.  
  147.     this.fn( args );
  148. }
  149. bot.Command.prototype.getInfo = function(){
  150.  
  151.     return 'I was last called on ' + ( new Date( this.lastCall ) ) + ' which is ' + ( ( Date.now() - this.lastCall ) / 1000 ) + 's ago by ' + this.lastUser + ' with the following arguments: ' + this.lastArgs.join(', ');
  152. };
  153.  
  154. bot.recalls = {
  155.     'author' : 'towc @ towc.eu',
  156.     'cheese' : 'definitely what you need in your life',
  157.     'the-best' : 'Biba. Duh.'
  158. };
  159. bot.whyAnswers = [
  160.     'Because You\'re aweseome!',
  161.     'Only Jabba knows',
  162.     'No real reason. Blame the government',
  163.     'For the same reason you love cheese',
  164.     'For the same reason cheese loves you',
  165.     'Not like it\'s none of your business'
  166. ];
  167. bot.shortAnswers = [
  168.     'Absolutely',
  169.     'Absolutely not',
  170.     'Could be',
  171.     'No way',
  172.     'Why would you even think that?',
  173.     'How could you doubt that?'
  174. ];
  175. bot.whatAnswers = [
  176.    
  177.     'rainbows. duh',
  178.     'cheese is the only possible answer',
  179.     'why would you care?'
  180. ]
  181.  
  182. bot.commands = [
  183.    
  184.     new bot.Command( 'say', 'Echoes what the user inputted.\nUsage: //say' + bot.opts.separator + '<text>', function( args ){
  185.  
  186.         if( args.length > 0 )
  187.             bot.send( args.join( bot.opts.separator ) );
  188.  
  189.         else
  190.             bot.send( this.description );
  191.     } ),
  192.  
  193.     new bot.Command( 'help', 'Provides information about a certain command.\nUsage: //help' + bot.opts.separator + '<cmd>', function( args ){
  194.  
  195.         if( args.length > 0 ){
  196.  
  197.             var command = bot.getCommand( args[ 0 ] );
  198.  
  199.             if( command.isCommand )
  200.                 bot.send( command.description );
  201.             else bot.send( 'command "' + command + '" not found. Type //commands for a list of commands' );
  202.  
  203.         } else bot.send( this.description );
  204.     } ),
  205.  
  206.     new bot.Command( 'commands', 'Provides a list of commands to use on the bot.\nUsage: //commands', function( args ){
  207.  
  208.         var commandNames = [];
  209.  
  210.         bot.commands.map( function( cmd ){ commandNames.push( cmd.name ); } );
  211.  
  212.         commandNames.sort();
  213.  
  214.         bot.send( commandNames.join( ', ' ) );
  215.     }),
  216.  
  217.     new bot.Command( 'tell', 'Sends the output of a command to someone.\nUsage: //tell' + bot.opts.separator + '<person>' + bot.opts.separator + '<command>' + bot.opts.separator + '<command arguments>', function( args ){
  218.  
  219.         if( args.length >= 2 ){
  220.  
  221.             var person = args.shift();
  222.  
  223.             if( person[ 0 ] === '@' ){
  224.                 bot.send( 'Don\'t double-ping, it\'s annoying' );
  225.  
  226.                 person = person.split('');
  227.                 person.shift();
  228.                 person = person.join('');
  229.             }
  230.  
  231.             else {
  232.  
  233.                 var commandName = args.shift(),
  234.                     command = bot.getCommand( commandName );
  235.  
  236.                 if( command.isCommand ){
  237.  
  238.                     command.execute( args );
  239.  
  240.                     bot.modifyMsg( 'last', function( msg ){
  241.  
  242.                         return '@' + person + ' ' + msg;
  243.                     });
  244.                 } else
  245.                     bot.send( 'command ' + commandName + 'does not appear to exist. Type //commands to get a full list of commands' );
  246.             }
  247.         }
  248.     }),
  249.  
  250.     new bot.Command( 'recall', 'Sends back some text based on certain keys.\nUsage: //recall' + bot.opts.separator + '<key>\nAvailable keys: ' + Object.keys( bot.recalls ).join( ', ' ), function( args ){
  251.  
  252.         if( args.length > 0 ){
  253.             var recall = bot.recalls[  args.join('') ];
  254.  
  255.             if( recall ){
  256.  
  257.                 bot.send( recall );
  258.             } else
  259.                 bot.send( 'There is no such key, sorry :/')
  260.         } else {
  261.  
  262.             bot.getCommand( 'help' ).execute( [ 'recall' ] )
  263.         }
  264.     }),
  265.  
  266.     new bot.Command( 'info', 'Provides info on the bot or on a certain command.\nUsage: //info [<cmd>]', function( args ){
  267.  
  268.         if( args.length === 0 ){
  269.  
  270.             bot.send( 'I am ran on the client by @towc, the man who puts cheese on canvas. The purpose is to make a bot that can be copy-pasted into your console and easily controllable through the console. I was invoked on ' + ( new Date( bot.awaken ) ) + ' through towc\'s holy F12' )
  271.         } else {
  272.  
  273.             var command = bot.getCommand( args[ 0 ] );
  274.  
  275.             if( command.isCommand )
  276.                 bot.send( command.getInfo() )
  277.             else
  278.                 bot.send( 'command ' + args[ 0 ] + ' does not seem to exist. Type //commands for a full list of commands' );
  279.         }
  280.     }),
  281.  
  282.     new bot.Command( 'why', 'Answers to questions. 100% Accurate.\nUsage: //why <question>', function( args ){
  283.  
  284.         if( args.length > 0 )
  285.             bot.send( bot.whyAnswers[ ( bot.whyAnswers.length * Math.random() ) |0 ] );
  286.  
  287.         else
  288.             bot.getCommand( 'help' ).execute( [ 'why' ] );
  289.     }),
  290.  
  291.     new bot.Command( 'are', 'Answers shortly to specific questions.\nUsage: //are <question>', function( args ){
  292.  
  293.         if( args.length > 0 )
  294.             bot.send( bot.shortAnswers[ ( bot.shortAnswers.length * Math.random() ) |0 ] );
  295.  
  296.         else
  297.             bot.getCommand( 'help' ).execute( [ 'are' ] );
  298.     }),
  299.  
  300.     new bot.Command( 'is', 'Answers shortly to specific questions.\nUsage: //is <question>', function( args ){
  301.  
  302.         if( args.length > 0 )
  303.             bot.send( bot.shortAnswers[ ( bot.shortAnswers.length * Math.random() ) |0 ] );
  304.  
  305.         else
  306.             bot.getCommand( 'help' ).execute( [ 'is' ] );
  307.     }),
  308.  
  309.     new bot.Command( 'what', 'Answers to questions starting with "what".\nUsage: //what <question>', function( args ){
  310.  
  311.         if( args.length > 0 )
  312.             bot.send( bot.whatAnswers[ ( bot.whatAnswers.length * Math.random() ) |0 ] );
  313.  
  314.         else
  315.             bot.getCommand( 'help' ).execute( [ 'what' ] );
  316.     })
  317. ];
  318. bot.modifyMsg = function( index, fn ){
  319.  
  320.     if( index === 'last' )
  321.         index = bot.messages.length - 1;
  322.  
  323.     bot.messages[ index ] = fn( bot.messages[ index ] );
  324. }
  325. bot.getCommand = function( name ){
  326.  
  327.     var command = 'not found';
  328.  
  329.     bot.commands.map( function( cmd ){
  330.  
  331.         if( cmd.name === name )
  332.             command = cmd;
  333.     });
  334.  
  335.     return command;
  336. }
  337.  
  338. bot.init();
Advertisement
Add Comment
Please, Sign In to add comment