- unless(m.params && m.params =~ /^[0-9]*d[0-9]+([+-]([0-9]+|[0-9]*d[0-9])+)*$/)
- m.nickreply "incorrect usage: " + help(m.plugin)
+ # If either not given parameters or given incorrect parameters, return with
+ # the help message
+ unless m.params && m.params =~ /^\d*d\d+(\s*[+-]\s*(\d+|\d*d\d)+)*$/
+ m.reply "incorrect usage: " + help(m.plugin)
+ return
+ end
+
+ # Extract the actual dice request from the message parameters, splitting it
+ # into dice and modifiers
+ a = m.params.gsub(/\s+/,'').scan(/^\d*d\d+|[+-]\d*d\d+|[+-]\d+/)
+ # check nr of total dices and sides per dice
+ nr = 0
+ a.each { |dice|
+ dc, ds = dice.split(/d/)
+ # check sides
+ if ds.to_i > @bot.config['dice.max_sides']
+ m.reply "sorry, don't have any dices with more than %u sides" % @bot.config['dice.max_sides'], :nick => true
+ return
+ end
+ # We use .max with 1 so that specs such as d6 count as 1 and not as 0
+ nr += [dc.to_i, 1].max
+ }
+ if nr > @bot.config['dice.max_dices']
+ m.reply "can't handle more than %u dices" % @bot.config['dice.max_dices'], :nick => true