Skip to content

Commit

Permalink
add roll command
Browse files Browse the repository at this point in the history
  • Loading branch information
slugalisk committed Oct 24, 2023
1 parent b538c2b commit 55b638c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"log"
"math"
"math/rand"
"regexp"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func main() {
b.dropAT,
b.ban,
b.sudoku,
b.roll,
)
dgg.AddMessageHandler(b.onMessage)
dgg.AddErrorHandler(b.onError)
Expand Down

0 comments on commit 55b638c

Please sign in to comment.