Skip to content
This repository has been archived by the owner on Nov 14, 2017. It is now read-only.

Command event #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Command event #12

wants to merge 1 commit into from

Conversation

jakajancar
Copy link

Added the "command" event, which works both on channels as well as privately.

This allows one to simply do:

bot.on 'command', (message, reply) ->
    reply 'You told me to "' + message + '"'

This will make the bot accept the command both privately as well as on channels, without a separate handler.

@jakajancar
Copy link
Author

I'm not sure if the constructor is the right place for this. Feel free if you think there's a better place to put it in.

@uxdiogenes
Copy link

I'm not sure command is the best name. Maybe take a cue from hubot and add robot.hear() and robot.respond()? I added on a similar API to wobot like so:

bot.onConnect(function () {
  // add some hubot-ish helpers
  bot.hear = function (regex, fn) {
    bot.onMessage(regex, fn);
    bot.onPrivateMessage(regex, fn);
  };

  if (!bot.mention_name) throw "What, no mention name?";
  var respondRegex = new RegExp('^@' + bot.mention_name + ' (.*)');

  bot.respond = function (regex, responseFn) {
    var onFn = function(channel, from, message, matches) {
      if (regex.test(message)) {
        responseFn(channel, from, message, matches[0].match(regex));
      }
    };

    bot.onMessage(respondRegex, onFn);
    bot.onPrivateMessage(respondRegex, onFn);
  };

  bot.random = function (array) {
    return array[Math.floor(Math.random() * array.length)];
  };
});

@mbielski
Copy link

@uxdiogenes That is a much better solution and is what I will be using on my installation.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants