1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-15 19:33:17 +00:00
arikawa/bot/subcommand.go

491 lines
11 KiB
Go
Raw Normal View History

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"
"github.com/diamondburned/arikawa/discord"
2020-01-19 06:06:00 +00:00
"github.com/diamondburned/arikawa/gateway"
"github.com/pkg/errors"
)
var (
typeMessageCreate = reflect.TypeOf((*gateway.MessageCreateEvent)(nil))
2020-01-26 09:06:54 +00:00
typeString = reflect.TypeOf("")
typeEmbed = reflect.TypeOf((*discord.Embed)(nil))
typeSend = reflect.TypeOf((*api.SendMessageData)(nil))
typeSubcmd = reflect.TypeOf((*Subcommand)(nil))
2020-01-19 06:06:00 +00:00
typeIError = reflect.TypeOf((*error)(nil)).Elem()
typeIManP = reflect.TypeOf((*ManualParser)(nil)).Elem()
typeICusP = reflect.TypeOf((*CustomParser)(nil)).Elem()
typeIParser = reflect.TypeOf((*Parser)(nil)).Elem()
2020-04-09 04:25:50 +00:00
typeIUsager = reflect.TypeOf((*Usager)(nil)).Elem()
typeSetupFn = func() reflect.Type {
method, _ := reflect.TypeOf((*CanSetup)(nil)).
Elem().
MethodByName("Setup")
return method.Type
}()
2020-01-19 06:06:00 +00:00
)
2020-04-10 06:10:21 +00:00
// HelpUnderline formats command arguments with an underline, similar to
// manpages.
var HelpUnderline = true
func underline(word string) string {
if HelpUnderline {
return "__" + word + "__"
}
return word
}
// Subcommand is any form of command, which could be a top-level command or a
// subcommand.
//
// Allowed method signatures
//
// These are the acceptable function signatures that would be parsed as commands
// or events. A return type <T> implies that return value will be ignored.
//
// func(*gateway.MessageCreateEvent, ...) (string, error)
// func(*gateway.MessageCreateEvent, ...) (*discord.Embed, error)
// func(*gateway.MessageCreateEvent, ...) (*api.SendMessageData, error)
// func(*gateway.MessageCreateEvent, ...) (T, error)
// func(*gateway.MessageCreateEvent, ...) error
// func(*gateway.MessageCreateEvent, ...)
// func(<AnyEvent>) (T, error)
// func(<AnyEvent>) error
// func(<AnyEvent>)
//
2020-01-19 06:06:00 +00:00
type Subcommand struct {
Description string
// Raw struct name, including the flag (only filled for actual subcommands,
// will be empty for Context):
StructName string
// Parsed command name:
Command string
2020-01-29 00:01:39 +00:00
// struct flags
Flag NameFlag
// SanitizeMessage is executed on the message content if the method returns
// a string content or a SendMessageData.
SanitizeMessage func(content string) string
// QuietUnknownCommand, if true, will not make the bot reply with an unknown
// command error into the chat. If this is set in Context, it will apply to
// all other subcommands.
QuietUnknownCommand bool
2020-01-26 09:06:54 +00:00
// Commands can actually return either a string, an embed, or a
// SendMessageData, with error as the second argument.
// All registered command contexts:
2020-01-19 06:06:00 +00:00
Commands []*CommandContext
2020-01-24 06:27:21 +00:00
Events []*CommandContext
2020-01-19 06:06:00 +00:00
// Middleware command contexts:
mwMethods []*CommandContext
2020-01-19 06:06:00 +00:00
// Plumb nameflag, use Commands[0] if true.
plumb bool
2020-01-19 06:06:00 +00:00
// Directly to struct
cmdValue reflect.Value
cmdType reflect.Type
// Pointer value
ptrValue reflect.Value
ptrType reflect.Type
// command interface as reference
command interface{}
}
// CommandContext is an internal struct containing fields to make this library
// work. As such, they're all unexported. Description, however, is exported for
// editing, and may be used to generate more informative help messages.
type CommandContext struct {
Description string
Flag NameFlag
MethodName string
Command string // empty if Plumb
// Hidden is true if the method has a hidden nameflag.
Hidden bool
// Variadic is true if the function is a variadic one or if the last
// argument accepts multiple strings.
2020-05-03 22:59:10 +00:00
Variadic bool
2020-01-19 06:06:00 +00:00
value reflect.Value // Func
event reflect.Type // gateway.*Event
method reflect.Method
Arguments []Argument
2020-01-19 06:06:00 +00:00
}
// CanSetup is used for subcommands to change variables, such as Description.
// This method will be triggered when InitCommands is called, which is during
// New for Context and during RegisterSubcommand for subcommands.
type CanSetup interface {
// Setup should panic when it has an error.
Setup(*Subcommand)
2020-01-19 06:06:00 +00:00
}
func (cctx *CommandContext) Usage() []string {
if len(cctx.Arguments) == 0 {
2020-01-19 06:06:00 +00:00
return nil
}
var arguments = make([]string, len(cctx.Arguments))
for i, arg := range cctx.Arguments {
arguments[i] = arg.String
}
return arguments
2020-01-19 06:06:00 +00:00
}
// NewSubcommand is used to make a new subcommand. You usually wouldn't call
// this function, but instead use (*Context).RegisterSubcommand().
2020-01-19 06:06:00 +00:00
func NewSubcommand(cmd interface{}) (*Subcommand, error) {
var sub = Subcommand{
command: cmd,
2020-01-26 09:06:54 +00:00
SanitizeMessage: func(c string) string {
return c
},
2020-01-19 06:06:00 +00:00
}
if err := sub.reflectCommands(); err != nil {
return nil, errors.Wrap(err, "Failed to reflect commands")
}
if err := sub.parseCommands(); err != nil {
return nil, errors.Wrap(err, "Failed to parse commands")
}
return &sub, nil
}
// NeedsName sets the name for this subcommand. Like InitCommands, this
// shouldn't be called at all, rather you should use RegisterSubcommand.
func (sub *Subcommand) NeedsName() {
sub.StructName = sub.cmdType.Name()
2020-01-19 06:06:00 +00:00
flag, name := ParseFlag(sub.StructName)
2020-01-19 06:06:00 +00:00
if !flag.Is(Raw) {
name = lowerFirstLetter(name)
2020-01-19 06:06:00 +00:00
}
sub.Command = name
2020-01-19 06:06:00 +00:00
sub.Flag = flag
}
2020-04-09 06:06:46 +00:00
// FindCommand finds the command. Nil is returned if nothing is found. It's a
// better idea to not handle nil, as they would become very subtle bugs.
func (sub *Subcommand) FindCommand(methodName string) *CommandContext {
for _, c := range sub.Commands {
if c.MethodName != methodName {
continue
}
return c
}
return nil
}
// ChangeCommandInfo changes the matched methodName's Command and Description.
// Empty means unchanged. The returned bool is true when the method is found.
func (sub *Subcommand) ChangeCommandInfo(methodName, cmd, desc string) bool {
for _, c := range sub.Commands {
if c.MethodName != methodName {
continue
}
if cmd != "" {
c.Command = cmd
}
if desc != "" {
c.Description = desc
}
return true
}
return false
}
func (sub *Subcommand) Help(indent string, hideAdmin bool) string {
2020-01-24 06:46:17 +00:00
if sub.Flag.Is(AdminOnly) && hideAdmin {
return ""
}
// The header part:
var header string
2020-01-25 22:30:15 +00:00
2020-01-24 06:46:17 +00:00
if sub.Command != "" {
2020-04-10 06:10:21 +00:00
header += "**" + sub.Command + "**"
2020-01-24 06:46:17 +00:00
}
if sub.Description != "" {
if header != "" {
header += ": "
2020-01-24 06:46:17 +00:00
}
header += sub.Description
2020-01-24 06:46:17 +00:00
}
header += "\n"
// The commands part:
var commands = ""
2020-01-24 06:46:17 +00:00
2020-04-09 04:25:50 +00:00
for i, cmd := range sub.Commands {
2020-01-24 06:46:17 +00:00
if cmd.Flag.Is(AdminOnly) && hideAdmin {
continue
}
2020-04-10 06:10:21 +00:00
switch {
case sub.Command != "" && cmd.Command != "":
2020-04-09 04:25:50 +00:00
commands += indent + sub.Command + " " + cmd.Command
2020-04-10 06:10:21 +00:00
case sub.Command != "":
commands += indent + sub.Command
default:
2020-04-09 04:25:50 +00:00
commands += indent + cmd.Command
}
2020-01-24 06:46:17 +00:00
// Write the usages first.
for _, usage := range cmd.Usage() {
commands += " " + underline(usage)
}
// Is the last argument trailing? If so, append ellipsis.
if cmd.Variadic {
commands += "..."
}
// Write the description if there's any.
if cmd.Description != "" {
commands += ": " + cmd.Description
2020-01-24 06:46:17 +00:00
}
// Add a new line if this isn't the last command.
2020-04-09 04:25:50 +00:00
if i != len(sub.Commands)-1 {
commands += "\n"
}
}
if commands == "" {
return ""
2020-01-24 06:46:17 +00:00
}
return header + commands
2020-01-24 06:46:17 +00:00
}
2020-01-19 06:06:00 +00:00
func (sub *Subcommand) reflectCommands() error {
t := reflect.TypeOf(sub.command)
v := reflect.ValueOf(sub.command)
if t.Kind() != reflect.Ptr {
return errors.New("sub is not a pointer")
}
// Set the pointer fields
sub.ptrValue = v
sub.ptrType = t
ts := t.Elem()
vs := v.Elem()
if ts.Kind() != reflect.Struct {
return errors.New("sub is not pointer to struct")
}
// Set the struct fields
sub.cmdValue = vs
sub.cmdType = ts
return nil
}
// InitCommands fills a Subcommand with a context. This shouldn't be called at
// all, rather you should use the RegisterSubcommand method of a Context.
func (sub *Subcommand) InitCommands(ctx *Context) error {
// Start filling up a *Context field
if err := sub.fillStruct(ctx); err != nil {
return err
}
// See if struct implements CanSetup:
if v, ok := sub.command.(CanSetup); ok {
v.Setup(sub)
}
2020-01-29 00:01:39 +00:00
// Finalize the subcommand:
for _, cmd := range sub.Commands {
// Inherit parent's flags
cmd.Flag |= sub.Flag
}
return nil
}
func (sub *Subcommand) fillStruct(ctx *Context) error {
2020-01-19 06:06:00 +00:00
for i := 0; i < sub.cmdValue.NumField(); i++ {
field := sub.cmdValue.Field(i)
if !field.CanSet() || !field.CanInterface() {
continue
}
if _, ok := field.Interface().(*Context); !ok {
continue
}
field.Set(reflect.ValueOf(ctx))
return nil
}
return errors.New("No fields with *bot.Context found")
2020-01-19 06:06:00 +00:00
}
func (sub *Subcommand) parseCommands() error {
var numMethods = sub.ptrValue.NumMethod()
for i := 0; i < numMethods; i++ {
method := sub.ptrValue.Method(i)
if !method.CanInterface() {
continue
}
methodT := method.Type()
numArgs := methodT.NumIn()
if numArgs == 0 {
// Doesn't meet the requirement for an event, continue.
continue
}
if methodT == typeSetupFn {
// Method is a setup method, continue.
continue
}
// Check number of returns:
2020-01-26 09:06:54 +00:00
numOut := methodT.NumOut()
// Returns can either be:
// Nothing - func()
// An error - func() error
// An error and something else - func() (T, error)
if numOut > 2 {
2020-01-19 06:06:00 +00:00
continue
}
// Check the last return's type if the method returns anything.
if numOut > 0 {
if i := methodT.Out(numOut - 1); i == nil || !i.Implements(typeIError) {
// Invalid, skip.
continue
}
2020-01-19 06:06:00 +00:00
}
var command = CommandContext{
2020-05-03 22:59:10 +00:00
method: sub.ptrType.Method(i),
value: method,
event: methodT.In(0), // parse event
Variadic: methodT.IsVariadic(),
2020-01-19 06:06:00 +00:00
}
// Parse the method name
flag, name := ParseFlag(command.method.Name)
// Set the method name, command, and flag:
command.MethodName = name
command.Command = name
command.Flag = flag
// Check if Raw is enabled for command:
2020-01-19 06:06:00 +00:00
if !flag.Is(Raw) {
command.Command = lowerFirstLetter(name)
2020-01-19 06:06:00 +00:00
}
2020-01-24 06:27:21 +00:00
// Middlewares shouldn't even have arguments.
if flag.Is(Middleware) {
sub.mwMethods = append(sub.mwMethods, &command)
continue
}
2020-01-19 06:06:00 +00:00
// TODO: allow more flexibility
2020-01-25 22:30:15 +00:00
if command.event != typeMessageCreate || flag.Is(Hidden) {
2020-01-24 06:27:21 +00:00
sub.Events = append(sub.Events, &command)
continue
2020-01-19 06:06:00 +00:00
}
2020-01-26 09:06:54 +00:00
// See if we know the first return type, if error's return is the
// second:
if numOut > 1 {
switch t := methodT.Out(0); t {
case typeString, typeEmbed, typeSend:
// noop, passes
default:
continue
}
}
// If a plumb method has been found:
if sub.plumb {
continue
}
// If the method only takes an event:
2020-01-19 06:06:00 +00:00
if numArgs == 1 {
sub.Commands = append(sub.Commands, &command)
continue
}
command.Arguments = make([]Argument, 0, numArgs)
2020-01-19 06:06:00 +00:00
// Fill up arguments. This should work with cusP and manP
2020-01-19 06:06:00 +00:00
for i := 1; i < numArgs; i++ {
t := methodT.In(i)
a, err := newArgument(t, command.Variadic)
2020-01-19 06:06:00 +00:00
if err != nil {
return errors.Wrap(err, "Error parsing argument "+t.String())
}
command.Arguments = append(command.Arguments, *a)
// We're done if the type accepts multiple arguments.
if a.custom != nil || a.manual != nil {
command.Variadic = true // treat as variadic
break
}
2020-01-19 06:06:00 +00:00
}
// If the current event is a plumb event:
if flag.Is(Plumb) {
command.Command = "" // plumbers don't have names
sub.Commands = []*CommandContext{&command}
sub.plumb = true
continue
}
2020-01-19 06:06:00 +00:00
// Append
2020-01-24 06:27:21 +00:00
sub.Commands = append(sub.Commands, &command)
2020-01-19 06:06:00 +00:00
}
return nil
}
func lowerFirstLetter(name string) string {
return strings.ToLower(string(name[0])) + name[1:]
}