diff --git a/bot/ctx_test.go b/bot/ctx_test.go index 2aae158..9571884 100644 --- a/bot/ctx_test.go +++ b/bot/ctx_test.go @@ -16,6 +16,25 @@ import ( "github.com/diamondburned/arikawa/v3/utils/handler" ) +type testUnexportedCtx struct { + ctx *Context +} + +func TestUnexportedCtx(t *testing.T) { + s := &state.State{ + Cabinet: store.NoopCabinet, + } + + _, err := New(s, &testUnexportedCtx{}) + if err == nil { + t.Fatal("New returned unexpected nil error") + } + + if !strings.Contains(err.Error(), "no exported field with *bot.Context found") { + t.Fatal("unexpected New error:", err) + } +} + type testc struct { Ctx *Context Return chan interface{} diff --git a/bot/subcommand.go b/bot/subcommand.go index aee97a4..cb1df6d 100644 --- a/bot/subcommand.go +++ b/bot/subcommand.go @@ -14,12 +14,13 @@ var ( typeMessageCreate = reflect.TypeOf((*gateway.MessageCreateEvent)(nil)) typeMessageUpdate = reflect.TypeOf((*gateway.MessageUpdateEvent)(nil)) - typeIError = reflect.TypeOf((*error)(nil)).Elem() - typeIManP = reflect.TypeOf((*ManualParser)(nil)).Elem() - typeICusP = reflect.TypeOf((*CustomParser)(nil)).Elem() - typeIParser = reflect.TypeOf((*Parser)(nil)).Elem() - typeIUsager = reflect.TypeOf((*Usager)(nil)).Elem() - typeSetupFn = methodType((*CanSetup)(nil), "Setup") + typeContextPtr = reflect.TypeOf((*Context)(nil)) + typeIError = reflect.TypeOf((*error)(nil)).Elem() + typeIManP = reflect.TypeOf((*ManualParser)(nil)).Elem() + typeICusP = reflect.TypeOf((*CustomParser)(nil)).Elem() + typeIParser = reflect.TypeOf((*Parser)(nil)).Elem() + typeIUsager = reflect.TypeOf((*Usager)(nil)).Elem() + typeSetupFn = methodType((*CanSetup)(nil), "Setup") ) func methodType(iface interface{}, name string) reflect.Type { @@ -351,14 +352,10 @@ func (sub *Subcommand) InitCommands(ctx *Context) error { } func (sub *Subcommand) fillStruct(ctx *Context) error { - for i := 0; i < sub.cmdValue.NumField(); i++ { + for i := 0; i < sub.cmdType.NumField(); i++ { field := sub.cmdValue.Field(i) - if !field.CanSet() || !field.CanInterface() { - continue - } - - if _, ok := field.Interface().(*Context); !ok { + if !field.CanSet() || field.Type() != typeContextPtr { continue } @@ -366,7 +363,7 @@ func (sub *Subcommand) fillStruct(ctx *Context) error { return nil } - return errors.New("no fields with *bot.Context found") + return errors.New("no exported field with *bot.Context found") } func (sub *Subcommand) parseCommands() error {