mirror of
https://github.com/diamondburned/arikawa.git
synced 2025-05-21 23:01:07 +00:00
Bot: Fixed CustomParser arguments still having subcommand
This commit is contained in:
parent
4992f4ab20
commit
0932e2903b
|
@ -70,10 +70,9 @@ func (r ArgumentParts) Usage() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// CustomParser has a CustomParse method, which would be passed in the full
|
// CustomParser has a CustomParse method, which would be passed in the full
|
||||||
// message content with the prefix and command trimmed. This is used
|
// message content with the prefix, command, subcommand and space trimmed. This
|
||||||
// for commands that require more advanced parsing than the default parser.
|
// is used for commands that require more advanced parsing than the default
|
||||||
//
|
// parser.
|
||||||
// Keep in mind that this does not trim arguments before it.
|
|
||||||
type CustomParser interface {
|
type CustomParser interface {
|
||||||
CustomParse(arguments string) error
|
CustomParse(arguments string) error
|
||||||
}
|
}
|
||||||
|
@ -100,9 +99,10 @@ type Argument struct {
|
||||||
pointer bool
|
pointer bool
|
||||||
|
|
||||||
// if nil, then manual
|
// if nil, then manual
|
||||||
fn argumentValueFn
|
fn argumentValueFn
|
||||||
manual *reflect.Method
|
|
||||||
custom *reflect.Method
|
manual func(ManualParser, []string) error
|
||||||
|
custom func(CustomParser, string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Argument) Type() reflect.Type {
|
func (a *Argument) Type() reflect.Type {
|
||||||
|
@ -132,9 +132,6 @@ func newArgument(t reflect.Type, variadic bool) (*Argument, error) {
|
||||||
|
|
||||||
// This shouldn't be variadic.
|
// This shouldn't be variadic.
|
||||||
if !variadic && typeI.Implements(typeICusP) {
|
if !variadic && typeI.Implements(typeICusP) {
|
||||||
mt, _ := typeI.MethodByName("CustomParse")
|
|
||||||
|
|
||||||
// TODO: maybe ish?
|
|
||||||
if t.Kind() == reflect.Ptr {
|
if t.Kind() == reflect.Ptr {
|
||||||
t = t.Elem()
|
t = t.Elem()
|
||||||
}
|
}
|
||||||
|
@ -143,14 +140,12 @@ func newArgument(t reflect.Type, variadic bool) (*Argument, error) {
|
||||||
String: fromUsager(t),
|
String: fromUsager(t),
|
||||||
rtype: t,
|
rtype: t,
|
||||||
pointer: ptr,
|
pointer: ptr,
|
||||||
custom: &mt,
|
custom: CustomParser.CustomParse,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// This shouldn't be variadic either.
|
// This shouldn't be variadic either.
|
||||||
if !variadic && typeI.Implements(typeIManP) {
|
if !variadic && typeI.Implements(typeIManP) {
|
||||||
mt, _ := typeI.MethodByName("ParseContent")
|
|
||||||
|
|
||||||
if t.Kind() == reflect.Ptr {
|
if t.Kind() == reflect.Ptr {
|
||||||
t = t.Elem()
|
t = t.Elem()
|
||||||
}
|
}
|
||||||
|
@ -159,7 +154,7 @@ func newArgument(t reflect.Type, variadic bool) (*Argument, error) {
|
||||||
String: fromUsager(t),
|
String: fromUsager(t),
|
||||||
rtype: t,
|
rtype: t,
|
||||||
pointer: ptr,
|
pointer: ptr,
|
||||||
manual: &mt,
|
manual: ManualParser.ParseContent,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -253,7 +253,7 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent, value refl
|
||||||
// If the argument wants all arguments:
|
// If the argument wants all arguments:
|
||||||
case last.manual != nil:
|
case last.manual != nil:
|
||||||
// Call the manual parse method:
|
// 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:
|
// If the argument wants all arguments in string:
|
||||||
case last.custom != nil:
|
case last.custom != nil:
|
||||||
|
@ -261,32 +261,16 @@ func (ctx *Context) callMessageCreate(mc *gateway.MessageCreateEvent, value refl
|
||||||
// have erroneous hanging quotes.
|
// have erroneous hanging quotes.
|
||||||
parseErr = nil
|
parseErr = nil
|
||||||
|
|
||||||
// Manual string seeking is a must here. This is because the string
|
content = strings.TrimSpace(content)
|
||||||
// could contain multiple whitespaces, and the parser would not
|
content = strings.TrimPrefix(content, cmd.Command)
|
||||||
// count them.
|
|
||||||
var seekTo = cmd.Command
|
|
||||||
// We can't rely on the plumbing behavior.
|
|
||||||
if sub.plumbed != nil {
|
|
||||||
seekTo = sub.Command
|
|
||||||
}
|
|
||||||
|
|
||||||
// Seek to the string.
|
if !sub.IsPlumbed() {
|
||||||
var i = strings.Index(content, seekTo)
|
content = strings.TrimSpace(content)
|
||||||
// Edge case if the subcommand is the same as the command.
|
content = strings.TrimPrefix(content, sub.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:])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call the method with the raw unparsed 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:
|
// Check the returned error:
|
||||||
|
|
Loading…
Reference in a new issue