Last active
December 15, 2015 11:19
-
-
Save passcod/5252227 to your computer and use it in GitHub Desktop.
Delegated commands for jsBot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Requires npm packages: | |
| // request and underscore | |
| var _ = require('underscore'); | |
| var request = require('request'); | |
| module.exports = function Module(bot) { | |
| var delegates = {}; | |
| var say = function(client, to, message) { | |
| bot.emit('command_say', client, bot.details.nick, to, message.split(' ')); | |
| }; | |
| var delegate = function(client, from, to, args) { | |
| var text; | |
| var addDelegate = function(url, name) { | |
| if (!name) { | |
| // | |
| } | |
| if (_.values(delegates).indexOf(url) === -1) { | |
| delegates[name] = url; | |
| return 'Added.' | |
| } else { | |
| return 'Already registered.'; | |
| } | |
| }; | |
| var removeDelegate = function(un) { | |
| var i = _.values(delegates).indexOf(un); | |
| if (i === -1) { | |
| if (_.has(delegates, un)) { | |
| delegates = _.omit(delegates, un); | |
| } else { | |
| return 'Not found'; | |
| } | |
| } else { | |
| delegates = _.reject(delegates, function(k,v) {return v === un;}); | |
| } | |
| return 'Deleted'; | |
| }; | |
| if (args.length === 0) { | |
| text = bot.help('delegate', '<list|add|remove> URL[:PORT] [NAME]'); | |
| return say(client, to, text); | |
| } | |
| if (args[0] === 'list') { | |
| text = Object.keys(delegates).map(function(d) { | |
| return d + ': ' + delegates[d]; | |
| }).join('\n'); | |
| return say(client, to, text); | |
| } | |
| if (args.length > 1) { | |
| if (args[0] === 'add') { | |
| text = addDelegate(args[1], args[2]); | |
| } else if (args[0] === 'remove') { | |
| text = removeDelegate(args[1]); | |
| } else { | |
| text = 'Err, what?'; | |
| } | |
| } else { | |
| text = 'O.o'; | |
| } | |
| say(client, to, text); | |
| }; | |
| var delegatedCommand = function(client, from, to, args) { | |
| if (args.length === 0) { | |
| text = bot.help('d', 'NAME COMMAND...'); | |
| return say(client, to, text); | |
| } | |
| var cmd = args[0]; | |
| var args = _.rest(args); | |
| var url = delegates[cmd]; | |
| if (typeof url !== 'undefined') { | |
| request({ | |
| url: url, | |
| method: 'POST', | |
| body: args.join(' ') | |
| }, function(err, res, body) { | |
| if (err) { | |
| body = err; | |
| } | |
| say(client, to, cmd + '=> ' + body); | |
| }); | |
| } | |
| }; | |
| this.load = function() { | |
| bot.registerCommand('delegate', delegate, 'admin'); | |
| bot.registerCommand('d', delegatedCommand); | |
| console.info('module delegate loaded'); | |
| }; | |
| this.unload = function() { | |
| bot.unregisterCommand('delegate', delegate); | |
| bot.unregisterCommand('d', delegatedCommand); | |
| console.info('module delegate unloaded'); | |
| }; | |
| this.load(); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assuming this module structure, then the butts module can be refactored to this: