Skip to content
This repository has been archived by the owner on Jul 17, 2020. It is now read-only.

Commit

Permalink
added /learn memory
Browse files Browse the repository at this point in the history
  • Loading branch information
Zirak committed Nov 17, 2012
1 parent 5079703 commit c254472
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 186 deletions.
214 changes: 122 additions & 92 deletions master.js
Expand Up @@ -6162,95 +6162,125 @@ bot.addCommand({
}()); }());


; ;
(function () { (function () {
var parse; "use strict";

var parse = bot.getCommand( 'parse' );
function learn ( args ) { var storage = JSON.parse( localStorage.bot_learn || '{}' );
bot.log( args, '/learn input' ); loadCommands();


var commandParts = args.parse(); function learn ( args ) {
var command = { bot.log( args, '/learn input' );
name : commandParts[ 0 ],
output : commandParts[ 1 ], var commandParts = args.parse();
input : commandParts[ 2 ] || '.*' var command = {
}; name : commandParts[ 0 ],

output : commandParts[ 1 ],
//a truthy value, unintuitively, means it isn't valid, because it returns input : commandParts[ 2 ] || '.*'
// an error message };
var errorMessage = checkCommand( command );
if ( errorMessage ) { //a truthy value, unintuitively, means it isn't valid, because it returns
return errorMessage; // an error message
} var errorMessage = checkCommand( command );
command.name = command.name.toLowerCase(); if ( errorMessage ) {
command.input = new RegExp( command.input ); return errorMessage;

}
parse = bot.getCommand( 'parse' ); command.name = command.name.toLowerCase();
if ( parse.error ) { command.input = new RegExp( command.input );
console.error( '/parse not loaded, cannot /learn' );
return 'Failed; /parse not loaded'; bot.log( command, '/learn parsed' );
}
console.log( parse ); addCustomCommand( command );

saveCommand( command );
bot.log( command, '/learn parsed' ); return 'Command ' + command.name + ' learned';

}
addCustomCommand( command );
return 'Command ' + command.name + ' learned'; function addCustomCommand ( command ) {
}; var cmd = bot.Command({

name : command.name,
function addCustomCommand ( command ) { description : 'User-taught command: ' + command.output,
bot.addCommand({
name : command.name, fun : makeCustomCommand( command ),
description : 'User-taught command: ' + command.output, permissions : {

use : 'ALL',
fun : makeCustomCommand( command ), del : 'ALL'
permissions : { }
use : 'ALL', });
del : 'ALL'
} cmd.del = (function ( old ) {
}); return function () {
} deleteCommand( command.name );
function makeCustomCommand ( command ) { old();

};
return function ( args ) { }( cmd.del ));
bot.log( args, command.name + ' input' );

bot.log( cmd, '/learn addCustomCommand' );
var cmdArgs = bot.Message( command.output, args.get() ); bot.addCommand( cmd );
return parse.exec( cmdArgs, command.input.exec(args) ); }
}; function makeCustomCommand ( command ) {
} bot.log( command, '/learn makeCustomCommand' );

return function ( args ) {
//return a truthy value (an error message) if it's invalid, falsy if it's bot.log( args, command.name + ' input' );
// valid
function checkCommand ( cmd ) { var cmdArgs = bot.Message( command.output, args.get() );
var somethingUndefined = Object.keys( cmd ).some(function ( key ) { return parse.exec( cmdArgs, command.input.exec(args) );
return !cmd[ key ]; };
}), }
error;

//return a truthy value (an error message) if it's invalid, falsy if it's
if ( somethingUndefined ) { // valid
error = 'Illegal /learn object'; function checkCommand ( cmd ) {
} var somethingUndefined = Object.keys( cmd ).some(function ( key ) {

return !cmd[ key ];
if ( !/^[\w\-]+$/.test(cmd.name) ) { }),
error = 'Invalid command name'; error;
}

if ( somethingUndefined ) {
if ( bot.commandExists(cmd.name.toLowerCase()) ) { error = 'Illegal /learn object';
error = 'Command ' + cmd.name + ' already exists'; }
}

else if ( !/^[\w\-]+$/.test(cmd.name) ) {
return error; error = 'Invalid command name';
} }



else if ( bot.commandExists(cmd.name.toLowerCase()) ) {
bot.addCommand({ error = 'Command ' + cmd.name + ' already exists';
name : 'learn', }
fun : learn,
privileges : { return error;
del : 'NONE', }
},

function loadCommands () {
description : 'Teaches the bot a command. ' + Object.keys( storage ).forEach( teach );
'`/learn cmdName cmdOutputMacro [cmdInputRegex]`'
}); function teach ( key ) {
}()); var cmd = JSON.parse( storage[key] );
cmd.input = new RegExp( cmd.input );

bot.log( cmd, '/learn loadCommands' );
addCustomCommand( cmd );
}
}
function saveCommand ( command ) {
storage[ command.name ] = JSON.stringify({
name : command.name,
input : command.input.source,
output : command.output
});
localStorage.bot_learn = JSON.stringify( storage );
}
function deleteCommand ( name ) {
delete storage[ name ];
localStorage.bot_learn = JSON.stringify( storage );
}

bot.addCommand({
name : 'learn',
fun : learn,
privileges : {
del : 'NONE'
},

description : 'Teaches the bot a command. ' +
'`/learn cmdName cmdOutputMacro [cmdInputRegex]`'
});
}());

0 comments on commit c254472

Please sign in to comment.