From 5cf90653bfe1302a341b209400c5442520dc7ea0 Mon Sep 17 00:00:00 2001 From: Malachy Byrne Date: Tue, 1 Apr 2025 22:36:32 +0100 Subject: [PATCH] Convert to lowercase when checking prefix Return flat modifier in dice roll Use log.Print instead of log.Fatal where makes sense --- application/bot.go | 4 ++-- application/commands.go | 6 +++--- dice/dice.go | 10 +++++----- dice/dice_test.go | 10 +++++----- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/application/bot.go b/application/bot.go index c547062..54526b8 100644 --- a/application/bot.go +++ b/application/bot.go @@ -33,7 +33,7 @@ func init() { return } messageContent := strings.Split(m.Content, " ") - if !strings.HasPrefix(messageContent[0], prefix) { + if !strings.HasPrefix(strings.ToLower(messageContent[0]), prefix) { return } messageContent[0] = messageContent[0][len(prefix):] @@ -49,7 +49,7 @@ func init() { }, }) if err != nil { - log.Fatalf("Error sending message: %v", err) + log.Printf("Error sending message: %v", err) } }) } diff --git a/application/commands.go b/application/commands.go index a6783fc..8099fdf 100644 --- a/application/commands.go +++ b/application/commands.go @@ -33,7 +33,7 @@ var ( }, }) if err != nil { - log.Fatalf("Error responding to interaction: %s\n", err) + log.Printf("Error responding to interaction: %s\n", err) } }, } @@ -67,7 +67,7 @@ func roll(opts optionMap) string { if err != nil { return "Error rolling dice: " + err.Error() } - roll, err := d.Roll() + roll, flat, err := d.Roll() if err != nil { ret := fmt.Sprintf("Error rolling dice: %s", err) fmt.Println(ret) @@ -78,7 +78,7 @@ func roll(opts optionMap) string { if err != nil { return fmt.Sprintf("Error rolling dice: %s", err) } - total := d.Add + total := flat for i, die := range roll { _, err = fmt.Fprintf(&ret, "%v", die.Result) if err != nil { diff --git a/dice/dice.go b/dice/dice.go index c6cb618..4931c75 100644 --- a/dice/dice.go +++ b/dice/dice.go @@ -22,14 +22,14 @@ type DieResult struct { Result int } -func (d DieExp) Roll() ([]DieResult, error) { +func (d DieExp) Roll() ([]DieResult, int, error) { results := make([]DieResult, 0) var err error for i := 0; i < d.Count; i++ { results, err = addDice(d, results) if err != nil { - return nil, err + return nil, 0, err } } @@ -52,7 +52,7 @@ func (d DieExp) Roll() ([]DieResult, error) { } if d.KeepLow > len(results) { - return results, errors.New("keep low higher than remaining results") + return nil, 0, errors.New("keep low higher than remaining results") } if d.KeepLow != 0 { @@ -84,7 +84,7 @@ func (d DieExp) Roll() ([]DieResult, error) { alreadyRolled := len(results) results, err = addDice(d, results) if err != nil { - return nil, err + return nil, 0, err } for _, result := range results[alreadyRolled:] { if result.Result == result.Size { @@ -93,7 +93,7 @@ func (d DieExp) Roll() ([]DieResult, error) { } } } - return results, nil + return results, d.Add, nil } func CreateFromExp(exp string) (DieExp, error) { diff --git a/dice/dice_test.go b/dice/dice_test.go index 040ccd7..f3f9545 100644 --- a/dice/dice_test.go +++ b/dice/dice_test.go @@ -78,7 +78,7 @@ func TestDieExp_roll(t *testing.T) { KeepLow: tt.fields.KeepLow, Explode: tt.fields.Explode, } - got, err := d.Roll() + got, _, err := d.Roll() total := 0 for _, v := range got { total += v.Result @@ -102,7 +102,7 @@ func TestDieExp_roll_keep_high(t *testing.T) { KeepLow: 0, Explode: false, } - got, err := d.Roll() + got, _, err := d.Roll() if err != nil { t.Errorf("roll() error = %v", err) return @@ -120,7 +120,7 @@ func TestDieExp_roll_keep_low(t *testing.T) { KeepLow: 2, Explode: false, } - got, err := d.Roll() + got, _, err := d.Roll() if err != nil { t.Errorf("roll() error = %v", err) return @@ -138,7 +138,7 @@ func TestDieExp_roll_keep_high_low(t *testing.T) { KeepLow: 2, Explode: false, } - got, err := d.Roll() + got, _, err := d.Roll() if err != nil { t.Errorf("roll() error = %v", err) return @@ -156,7 +156,7 @@ func TestDieExp_roll_keep_low_too_high(t *testing.T) { KeepLow: 5, Explode: false, } - _, err := d.Roll() + _, _, err := d.Roll() if err == nil { t.Errorf("expected roll error, got nil") }