2020-01-19 06:06:00 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ErrUnknownCommand struct {
|
|
|
|
Command string
|
|
|
|
Parent string
|
|
|
|
|
|
|
|
// TODO: list available commands?
|
|
|
|
// Here, as a reminder
|
|
|
|
ctx []*CommandContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *ErrUnknownCommand) Error() string {
|
2020-01-25 02:49:33 +00:00
|
|
|
return UnknownCommandString(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var UnknownCommandString = func(err *ErrUnknownCommand) string {
|
2020-04-06 20:25:42 +00:00
|
|
|
var header = "Unknown command: "
|
2020-01-19 06:06:00 +00:00
|
|
|
if err.Parent != "" {
|
|
|
|
header += err.Parent + " " + err.Command
|
|
|
|
} else {
|
|
|
|
header += err.Command
|
|
|
|
}
|
|
|
|
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
|
|
|
type ErrInvalidUsage struct {
|
2020-04-06 20:25:42 +00:00
|
|
|
Args []string
|
2020-01-19 06:06:00 +00:00
|
|
|
Index int
|
|
|
|
Err string
|
|
|
|
|
|
|
|
// TODO: usage generator?
|
|
|
|
// Here, as a reminder
|
2020-01-25 02:49:33 +00:00
|
|
|
Ctx *CommandContext
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (err *ErrInvalidUsage) Error() string {
|
2020-01-25 02:49:33 +00:00
|
|
|
return InvalidUsageString(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var InvalidUsageString = func(err *ErrInvalidUsage) string {
|
2020-01-19 06:06:00 +00:00
|
|
|
if err.Index == 0 {
|
2020-01-21 04:25:47 +00:00
|
|
|
return "Invalid usage, error: " + err.Err
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(err.Args) == 0 {
|
|
|
|
return "Missing arguments. Refer to help."
|
|
|
|
}
|
|
|
|
|
2020-04-06 20:25:42 +00:00
|
|
|
body := "Invalid usage at " +
|
|
|
|
// Write the first part
|
|
|
|
strings.Join(err.Args[:err.Index], " ") +
|
|
|
|
// Write the wrong part
|
|
|
|
" __" + err.Args[err.Index] + "__ " +
|
|
|
|
// Write the last part
|
|
|
|
strings.Join(err.Args[err.Index+1:], " ")
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
if err.Err != "" {
|
|
|
|
body += "\nError: " + err.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
return body
|
|
|
|
}
|