brettstimmerman / jabber-bot

Easily create simple regex powered Jabber bots.
BSD 3-Clause "New" or "Revised" License
91 stars 30 forks source link

Commands can have several parameters #3

Closed vivien closed 13 years ago

vivien commented 13 years ago

Hi Brett,

That is the new version of my pull request, to add the ability to have several parameters for commands you add to the bot.

Here's an example of command with this feature:

add_command(
  :syntax      => 'rand_in <min> <max>',
  :description => 'Produce a random number between <min> and <max>',
  :regex       => /^rand_in\s+(\d+)\s+(\d+)$/,
  :is_public   => true
) do |sender, params|
  min, max = params.map { |d| d.to_i }
  rand(max - min) + min
end

So that could be used like this: rand_in 30 50 # => 42.

I think it is useful (for a project, I use something like email recipient@somewhere.com Body of the message...), and in my opinion, adding groups to the regex makes the command more readable and easier to handle.

Thanks, Vivien.

brettstimmerman commented 13 years ago

Nice. I think my original plan was to keep message simple, and allow the implementation to parse message in some way if needed. I like how this formalizes the process and is easy to re-use.

I'll hope to have time to roll this by Monday.