2020-01-19 06:06:00 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2020-01-26 09:06:54 +00:00
|
|
|
"github.com/diamondburned/arikawa/api"
|
2020-01-19 06:06:00 +00:00
|
|
|
"github.com/diamondburned/arikawa/discord"
|
|
|
|
"github.com/diamondburned/arikawa/gateway"
|
2020-01-26 07:17:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-01-19 06:06:00 +00:00
|
|
|
)
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Break is a non-fatal error that could be returned from middlewares to stop
|
|
|
|
// the chain of execution.
|
|
|
|
var Break = errors.New("break middleware chain, non-fatal")
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// filterEventType filters all commands and subcommands into a 2D slice,
|
|
|
|
// structured so that a Break would only exit out the nested slice.
|
|
|
|
func (ctx *Context) filterEventType(evT reflect.Type) (callers [][]caller) {
|
2020-04-09 20:49:12 +00:00
|
|
|
// Find the main context first.
|
2020-05-10 08:45:00 +00:00
|
|
|
callers = append(callers, ctx.eventCallers(evT))
|
2020-04-09 20:49:12 +00:00
|
|
|
|
|
|
|
for _, sub := range ctx.subcommands {
|
|
|
|
// Find subcommands second.
|
2020-05-10 08:45:00 +00:00
|
|
|
callers = append(callers, sub.eventCallers(evT))
|
2020-04-09 20:49:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
return
|
2020-01-23 07:20:24 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func (ctx *Context) callCmd(ev interface{}) (bottomError error) {
|
|
|
|
evV := reflect.ValueOf(ev)
|
|
|
|
evT := evV.Type()
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
var callers [][]caller
|
2020-01-24 03:17:03 +00:00
|
|
|
|
|
|
|
// Hit the cache
|
|
|
|
t, ok := ctx.typeCache.Load(evT)
|
|
|
|
if ok {
|
2020-05-10 08:45:00 +00:00
|
|
|
callers = t.([][]caller)
|
2020-01-24 03:17:03 +00:00
|
|
|
} else {
|
|
|
|
callers = ctx.filterEventType(evT)
|
|
|
|
ctx.typeCache.Store(evT, callers)
|
|
|
|
}
|
2020-01-23 07:20:24 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
for _, subcallers := range callers {
|
|
|
|
for _, c := range subcallers {
|
|
|
|
_, err := c.call(evV)
|
|
|
|
if err != nil {
|
|
|
|
// Only count as an error if it's not Break.
|
|
|
|
if err = errNoBreak(err); err != nil {
|
|
|
|
bottomError = err
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Break the caller loop only for this subcommand.
|
|
|
|
break
|
2020-04-09 20:49:12 +00:00
|
|
|
}
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 01:52:15 +00:00
|
|
|
var msc *gateway.MessageCreateEvent
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// We call the messages later, since we want MessageCreate middlewares to
|
|
|
|
// run as well.
|
2020-05-15 01:52:15 +00:00
|
|
|
switch {
|
|
|
|
case evT == typeMessageCreate:
|
|
|
|
msc = ev.(*gateway.MessageCreateEvent)
|
|
|
|
|
|
|
|
case evT == typeMessageUpdate && ctx.EditableCommands:
|
|
|
|
up := ev.(*gateway.MessageUpdateEvent)
|
|
|
|
// Message updates could have empty contents when only their embeds are
|
|
|
|
// filled. We don't need that here.
|
|
|
|
if up.Content == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the updated message.
|
|
|
|
m, err := ctx.Store.Message(up.ChannelID, up.ID)
|
2020-05-10 08:45:00 +00:00
|
|
|
if err != nil {
|
2020-05-15 01:52:15 +00:00
|
|
|
// It's probably safe to ignore this.
|
|
|
|
return nil
|
2020-05-10 08:45:00 +00:00
|
|
|
}
|
2020-05-15 01:52:15 +00:00
|
|
|
|
|
|
|
// Treat the message update as a message create event to avoid breaking
|
|
|
|
// changes.
|
|
|
|
msc = &gateway.MessageCreateEvent{Message: *m, Member: up.Member}
|
|
|
|
|
|
|
|
// Fill up member, if available.
|
|
|
|
if m.GuildID.Valid() && up.Member == nil {
|
|
|
|
if mem, err := ctx.Store.Member(m.GuildID, m.Author.ID); err == nil {
|
|
|
|
msc.Member = mem
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the reflect value as well.
|
|
|
|
evV = reflect.ValueOf(msc)
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Unknown event, return.
|
|
|
|
return nil
|
2020-01-25 22:30:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 01:52:15 +00:00
|
|
|
// There's no need for an errNoBreak here, as the method already checked
|
|
|
|
// for that.
|
|
|
|
return ctx.callMessageCreate(msc, evV)
|
2020-01-24 03:17:03 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent, value reflect.Value) error {
|
2020-04-10 04:46:21 +00:00
|
|
|
// check if bot
|
|
|
|
if !ctx.AllowBot && mc.Author.Bot {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
// check if prefix
|
2020-04-06 20:25:42 +00:00
|
|
|
pf, ok := ctx.HasPrefix(mc)
|
|
|
|
if !ok {
|
2020-01-19 06:06:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim the prefix before splitting, this way multi-words prefices work
|
2020-04-06 20:25:42 +00:00
|
|
|
content := mc.Content[len(pf):]
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
if content == "" {
|
|
|
|
return nil // just the prefix only
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse arguments
|
2020-05-06 06:23:52 +00:00
|
|
|
parts, err := ctx.ParseArgs(content)
|
2020-01-19 06:06:00 +00:00
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.Wrap(err, "failed to parse command")
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
if len(parts) == 0 {
|
2020-01-19 06:06:00 +00:00
|
|
|
return nil // ???
|
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Find the command and subcommand.
|
|
|
|
arguments, cmd, sub, err := ctx.findCommand(parts)
|
|
|
|
if err != nil {
|
|
|
|
return errNoBreak(err)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// We don't run the subcommand's middlewares here, as the callCmd function
|
|
|
|
// already handles that.
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Run command middlewares.
|
|
|
|
if err := cmd.walkMiddlewares(value); err != nil {
|
|
|
|
return errNoBreak(err)
|
2020-01-23 07:20:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
// Start converting
|
|
|
|
var argv []reflect.Value
|
2020-05-04 01:33:24 +00:00
|
|
|
var argc int
|
|
|
|
|
|
|
|
// the last argument in the list, not used until set
|
|
|
|
var last Argument
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-01-25 22:07:49 +00:00
|
|
|
// Here's an edge case: when the handler takes no arguments, we allow that
|
|
|
|
// anyway, as they might've used the raw content.
|
2020-05-14 03:42:31 +00:00
|
|
|
if len(cmd.Arguments) == 0 {
|
2020-01-25 22:07:49 +00:00
|
|
|
goto Call
|
|
|
|
}
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
// Argument count check.
|
2020-05-04 01:33:24 +00:00
|
|
|
if argdelta := len(arguments) - len(cmd.Arguments); argdelta != 0 {
|
2020-05-03 22:59:10 +00:00
|
|
|
var err error // no err if nil
|
|
|
|
|
2020-05-04 04:22:19 +00:00
|
|
|
// If the function is variadic, then we can allow the last argument to
|
|
|
|
// be empty.
|
|
|
|
if cmd.Variadic {
|
|
|
|
argdelta++
|
|
|
|
}
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
switch {
|
|
|
|
// If there aren't enough arguments given.
|
|
|
|
case argdelta < 0:
|
|
|
|
err = ErrNotEnoughArgs
|
|
|
|
|
|
|
|
// If there are too many arguments, then check if the command supports
|
|
|
|
// variadic arguments. We already did a length check above.
|
|
|
|
case argdelta > 0 && !cmd.Variadic:
|
|
|
|
// If it's not variadic, then we can't accept it.
|
|
|
|
err = ErrTooManyArgs
|
2020-01-26 06:15:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return &ErrInvalidUsage{
|
|
|
|
Prefix: pf,
|
2020-05-04 01:33:24 +00:00
|
|
|
Args: parts,
|
|
|
|
Index: len(parts) - 1,
|
2020-05-03 22:59:10 +00:00
|
|
|
Wrap: err,
|
|
|
|
Ctx: cmd,
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// The last argument in the arguments slice.
|
|
|
|
last = cmd.Arguments[len(cmd.Arguments)-1]
|
|
|
|
|
2020-05-03 22:59:10 +00:00
|
|
|
// Allocate a new slice the length of function arguments.
|
2020-05-04 01:33:24 +00:00
|
|
|
argc = len(cmd.Arguments) - 1 // arg len without last
|
|
|
|
argv = make([]reflect.Value, 0, argc) // could be 0
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// Parse all arguments except for the last one.
|
|
|
|
for i := 0; i < argc; i++ {
|
|
|
|
v, err := cmd.Arguments[i].fn(arguments[0])
|
2020-01-19 06:06:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return &ErrInvalidUsage{
|
2020-05-03 22:59:10 +00:00
|
|
|
Prefix: pf,
|
2020-05-04 01:33:24 +00:00
|
|
|
Args: parts,
|
|
|
|
Index: len(parts) - len(arguments) + i,
|
2020-05-03 22:59:10 +00:00
|
|
|
Wrap: err,
|
|
|
|
Ctx: cmd,
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// Pop arguments.
|
|
|
|
arguments = arguments[1:]
|
|
|
|
argv = append(argv, v)
|
2020-05-03 22:59:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// Is this last argument actually a variadic slice? If yes, then it
|
|
|
|
// should still have fn normally.
|
|
|
|
if last.fn != nil {
|
|
|
|
// Allocate a new slice to append into.
|
|
|
|
vars := make([]reflect.Value, 0, len(arguments))
|
2020-05-03 22:59:10 +00:00
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
// Parse the rest with variadic arguments. Go's reflect states that
|
|
|
|
// varidic parameters will automatically be copied, which is good.
|
|
|
|
for i := 0; len(arguments) > 0; i++ {
|
|
|
|
v, err := last.fn(arguments[0])
|
2020-05-03 22:59:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return &ErrInvalidUsage{
|
|
|
|
Prefix: pf,
|
2020-05-04 01:33:24 +00:00
|
|
|
Args: parts,
|
|
|
|
Index: len(parts) - len(arguments) + i,
|
2020-05-03 22:59:10 +00:00
|
|
|
Wrap: err,
|
|
|
|
Ctx: cmd,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 01:33:24 +00:00
|
|
|
arguments = arguments[1:]
|
|
|
|
vars = append(vars, v)
|
2020-05-03 22:59:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
argv = append(argv, vars...)
|
2020-05-04 01:33:24 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Create a zero value instance of this:
|
|
|
|
v := reflect.New(last.rtype)
|
|
|
|
var err error // return error
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// If the argument wants all arguments:
|
|
|
|
case last.manual != nil:
|
|
|
|
// Call the manual parse method:
|
|
|
|
_, err = callWith(last.manual.Func, v, reflect.ValueOf(arguments))
|
|
|
|
|
|
|
|
// If the argument wants all arguments in string:
|
|
|
|
case last.custom != nil:
|
|
|
|
// Manual string seeking is a must here. This is because the string
|
|
|
|
// could contain multiple whitespaces, and the parser would not
|
|
|
|
// count them.
|
|
|
|
var seekTo = cmd.Command
|
2020-05-14 03:42:31 +00:00
|
|
|
// We can't rely on the plumbing behavior.
|
|
|
|
if sub.plumbed != nil {
|
2020-05-04 01:33:24 +00:00
|
|
|
seekTo = sub.Command
|
|
|
|
}
|
|
|
|
|
|
|
|
// Seek to the string.
|
|
|
|
if i := strings.Index(content, seekTo); i > -1 {
|
2020-05-04 05:57:44 +00:00
|
|
|
// Seek past the substring.
|
|
|
|
i += len(seekTo)
|
2020-05-04 01:33:24 +00:00
|
|
|
content = strings.TrimSpace(content[i:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the method with the raw unparsed command:
|
|
|
|
_, err = callWith(last.custom.Func, v, reflect.ValueOf(content))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the returned error:
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the argument wants a non-pointer:
|
|
|
|
if last.pointer {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the argument into argv.
|
|
|
|
argv = append(argv, v)
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Call:
|
|
|
|
// call the function and parse the error return value
|
2020-05-10 08:45:00 +00:00
|
|
|
v, err := cmd.call(value, argv...)
|
2020-01-26 09:06:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := v.(type) {
|
|
|
|
case string:
|
|
|
|
v = sub.SanitizeMessage(v)
|
|
|
|
_, err = ctx.SendMessage(mc.ChannelID, v, nil)
|
|
|
|
case *discord.Embed:
|
|
|
|
_, err = ctx.SendMessage(mc.ChannelID, "", v)
|
|
|
|
case *api.SendMessageData:
|
|
|
|
if v.Content != "" {
|
|
|
|
v.Content = sub.SanitizeMessage(v.Content)
|
|
|
|
}
|
|
|
|
_, err = ctx.SendMessageComplex(mc.ChannelID, *v)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// findCommand filters.
|
|
|
|
func (ctx *Context) findCommand(parts []string) ([]string, *MethodContext, *Subcommand, error) {
|
|
|
|
// Main command entrypoint cannot have plumb.
|
2020-05-13 04:37:06 +00:00
|
|
|
for _, c := range ctx.Commands {
|
2020-05-10 08:45:00 +00:00
|
|
|
if c.Command == parts[0] {
|
|
|
|
return parts[1:], c, ctx.Subcommand, nil
|
|
|
|
}
|
2020-05-24 10:49:23 +00:00
|
|
|
// Check for alias
|
|
|
|
for _, alias := range c.Aliases {
|
|
|
|
if alias == parts[0] {
|
|
|
|
return parts[1:], c, ctx.Subcommand, nil
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
// Can't find the command, look for subcommands if len(args) has a 2nd
|
|
|
|
// entry.
|
|
|
|
for _, s := range ctx.subcommands {
|
|
|
|
if s.Command != parts[0] {
|
|
|
|
continue
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-13 04:37:06 +00:00
|
|
|
// Only actually plumb if we actually have a plumbed handler AND
|
|
|
|
// 1. We only have one command handler OR
|
|
|
|
// 2. We only have the subcommand name but no command.
|
|
|
|
if s.plumbed != nil && (len(s.Commands) == 1 || len(parts) <= 2) {
|
2020-05-10 08:45:00 +00:00
|
|
|
return parts[1:], s.plumbed, s, nil
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
if len(parts) >= 2 {
|
2020-05-13 04:37:06 +00:00
|
|
|
for _, c := range s.Commands {
|
|
|
|
if c.Command == parts[1] {
|
2020-05-10 08:45:00 +00:00
|
|
|
return parts[2:], c, s, nil
|
|
|
|
}
|
2020-05-24 10:49:23 +00:00
|
|
|
// Check for aliases
|
|
|
|
for _, alias := range c.Aliases {
|
|
|
|
if alias == parts[1] {
|
|
|
|
return parts[2:], c, s, nil
|
|
|
|
}
|
|
|
|
}
|
2020-05-10 08:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
// If unknown command is disabled or the subcommand is hidden:
|
|
|
|
if ctx.SilentUnknown.Subcommand || s.Hidden {
|
2020-05-10 08:45:00 +00:00
|
|
|
return nil, nil, nil, Break
|
|
|
|
}
|
2020-01-23 07:20:24 +00:00
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
return nil, nil, nil, &ErrUnknownCommand{
|
|
|
|
Parts: parts,
|
|
|
|
Subcmd: s,
|
|
|
|
}
|
2020-01-23 07:20:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 03:42:31 +00:00
|
|
|
if ctx.SilentUnknown.Command {
|
2020-05-10 08:45:00 +00:00
|
|
|
return nil, nil, nil, Break
|
2020-01-23 07:20:24 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
return nil, nil, nil, &ErrUnknownCommand{
|
|
|
|
Parts: parts,
|
|
|
|
Subcmd: ctx.Subcommand,
|
2020-05-04 01:33:24 +00:00
|
|
|
}
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-10 08:45:00 +00:00
|
|
|
func errNoBreak(err error) error {
|
|
|
|
if errors.Is(err, Break) {
|
|
|
|
return nil
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
2020-05-10 08:45:00 +00:00
|
|
|
return err
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|