1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-09 13:37:02 +00:00

Bot: Separated events from commands

This commit is contained in:
diamondburned (Forefront) 2020-01-23 22:27:21 -08:00
parent 95f06cca45
commit 3baf89671b

View file

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