1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-27 17:23:00 +00:00

Bot: Fixed CustomParser arguments still having subcommand

This commit is contained in:
diamondburned 2020-11-14 21:05:31 -08:00
parent 4992f4ab20
commit 0932e2903b
2 changed files with 16 additions and 37 deletions

View file

@ -70,10 +70,9 @@ func (r ArgumentParts) Usage() string {
}
// CustomParser has a CustomParse method, which would be passed in the full
// message content with the prefix and command trimmed. This is used
// for commands that require more advanced parsing than the default parser.
//
// Keep in mind that this does not trim arguments before it.
// message content with the prefix, command, subcommand and space trimmed. This
// is used for commands that require more advanced parsing than the default
// parser.
type CustomParser interface {
CustomParse(arguments string) error
}
@ -100,9 +99,10 @@ type Argument struct {
pointer bool
// if nil, then manual
fn argumentValueFn
manual *reflect.Method
custom *reflect.Method
fn argumentValueFn
manual func(ManualParser, []string) error
custom func(CustomParser, string) error
}
func (a *Argument) Type() reflect.Type {
@ -132,9 +132,6 @@ func newArgument(t reflect.Type, variadic bool) (*Argument, error) {
// This shouldn't be variadic.
if !variadic && typeI.Implements(typeICusP) {
mt, _ := typeI.MethodByName("CustomParse")
// TODO: maybe ish?
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
@ -143,14 +140,12 @@ func newArgument(t reflect.Type, variadic bool) (*Argument, error) {
String: fromUsager(t),
rtype: t,
pointer: ptr,
custom: &mt,
custom: CustomParser.CustomParse,
}, nil
}
// This shouldn't be variadic either.
if !variadic && typeI.Implements(typeIManP) {
mt, _ := typeI.MethodByName("ParseContent")
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
@ -159,7 +154,7 @@ func newArgument(t reflect.Type, variadic bool) (*Argument, error) {
String: fromUsager(t),
rtype: t,
pointer: ptr,
manual: &mt,
manual: ManualParser.ParseContent,
}, nil
}

View file

@ -253,7 +253,7 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent, value refl
// If the argument wants all arguments:
case last.manual != nil:
// Call the manual parse method:
_, err = callWith(last.manual.Func, v, reflect.ValueOf(arguments))
err = last.manual(v.Interface().(ManualParser), arguments)
// If the argument wants all arguments in string:
case last.custom != nil:
@ -261,32 +261,16 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent, value refl
// have erroneous hanging quotes.
parseErr = 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
// We can't rely on the plumbing behavior.
if sub.plumbed != nil {
seekTo = sub.Command
}
content = strings.TrimSpace(content)
content = strings.TrimPrefix(content, cmd.Command)
// Seek to the string.
var i = strings.Index(content, seekTo)
// Edge case if the subcommand is the same as the command.
if cmd.Command == sub.Command {
// Seek again past the command.
i = strings.Index(content[i+len(seekTo):], seekTo)
}
if i > -1 {
// Seek past the substring.
i += len(seekTo)
content = strings.TrimSpace(content[i:])
if !sub.IsPlumbed() {
content = strings.TrimSpace(content)
content = strings.TrimPrefix(content, sub.Command)
}
// Call the method with the raw unparsed command:
_, err = callWith(last.custom.Func, v, reflect.ValueOf(content))
err = last.custom(v.Interface().(CustomParser), content)
}
// Check the returned error: