Bot: Separated events from commands

This commit is contained in:
diamondburned (Forefront) 2020-01-23 22:27:21 -08:00
parent 95f06cca45
commit 3baf89671b
1 changed files with 10 additions and 13 deletions

View File

@ -35,6 +35,7 @@ type Subcommand struct {
// All registered command contexts:
Commands []*CommandContext
Events []*CommandContext
// Middleware command contexts:
mwMethods []*CommandContext
@ -216,7 +217,6 @@ func (sub *Subcommand) fillStruct(ctx *Context) error {
func (sub *Subcommand) parseCommands() error {
var numMethods = sub.ptrValue.NumMethod()
var commands = make([]*CommandContext, 0, numMethods)
for i := 0; i < numMethods; i++ {
method := sub.ptrValue.Method(i)
@ -268,9 +268,16 @@ func (sub *Subcommand) parseCommands() error {
command.Command = strings.ToLower(name)
}
// Middlewares shouldn't even have arguments.
if flag.Is(Middleware) {
sub.mwMethods = append(sub.mwMethods, &command)
continue
}
// TODO: allow more flexibility
if command.event != typeMessageCreate {
goto Done
sub.Events = append(sub.Events, &command)
continue
}
// If the method only takes an event:
@ -279,11 +286,6 @@ func (sub *Subcommand) parseCommands() error {
goto Done
}
// Middlewares shouldn't even have arguments.
if flag.Is(Middleware) {
goto Done
}
// If the second argument implements ParseContent()
if t := methodT.In(1); t.Implements(typeIManP) {
mt, _ := t.MethodByName("ParseContent")
@ -315,13 +317,8 @@ func (sub *Subcommand) parseCommands() error {
Done:
// Append
if flag.Is(Middleware) {
sub.mwMethods = append(sub.mwMethods, &command)
} else {
commands = append(commands, &command)
}
sub.Commands = append(sub.Commands, &command)
}
sub.Commands = commands
return nil
}