Skip to content

Instantly share code, notes, and snippets.

@jason-s13r
Created November 7, 2012 01:16
Show Gist options
  • Save jason-s13r/4028911 to your computer and use it in GitHub Desktop.
Save jason-s13r/4028911 to your computer and use it in GitHub Desktop.
Some xbot + javascript ideas

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.

JavaScript Modules

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

!command registration for JavaScript:

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); });

Bot to emit a JavaScript event:

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.

Command or Event handlers

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).

Basically, let's add some more things to the bot's JavaScript api ;)

Thoughts on the Bot's JavaScript API:

  • Commands:
  • bot.register_command(command, command_handler)
  • bot.deregister_command(command)
  • bot.list_commands()
  • Events:
  • bot.add_listener(event, listener_name, event_listener)
  • bot.remove_listener(event, listener_name)

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment