diff --git a/commands.go b/commands.go index a86d1e1..76f4094 100644 --- a/commands.go +++ b/commands.go @@ -4,7 +4,9 @@ import ( "fmt" "log" "math" + "math/rand" "regexp" + "strconv" "strings" "sync" "time" @@ -523,3 +525,35 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) { s.SendUnban(parts[1]) } } + +// !roll sides [count] - roll dice +func (b *bot) roll(m dggchat.Message, s *dggchat.Session) { + if !strings.HasPrefix(m.Message, "!roll") { + return + } + + parts := strings.Split(m.Message, " ") + if len(parts) < 2 { + return + } + + sides, _ := strconv.ParseUint(parts[1], 10, 64) + if sides == 0 { + return + } + + count := uint64(1) + if len(parts) > 2 { + c, _ := strconv.ParseUint(parts[2], 10, 64) + if c != 0 { + count = c + } + } + + if math.MaxInt64/count <= sides { + return + } + + res := rand.Int63n(int64(sides * count)) + b.sendMessageDedupe(fmt.Sprintf("%s rolled %d", m.Sender.Nick, res), s) +} diff --git a/main.go b/main.go index c823f99..a9503d1 100644 --- a/main.go +++ b/main.go @@ -72,6 +72,7 @@ func main() { b.dropAT, b.ban, b.sudoku, + b.roll, ) dgg.AddMessageHandler(b.onMessage) dgg.AddErrorHandler(b.onError)