So @passcod added the ability JavaScript variables to be persistent across resets by storing them in the DB and loading them again.
We could go a bit further than just the attacks
and attack_results
arrays.
Consider LOLcryption as a JavaScript library that extends xbot.
It could be possible to store the LOLcryption.js file in the database as a dynamically added JS libary.
This would allow installing a JS module to be a runtime command, as well as other actions such as update, remove, about, etc. !jslib install https://raw.github.com/gist/4004407/LOLcryption.js
Perhaps prior to installing, a test could be done where it loads the file into the JS context, and allows you to use that library only for that !js message. !js var LOLcryption = require("https://raw.github.com/gist/4004407/LOLcryption.js"); LOLcryption.encrypt("Hello");
Perhaps it could be temporary loading of libraries (from url) using JavaScript var lib = require('http://example.com/script.js')
and persistent installation of a library using:
!jslib install http://example.com/script.js
or
!jslib install http://example.com/script.js as Script
!jslib list
Script (http://example.com/script.js), ...
!jslib about Script
!jslib update Script
!jslib remove Script
It would be cool to register a !command to a JavaScript function that handles that command.
Currently the attack
thing has to be done using !js attack()
or !js attack('master5o1')
. If in JavaScript I could register the command !attack
to the handler attack
, then perhaps it would be done using !attack
instead.
!js bot.register_command('attack', function(argument_string){ return attack(argument_string); });
!js bot.deregister_command('attack');
!js bot.list_commands();
It would be up to that initial command handler to appropriately transform the argument_string into the correct parameters for the attack()
function:
!js bot.register_command('attack', function(argument_string){ var defender = argument_string.split(' ')[0]; return attack(defender); });
Similar to command registration. Let's say that the bot's nick is highlighted in the conversation. That's an event. Now let's say that there is a JavaScript function that acts as a listener to that event.
!js bot.add_listener('nick_highlight', 'name', function(msg) { return "I see that %s said my name.".replace("%s", msg.author); });
!js bot.remove_listener('nick_hightlight', 'name');
<master5o1> Haha xbot is silly.
<xbot> I see that master5o1 said my name.
So the above raises a concern. Currently the only way of outputting to the channel is through the return value of the command. This means the current way of printing from a function would be returning the string you want to print to the bot as the JS output.
!js "Hello";
Hello
!js var hi = "Hello"; hi;
Hello
!js var hi = "Hello"; "Hi";
Hi
!js (function(s){ return s.substring(1); })("Hello")
ello
This could change by adding bot.reply("")
. This could be as simple as pushing replies to an array and then after the JS is done it outputs the replies (either as each line, or running along the same line).