Sha256: f7b97df4023d05a6514ae77577aaf829202ca4fc4cb3db63142d44214b2676c9

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

require "lita"

module Lita
  module Handlers
    class Dice < Handler
      route(/^roll(\s+(?<x>\d+)?d?(?<y>\d+)?)?/i, :roll, command: true, help: {
        'roll' => 'Roll one 6-sided die.',
        'roll X' => 'Roll X 6-sided dice.',
        'roll XdY' => 'Roll X Y-sided dice.'})

      def roll(response)
        int = lambda do |n, default|
          if n == nil
            return default
          end
          n.to_i
        end

        x, y = response.matches.first
        x = int.call(x, 1)
        y = int.call(y, 6)

        if x > 20 or x < 1
          s = "#{response.user.name}: You can only roll between 1 and 20 dice."
        elsif y > 1000 or y < 2
          s = "#{response.user.name}: Dice must have between 2 and 1000 sides."
        else
          rolls = []
          (1..x).each do |n|
            rolls << (1..y).to_a.sample
          end
          s = "#{response.user.name} rolled #{rolls.join(' ')}"
          if x > 1
            total = rolls.inject(:+)
            s += " (#{total})"
          end
        end
        response.reply s
      end
    end

    Lita.register_handler(Dice)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lita-dice-0.0.1 lib/lita/handlers/dice.rb