From c74a1ad8ea6522349b044665a0b9786e279e4724 Mon Sep 17 00:00:00 2001 From: Slugalisk Date: Mon, 20 Nov 2023 14:37:11 -0800 Subject: [PATCH] fix roll --- commands.go | 13 +++++++------ commands_test.go | 6 ++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/commands.go b/commands.go index a30e49b..3c34384 100644 --- a/commands.go +++ b/commands.go @@ -529,7 +529,7 @@ func (b *bot) ban(m dggchat.Message, s *dggchat.Session) { func computeRoll(input string) (int, error) { // Define a regular expression to extract dice rolling information - regexPattern := `^!rolls?\s+(\d+)d(\d+)\s*([+\-]\s*\d+)?(.*?)$` + regexPattern := `^!rolls?\s+(\d+)(?:d(\d+))?\s*([+\-]\s*\d+)?` regex := regexp.MustCompile(regexPattern) // Match the regular expression against the input string @@ -542,14 +542,15 @@ func computeRoll(input string) (int, error) { // Extract matched values numDice, _ := strconv.Atoi(matches[1]) numSides, _ := strconv.Atoi(matches[2]) - modifierStr := matches[3] - checkMod := modifierStr != "" - var modifier int - if checkMod { - modifier, _ = strconv.Atoi(modifierStr) + if matches[2] == "" { + numSides = numDice + numDice = 1 } + modifier, _ := strconv.Atoi(matches[3]) + checkMod := modifier != 0 + if numSides <= 0 || numDice <= 0 || modifier > math.MaxInt64 { return 0, fmt.Errorf("Sides, count or modifier too large") } diff --git a/commands_test.go b/commands_test.go index f11d18e..82fb008 100644 --- a/commands_test.go +++ b/commands_test.go @@ -23,7 +23,7 @@ func TestParseModifiers(t *testing.T) { } func TestComputeRoll(t *testing.T) { - testInputs := [11]string{ + testInputs := []string{ "!roll 2d2+100 foo biz baz", "!roll 2d2 + 100", "!roll 2d2 +100", @@ -34,7 +34,9 @@ func TestComputeRoll(t *testing.T) { "!roll 2d2- 100", "!roll 2d2- 100 foo biz baz", "!roll 23904823904823904823490d20 +1", - "!roll 2d20"} + "!roll 2d20", + "!roll 20", + "!roll 20+10"} for _, input := range testInputs { result, err := computeRoll(input)