From 367f445ee9ff0f32eb1ba05e396d5cf696e9cc14 Mon Sep 17 00:00:00 2001 From: diamondburned Date: Fri, 24 Jan 2020 08:08:12 -0800 Subject: [PATCH] Fixed bugs in the bot package Bugs include: Events not being called, inconsistent subcommand casing --- bot/ctx_call.go | 10 ++++------ bot/ctx_test.go | 28 +++++++++++++++++++++++----- bot/subcommand.go | 8 ++++++-- bot/subcommand_test.go | 4 ++-- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/bot/ctx_call.go b/bot/ctx_call.go index a945ee8..639fa29 100644 --- a/bot/ctx_call.go +++ b/bot/ctx_call.go @@ -14,7 +14,7 @@ func (ctx *Context) filterEventType(evT reflect.Type) []*CommandContext { var middles []*CommandContext var found bool - for _, cmd := range ctx.Commands { + for _, cmd := range ctx.Events { // Inherit parent's flags cmd.Flag |= ctx.Flag @@ -37,7 +37,7 @@ func (ctx *Context) filterEventType(evT reflect.Type) []*CommandContext { // Reset found status found = false - for _, cmd := range sub.Commands { + for _, cmd := range sub.Events { // Inherit parent's flags cmd.Flag |= sub.Flag @@ -113,7 +113,6 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent) error { // trim the prefix before splitting, this way multi-words prefices work content := mc.Content[len(ctx.Prefix):] - content = strings.TrimSpace(content) if content == "" { return nil // just the prefix only @@ -146,7 +145,6 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent) error { // Can't find command, look for subcommands of len(args) has a 2nd // entry. if cmd == nil && len(args) > 1 { - SubcommandLoop: for _, s := range ctx.subcommands { if s.Command != args[0] { continue @@ -160,8 +158,6 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent) error { // OR the flags c.Flag |= s.Flag - - break SubcommandLoop } } @@ -173,6 +169,8 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent) error { ctx: s.Commands, } } + + break } } diff --git a/bot/ctx_test.go b/bot/ctx_test.go index 4f777dd..a6054bc 100644 --- a/bot/ctx_test.go +++ b/bot/ctx_test.go @@ -18,6 +18,7 @@ type testCommands struct { Ctx *Context Return chan interface{} Counter uint64 + Typed bool } func (t *testCommands) MーBumpCounter(interface{}) error { @@ -25,7 +26,7 @@ func (t *testCommands) MーBumpCounter(interface{}) error { return nil } -func (t *testCommands) GetCounter(*gateway.MessageCreateEvent) error { +func (t *testCommands) GetCounter(_ *gateway.MessageCreateEvent) error { t.Return <- strconv.FormatUint(t.Counter, 10) return nil } @@ -48,6 +49,11 @@ func (t *testCommands) Noop(_ *gateway.MessageCreateEvent) error { return nil } +func (t *testCommands) OnTyping(_ *gateway.TypingStartEvent) error { + t.Typed = true + return nil +} + type CustomParseable struct { args []string } @@ -131,14 +137,26 @@ func TestContext(t *testing.T) { } t.Run("middleware", func(t *testing.T) { - ctx.Prefix = "pls do" + ctx.Prefix = "pls do " // This should trigger the middleware first. - if err := testReturn("1", "pls do getcounter"); err != nil { + if err := testReturn("1", "pls do getCounter"); err != nil { t.Fatal("Unexpected error:", err) } }) + t.Run("typing event", func(t *testing.T) { + typing := &gateway.TypingStartEvent{} + + if err := ctx.callCmd(typing); err != nil { + t.Fatal("Failed to call with TypingStart:", err) + } + + if !given.Typed { + t.Fatal("Typed bool is false") + } + }) + t.Run("call command", func(t *testing.T) { // Set a custom prefix ctx.Prefix = "~" @@ -169,7 +187,7 @@ func TestContext(t *testing.T) { t.Run("call command without args", func(t *testing.T) { ctx.Prefix = "" - if err := testMessage("noargs"); err.Error() != "passed" { + if err := testMessage("noArgs"); err.Error() != "passed" { t.Fatal("unexpected error:", err) } }) @@ -196,7 +214,7 @@ func TestContext(t *testing.T) { t.Fatal("Failed to register subcommand:", err) } - if err := testMessage("run testcommands noop"); err != nil { + if err := testMessage("run testCommands noop"); err != nil { t.Fatal("unexpected error:", err) } }) diff --git a/bot/subcommand.go b/bot/subcommand.go index 4235d2b..09ac54d 100644 --- a/bot/subcommand.go +++ b/bot/subcommand.go @@ -126,7 +126,7 @@ func (sub *Subcommand) NeedsName() { flag, name := ParseFlag(sub.StructName) if !flag.Is(Raw) { - name = strings.ToLower(name) + name = lowerFirstLetter(name) } sub.Command = name @@ -308,7 +308,7 @@ func (sub *Subcommand) parseCommands() error { // Check if Raw is enabled for command: if !flag.Is(Raw) { - command.Command = strings.ToLower(string(name[0])) + name[1:] + command.Command = lowerFirstLetter(name) } // Middlewares shouldn't even have arguments. @@ -365,3 +365,7 @@ func (sub *Subcommand) parseCommands() error { return nil } + +func lowerFirstLetter(name string) string { + return strings.ToLower(string(name[0])) + name[1:] +} diff --git a/bot/subcommand_test.go b/bot/subcommand_test.go index b723f91..56e0ff7 100644 --- a/bot/subcommand_test.go +++ b/bot/subcommand_test.go @@ -56,7 +56,7 @@ func TestSubcommand(t *testing.T) { t.Fatal("custom has nil manualParse") } - case "noargs": + case "noArgs": foundNoArgs = true if len(this.Arguments) != 0 { t.Fatal("expected 0 arguments, got non-zero") @@ -65,7 +65,7 @@ func TestSubcommand(t *testing.T) { t.Fatal("unexpected parseType") } - case "noop", "getcounter": + case "noop", "getCounter": // Found, but whatever default: