LoginSignup
2
2

More than 5 years have passed since last update.

Hubot | 自作Script | chusen.coffee - 抽選する

Posted at

Hubot | 自作Script | chusen.coffee - 抽選する

概要

chusen.coffee - 抽選する

用途

  • いくつかの候補の中から何か1つに決めたいがランダムでよいとき

例えば、

* レビュアーをランダムに決めたり
* 掃除当番をランダムに決めたり
* 買ってくるおやつをランダムに決めたり

仕様

filename

chusen.coffee

抽選、ですね。

respond or hear

respond

起動メッセージ

  • 正規表現
/chusen+ (.*)+/i
  • 具体例
    • chusen tanaka suzuki sato # => tanaka/suzuki/sato の中から誰か一人を抽選で選ぶ
    • chusen きのこの山 たけのこの里 # => きのこの山とたけのこの里のどちらかを抽選で選ぶ

ソースコード

# Description
#   Choose User
#
# Dependencies:
#   None
#
# Configuration:
#   None
#
# Commands:
#   hubot chusen <name1> <name2> <name3> ... - Output chosen name at random.
#
# Author:
#   tbpgr
module.exports = (robot) ->

  robot.respond /chusen+ (.*)+/i, (msg) ->
    if msg.match.length == 0
      msg.send 'chusen に続けて抽選対象をスペース区切りで列挙してください'
      return null

    names = msg.match[1].split(' ')
    random_result = msg.random(names)
    msg.send "抽選結果!#{random_result}"

試行結果

$ ./bin/hubot

Hubot> hubot chusen Matz Guido Larry
Hubot> 抽選結果!Guido
Hubot> hubot chusen Matz Guido Larry
Hubot> 抽選結果!Matz
Hubot> hubot chusen Matz Guido Larry
Hubot> 抽選結果!Matz
Hubot> hubot chusen Matz Guido Larry
Hubot> 抽選結果!Guido
Hubot> hubot chusen Matz Guido Larry
Hubot> 抽選結果!Larry

Hubot> hubot chusen きのこの山 たけのこの里
Hubot> 抽選結果!たけのこの里
Hubot> hubot chusen きのこの山 たけのこの里
Hubot> 抽選結果!たけのこの里
Hubot> hubot chusen きのこの山 たけのこの里
Hubot> 抽選結果!たけのこの里
Hubot> hubot chusen きのこの山 たけのこの里
Hubot> 抽選結果!きのこの山

補足

今のところ、 npm で公開したりする予定はないので適当にGitHubに突っ込んでおきます。
https://github.com/tbpgr/hubot_scripts

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2