allow implicit single die in roll command #1

Merged
reese merged 1 commits from reese/nekobot:main into main 2022-09-12 01:44:36 +00:00
1 changed files with 17 additions and 6 deletions

23
main.go
View File

@ -248,14 +248,25 @@ func handleCatFact() {
// handleRoll rolls X Y-sided dice lmao
func handleRoll(XdY string) {
x, y, found := strings.Cut(XdY, "d")
if !found {
chat("invalid format, here's an example: !neko roll 1d20")
helpMsg := "invalid format, here are some examples:\n!neko roll d20\n!neko roll 2d6"
parts := strings.Split(XdY, "d")
if len(parts) < 2 {
chat(helpMsg)
return
}
var x string
if len(parts[0]) == 0 {
x = "1"
} else {
x = parts[0]
}
y := parts[1]
diceToRoll, err := strconv.Atoi(x)
if err != nil {
chat("invalid format, here's an example: !neko roll 1d20")
chat(helpMsg)
return
}
if diceToRoll == 0 {
@ -268,11 +279,11 @@ func handleRoll(XdY string) {
}
diceSides, err := strconv.Atoi(y)
if err != nil {
chat("invalid format, here's an example: !neko roll 1d20")
chat(helpMsg)
return
}
if diceSides <= 0 {
chat("the number of sides the dice has should be positive :3")
chat("the number of sides should be positive :3")
return
}
var response string