Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added /learn memory

  • Loading branch information...
commit c25447225fa01aaf3a6fca3a3bf949f01cab6cf6 1 parent 5079703
@Zirak authored
Showing with 246 additions and 186 deletions.
  1. +122 −92 master.js
  2. +2 −2 master.min.js
  3. +122 −92 source/plugins/learn.js
View
214 master.js
@@ -6162,95 +6162,125 @@ bot.addCommand({
}());
;
-(function () {
-var parse;
-
-function learn ( args ) {
- bot.log( args, '/learn input' );
-
- var commandParts = args.parse();
- var command = {
- name : commandParts[ 0 ],
- output : commandParts[ 1 ],
- input : commandParts[ 2 ] || '.*'
- };
-
- //a truthy value, unintuitively, means it isn't valid, because it returns
- // an error message
- var errorMessage = checkCommand( command );
- if ( errorMessage ) {
- return errorMessage;
- }
- command.name = command.name.toLowerCase();
- command.input = new RegExp( command.input );
-
- parse = bot.getCommand( 'parse' );
- if ( parse.error ) {
- console.error( '/parse not loaded, cannot /learn' );
- return 'Failed; /parse not loaded';
- }
- console.log( parse );
-
- bot.log( command, '/learn parsed' );
-
- addCustomCommand( command );
- return 'Command ' + command.name + ' learned';
-};
-
-function addCustomCommand ( command ) {
- bot.addCommand({
- name : command.name,
- description : 'User-taught command: ' + command.output,
-
- fun : makeCustomCommand( command ),
- permissions : {
- use : 'ALL',
- del : 'ALL'
- }
- });
-}
-function makeCustomCommand ( command ) {
-
- return function ( args ) {
- bot.log( args, command.name + ' input' );
-
- var cmdArgs = bot.Message( command.output, args.get() );
- return parse.exec( cmdArgs, command.input.exec(args) );
- };
-}
-
-//return a truthy value (an error message) if it's invalid, falsy if it's
-// valid
-function checkCommand ( cmd ) {
- var somethingUndefined = Object.keys( cmd ).some(function ( key ) {
- return !cmd[ key ];
- }),
- error;
-
- if ( somethingUndefined ) {
- error = 'Illegal /learn object';
- }
-
- if ( !/^[\w\-]+$/.test(cmd.name) ) {
- error = 'Invalid command name';
- }
-
- if ( bot.commandExists(cmd.name.toLowerCase()) ) {
- error = 'Command ' + cmd.name + ' already exists';
- }
-
- return error;
-}
-
-
-bot.addCommand({
- name : 'learn',
- fun : learn,
- privileges : {
- del : 'NONE',
- },
-
- description : 'Teaches the bot a command. ' +
- '`/learn cmdName cmdOutputMacro [cmdInputRegex]`'
-});
-}());
+(function () {
+"use strict";
+var parse = bot.getCommand( 'parse' );
+var storage = JSON.parse( localStorage.bot_learn || '{}' );
+loadCommands();
+
+function learn ( args ) {
+ bot.log( args, '/learn input' );
+
+ var commandParts = args.parse();
+ var command = {
+ name : commandParts[ 0 ],
+ output : commandParts[ 1 ],
+ input : commandParts[ 2 ] || '.*'
+ };
+
+ //a truthy value, unintuitively, means it isn't valid, because it returns
+ // an error message
+ var errorMessage = checkCommand( command );
+ if ( errorMessage ) {
+ return errorMessage;
+ }
+ command.name = command.name.toLowerCase();
+ command.input = new RegExp( command.input );
+
+ bot.log( command, '/learn parsed' );
+
+ addCustomCommand( command );
+ saveCommand( command );
+ return 'Command ' + command.name + ' learned';
+}
+
+function addCustomCommand ( command ) {
+ var cmd = bot.Command({
+ name : command.name,
+ description : 'User-taught command: ' + command.output,
+
+ fun : makeCustomCommand( command ),
+ permissions : {
+ use : 'ALL',
+ del : 'ALL'
+ }
+ });
+
+ cmd.del = (function ( old ) {
+ return function () {
+ deleteCommand( command.name );
+ old();
+ };
+ }( cmd.del ));
+
+ bot.log( cmd, '/learn addCustomCommand' );
+ bot.addCommand( cmd );
+}
+function makeCustomCommand ( command ) {
+ bot.log( command, '/learn makeCustomCommand' );
+ return function ( args ) {
+ bot.log( args, command.name + ' input' );
+
+ var cmdArgs = bot.Message( command.output, args.get() );
+ return parse.exec( cmdArgs, command.input.exec(args) );
+ };
+}
+
+//return a truthy value (an error message) if it's invalid, falsy if it's
+// valid
+function checkCommand ( cmd ) {
+ var somethingUndefined = Object.keys( cmd ).some(function ( key ) {
+ return !cmd[ key ];
+ }),
+ error;
+
+ if ( somethingUndefined ) {
+ error = 'Illegal /learn object';
+ }
+
+ else if ( !/^[\w\-]+$/.test(cmd.name) ) {
+ error = 'Invalid command name';
+ }
+
+ else if ( bot.commandExists(cmd.name.toLowerCase()) ) {
+ error = 'Command ' + cmd.name + ' already exists';
+ }
+
+ return error;
+}
+
+function loadCommands () {
+ Object.keys( storage ).forEach( teach );
+
+ 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]`'
+});
+}());
View
4 master.min.js
2 additions, 2 deletions not shown
View
214 source/plugins/learn.js
@@ -1,92 +1,122 @@
-(function () {
-var parse;
-
-function learn ( args ) {
- bot.log( args, '/learn input' );
-
- var commandParts = args.parse();
- var command = {
- name : commandParts[ 0 ],
- output : commandParts[ 1 ],
- input : commandParts[ 2 ] || '.*'
- };
-
- //a truthy value, unintuitively, means it isn't valid, because it returns
- // an error message
- var errorMessage = checkCommand( command );
- if ( errorMessage ) {
- return errorMessage;
- }
- command.name = command.name.toLowerCase();
- command.input = new RegExp( command.input );
-
- parse = bot.getCommand( 'parse' );
- if ( parse.error ) {
- console.error( '/parse not loaded, cannot /learn' );
- return 'Failed; /parse not loaded';
- }
- console.log( parse );
-
- bot.log( command, '/learn parsed' );
-
- addCustomCommand( command );
- return 'Command ' + command.name + ' learned';
-};
-
-function addCustomCommand ( command ) {
- bot.addCommand({
- name : command.name,
- description : 'User-taught command: ' + command.output,
-
- fun : makeCustomCommand( command ),
- permissions : {
- use : 'ALL',
- del : 'ALL'
- }
- });
-}
-function makeCustomCommand ( command ) {
-
- return function ( args ) {
- bot.log( args, command.name + ' input' );
-
- var cmdArgs = bot.Message( command.output, args.get() );
- return parse.exec( cmdArgs, command.input.exec(args) );
- };
-}
-
-//return a truthy value (an error message) if it's invalid, falsy if it's
-// valid
-function checkCommand ( cmd ) {
- var somethingUndefined = Object.keys( cmd ).some(function ( key ) {
- return !cmd[ key ];
- }),
- error;
-
- if ( somethingUndefined ) {
- error = 'Illegal /learn object';
- }
-
- if ( !/^[\w\-]+$/.test(cmd.name) ) {
- error = 'Invalid command name';
- }
-
- if ( bot.commandExists(cmd.name.toLowerCase()) ) {
- error = 'Command ' + cmd.name + ' already exists';
- }
-
- return error;
-}
-
-
-bot.addCommand({
- name : 'learn',
- fun : learn,
- privileges : {
- del : 'NONE',
- },
-
- description : 'Teaches the bot a command. ' +
- '`/learn cmdName cmdOutputMacro [cmdInputRegex]`'
-});
-}());
+(function () {
+"use strict";
+var parse = bot.getCommand( 'parse' );
+var storage = JSON.parse( localStorage.bot_learn || '{}' );
+loadCommands();
+
+function learn ( args ) {
+ bot.log( args, '/learn input' );
+
+ var commandParts = args.parse();
+ var command = {
+ name : commandParts[ 0 ],
+ output : commandParts[ 1 ],
+ input : commandParts[ 2 ] || '.*'
+ };
+
+ //a truthy value, unintuitively, means it isn't valid, because it returns
+ // an error message
+ var errorMessage = checkCommand( command );
+ if ( errorMessage ) {
+ return errorMessage;
+ }
+ command.name = command.name.toLowerCase();
+ command.input = new RegExp( command.input );
+
+ bot.log( command, '/learn parsed' );
+
+ addCustomCommand( command );
+ saveCommand( command );
+ return 'Command ' + command.name + ' learned';
+}
+
+function addCustomCommand ( command ) {
+ var cmd = bot.Command({
+ name : command.name,
+ description : 'User-taught command: ' + command.output,
+
+ fun : makeCustomCommand( command ),
+ permissions : {
+ use : 'ALL',
+ del : 'ALL'
+ }
+ });
+
+ cmd.del = (function ( old ) {
+ return function () {
+ deleteCommand( command.name );
+ old();
+ };
+ }( cmd.del ));
+
+ bot.log( cmd, '/learn addCustomCommand' );
+ bot.addCommand( cmd );
+}
+function makeCustomCommand ( command ) {
+ bot.log( command, '/learn makeCustomCommand' );
+ return function ( args ) {
+ bot.log( args, command.name + ' input' );
+
+ var cmdArgs = bot.Message( command.output, args.get() );
+ return parse.exec( cmdArgs, command.input.exec(args) );
+ };
+}
+
+//return a truthy value (an error message) if it's invalid, falsy if it's
+// valid
+function checkCommand ( cmd ) {
+ var somethingUndefined = Object.keys( cmd ).some(function ( key ) {
+ return !cmd[ key ];
+ }),
+ error;
+
+ if ( somethingUndefined ) {
+ error = 'Illegal /learn object';
+ }
+
+ else if ( !/^[\w\-]+$/.test(cmd.name) ) {
+ error = 'Invalid command name';
+ }
+
+ else if ( bot.commandExists(cmd.name.toLowerCase()) ) {
+ error = 'Command ' + cmd.name + ' already exists';
+ }
+
+ return error;
+}
+
+function loadCommands () {
+ Object.keys( storage ).forEach( teach );
+
+ 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]`'
+});
+}());
Please sign in to comment.
Something went wrong with that request. Please try again.