Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands can have several parameters #3

Merged
merged 3 commits into from
May 14, 2011

Conversation

vivien
Copy link
Contributor

@vivien vivien commented May 12, 2011

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.

  • The first commit is a cleanup, to replace and/or with &&/||, because they are not synonyms. See this article for an explanation. This can avoid subtle bugs :)
  • The second commit is the new feature. The trick is done around the line 363. As we've talked about in the last pull request, the only thing to do is to add groups (expression between parenthesis) in the command regex, to allow several parameters in the callback function ("p Hello world!".match(/^p\s+(.*)$/).captures # => ["Hello world!"] shows the idea). By the way, the piece of code to scan the message is a bit cleaner.

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
Copy link
Owner

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.

brettstimmerman added a commit that referenced this pull request May 14, 2011
Allow multiple parameters for bot commands and tidy up a few things.
@brettstimmerman brettstimmerman merged commit b410886 into brettstimmerman:master May 14, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants