Skip to content

Instantly share code, notes, and snippets.

@passcod
Last active December 15, 2015 11:19
Show Gist options
  • Select an option

  • Save passcod/5252227 to your computer and use it in GitHub Desktop.

Select an option

Save passcod/5252227 to your computer and use it in GitHub Desktop.
Delegated commands for jsBot
// 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();
};
@jason-s13r

Copy link
Copy Markdown

Assuming this module structure, then the butts module can be refactored to this:

module.exports = function Module(bot){
    var self = this;
    var probability = 0.01;

    var say = function(client, to, message) {
        bot.emit('command_say', client, bot.details.nick, to, message.split(' '));
    };

    var setProbability = function(client, from, to, args) {
        var receiver = bot.startsWith(to, '#') ? to : from;
        var text = "Random butts probability is set to " + probability.toFixed(2) + ".";
        if (args.length == 0) {
            text = bot.help('butts_prob', "<float>");
        } else if (/number/i.test(typeof parseFloat(args[0])) && ! /nan/i.test(parseFloat(args[0]).toString())) {
            probability = parseFloat(args[0]);
            text = "Random butts generation probability is set to " + probability.toFixed(2) + ".";
        }
        say(client, receiver, text);
    };

    var buttHandler = function(client, from, to, message) {
        var receiver = bot.startsWith(to, '#') ? to : from,
            text = message.split(' ').map(function(word, index, arr) {
                if (word.length == 4 && index == Math.round(arr.length * Math.random())) {
                    return 'butt';
                }
                if (word.length == 5 && word.substring(4) == 's') {
                    return 'butts';
                }
                if (word.length > 4 && index == Math.round(arr.length * Math.random())) {
                    return 'poop';
                }
                return word;
            }).join(' ');

        if (text == message) { return; }

        if (Math.random() < probability)
            say(client, receiver, text);
    };


    this.load = function(){     
        bot.registerCommand('butts_prob', setProbability);
        bot.addListener('message', buttHandler);
        console.log('module butts loaded');
    };

    this.unload = function() {
        bot.deregisterCommand('butts_prob', setProbability);
        bot.removeListener('message', buttHandler);
        console.log('module butts unloaded');
    };

    this.load();
};

@passcod

passcod commented Mar 27, 2013

Copy link
Copy Markdown
Author

yeah, I guess

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