Fixed bugs in the bot package

Bugs include: Events not being called, inconsistent subcommand casing
This commit is contained in:
diamondburned 2020-01-24 08:08:12 -08:00
parent a5d817b28d
commit 367f445ee9
4 changed files with 35 additions and 15 deletions

View File

@ -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
}
}

View File

@ -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)
}
})

View File

@ -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:]
}

View File

@ -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: