Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var bot = {};
- bot.opts = {
- initiator: '//',
- separator: ' ',
- inputId: 'input',
- sendId: 'sayit-button',
- containerId: 'chat',
- minTimeMS: 5000,
- updateInterval: 100,
- }
- bot.init = function(){
- bot.inputElement = document.getElementById( bot.opts.inputId );
- bot.sendElement = document.getElementById( bot.opts.sendId );
- bot.chatElement = document.getElementById( bot.opts.containerId );
- bot.observer = new MutationObserver( function( mutations ){
- mutations.map( function( mutation ){
- var added = mutation.addedNodes;
- if( added.length > 0 ){
- for( var i = 0; i < added.length; ++i ){
- if( added[ i ].nodeName === 'DIV' )
- bot.evaluateNode( added[ i ] );
- }
- }
- });
- });
- bot.observer.observe( bot.chatElement, { childList: true, subtree: true } );
- bot.lastUser = 'towc';
- bot.lastUse = Date.now();
- bot.lastSent = Date.now();
- bot.awaken = Date.now();
- bot.messages = [];
- window.setInterval( bot.update, bot.opts.updateInterval );
- }
- bot.update = function(){
- var time = Date.now();
- if( time - bot.lastSent > bot.opts.minTimeMS && bot.messages.length > 0 ){
- bot.inputElement.value = ' - ' + bot.messages.shift();
- bot.sendElement.click();
- bot.lastSent = time;
- }
- }
- bot.evaluateNode = function( node ){
- var user = bot.lastUser,
- message = '',
- relevant = true;
- if( node.classList.contains( 'message' ) ){
- if( node.classList.contains( 'pending' ) ){
- // this is completely broken because jQuery frigging throws an error and the mutation observer breaks, I think
- if( node.childNodes.length > 1 ){
- var milliseconds = ( +node.childNodes[ 1 ].childNodes[ 0 ].split( ' ' )[ 9 ] ) * 1000 + 1000,
- retryElement = node.childNodes[ 1 ].childNodes[ 1 ];
- window.setTimeout( (function( el ){ return function(){ el.click(); } })( retryElement ), milliseconds );
- }
- } else {
- user = node.parentElement.parentElement.childNodes[ 0 ].childNodes[ 2 ].textContent,
- message = node.childNodes[ 1 ].textContent;
- }
- } else relevant = false;
- if( relevant )
- bot.receive( user, message );
- }
- bot.send = function( message ){
- bot.messages.push( message );
- }
- bot.receive = function( user, message ){
- if( message.indexOf( bot.opts.initiator ) === 0 ){
- bot.evaluate( user, message );
- bot.lastUser = user;
- bot.lastUse = Date.now();
- }
- }
- bot.evaluate = function( user, message ){
- var array = message.split( bot.opts.initiator ),
- cmdName = '',
- cmdArgs = [];
- array.shift();
- array = array.join( bot.opts.initiator ).split( bot.opts.separator );
- cmdName = array.shift();
- cmdArgs = array;
- var command = bot.getCommand( cmdName );
- if( command.isCommand )
- command.execute( cmdArgs, user );
- else bot.send( 'command not found.\nType //commands for a list of commands' );
- }
- bot.Command = function( name, description, fn ){
- this.name = name;
- this.description = description;
- this.fn = fn;
- this.isCommand = true;
- this.lastCall = Date.now();
- this.lastUser = 'none';
- this.lastArgs = [];
- }
- bot.Command.prototype.execute = function( args, user ){
- var lastArgs = [];
- this.lastCall = Date.now();
- this.lastUser = user;
- args.map( function( arg ){ lastArgs.push( arg ); } );
- this.lastArgs = lastArgs;
- this.fn( args );
- }
- bot.Command.prototype.getInfo = function(){
- 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(', ');
- };
- bot.recalls = {
- 'author' : 'towc @ towc.eu',
- 'cheese' : 'definitely what you need in your life',
- 'the-best' : 'Biba. Duh.'
- };
- bot.whyAnswers = [
- 'Because You\'re aweseome!',
- 'Only Jabba knows',
- 'No real reason. Blame the government',
- 'For the same reason you love cheese',
- 'For the same reason cheese loves you',
- 'Not like it\'s none of your business'
- ];
- bot.shortAnswers = [
- 'Absolutely',
- 'Absolutely not',
- 'Could be',
- 'No way',
- 'Why would you even think that?',
- 'How could you doubt that?'
- ];
- bot.whatAnswers = [
- 'rainbows. duh',
- 'cheese is the only possible answer',
- 'why would you care?'
- ]
- bot.commands = [
- new bot.Command( 'say', 'Echoes what the user inputted.\nUsage: //say' + bot.opts.separator + '<text>', function( args ){
- if( args.length > 0 )
- bot.send( args.join( bot.opts.separator ) );
- else
- bot.send( this.description );
- } ),
- new bot.Command( 'help', 'Provides information about a certain command.\nUsage: //help' + bot.opts.separator + '<cmd>', function( args ){
- if( args.length > 0 ){
- var command = bot.getCommand( args[ 0 ] );
- if( command.isCommand )
- bot.send( command.description );
- else bot.send( 'command "' + command + '" not found. Type //commands for a list of commands' );
- } else bot.send( this.description );
- } ),
- new bot.Command( 'commands', 'Provides a list of commands to use on the bot.\nUsage: //commands', function( args ){
- var commandNames = [];
- bot.commands.map( function( cmd ){ commandNames.push( cmd.name ); } );
- commandNames.sort();
- bot.send( commandNames.join( ', ' ) );
- }),
- 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 ){
- if( args.length >= 2 ){
- var person = args.shift();
- if( person[ 0 ] === '@' ){
- bot.send( 'Don\'t double-ping, it\'s annoying' );
- person = person.split('');
- person.shift();
- person = person.join('');
- }
- else {
- var commandName = args.shift(),
- command = bot.getCommand( commandName );
- if( command.isCommand ){
- command.execute( args );
- bot.modifyMsg( 'last', function( msg ){
- return '@' + person + ' ' + msg;
- });
- } else
- bot.send( 'command ' + commandName + 'does not appear to exist. Type //commands to get a full list of commands' );
- }
- }
- }),
- 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 ){
- if( args.length > 0 ){
- var recall = bot.recalls[ args.join('') ];
- if( recall ){
- bot.send( recall );
- } else
- bot.send( 'There is no such key, sorry :/')
- } else {
- bot.getCommand( 'help' ).execute( [ 'recall' ] )
- }
- }),
- new bot.Command( 'info', 'Provides info on the bot or on a certain command.\nUsage: //info [<cmd>]', function( args ){
- if( args.length === 0 ){
- 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' )
- } else {
- var command = bot.getCommand( args[ 0 ] );
- if( command.isCommand )
- bot.send( command.getInfo() )
- else
- bot.send( 'command ' + args[ 0 ] + ' does not seem to exist. Type //commands for a full list of commands' );
- }
- }),
- new bot.Command( 'why', 'Answers to questions. 100% Accurate.\nUsage: //why <question>', function( args ){
- if( args.length > 0 )
- bot.send( bot.whyAnswers[ ( bot.whyAnswers.length * Math.random() ) |0 ] );
- else
- bot.getCommand( 'help' ).execute( [ 'why' ] );
- }),
- new bot.Command( 'are', 'Answers shortly to specific questions.\nUsage: //are <question>', function( args ){
- if( args.length > 0 )
- bot.send( bot.shortAnswers[ ( bot.shortAnswers.length * Math.random() ) |0 ] );
- else
- bot.getCommand( 'help' ).execute( [ 'are' ] );
- }),
- new bot.Command( 'is', 'Answers shortly to specific questions.\nUsage: //is <question>', function( args ){
- if( args.length > 0 )
- bot.send( bot.shortAnswers[ ( bot.shortAnswers.length * Math.random() ) |0 ] );
- else
- bot.getCommand( 'help' ).execute( [ 'is' ] );
- }),
- new bot.Command( 'what', 'Answers to questions starting with "what".\nUsage: //what <question>', function( args ){
- if( args.length > 0 )
- bot.send( bot.whatAnswers[ ( bot.whatAnswers.length * Math.random() ) |0 ] );
- else
- bot.getCommand( 'help' ).execute( [ 'what' ] );
- })
- ];
- bot.modifyMsg = function( index, fn ){
- if( index === 'last' )
- index = bot.messages.length - 1;
- bot.messages[ index ] = fn( bot.messages[ index ] );
- }
- bot.getCommand = function( name ){
- var command = 'not found';
- bot.commands.map( function( cmd ){
- if( cmd.name === name )
- command = cmd;
- });
- return command;
- }
- bot.init();
Advertisement
Add Comment
Please, Sign In to add comment