allow implicit single die in roll command #1

Merged
reese merged 1 commit from reese/nekobot:main into main 2022-09-12 01:44:36 +00:00

23
main.go
View file

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