1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-09 16:35:12 +00:00
arikawa/bot/error.go

82 lines
1.5 KiB
Go
Raw Normal View History

2020-01-19 06:06:00 +00:00
package bot
import (
2020-05-03 22:59:10 +00:00
"errors"
2020-01-19 06:06:00 +00:00
"strings"
)
type ErrUnknownCommand struct {
2020-05-03 22:59:10 +00:00
Prefix string
2020-01-19 06:06:00 +00:00
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-05-03 22:59:10 +00:00
var header = "Unknown command: " + err.Prefix
2020-01-19 06:06:00 +00:00
if err.Parent != "" {
header += err.Parent + " " + err.Command
} else {
header += err.Command
}
return header
}
2020-05-03 22:59:10 +00:00
var (
ErrTooManyArgs = errors.New("Too many arguments given")
ErrNotEnoughArgs = errors.New("Not enough arguments given")
)
2020-01-19 06:06:00 +00:00
type ErrInvalidUsage struct {
2020-05-03 22:59:10 +00:00
Prefix string
Args []string
Index int
Wrap error
2020-01-19 06:06:00 +00:00
// 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)
}
2020-05-03 22:59:10 +00:00
func (err *ErrInvalidUsage) Unwrap() error {
return err.Wrap
}
2020-01-25 02:49:33 +00:00
var InvalidUsageString = func(err *ErrInvalidUsage) string {
2020-01-19 06:06:00 +00:00
if err.Index == 0 {
2020-05-03 22:59:10 +00:00
return "Invalid usage, error: " + err.Wrap.Error() + "."
2020-01-19 06:06:00 +00:00
}
if len(err.Args) == 0 {
return "Missing arguments. Refer to help."
}
body := "Invalid usage at " +
2020-05-03 22:59:10 +00:00
// Write the prefix.
err.Prefix +
// 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
2020-05-03 22:59:10 +00:00
if err.Wrap != nil {
body += "\nError: " + err.Wrap.Error() + "."
2020-01-19 06:06:00 +00:00
}
return body
}