1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2025-01-21 03:57:26 +00:00

Bot: Add ErrorReplier

This commit adds the ErrorReplier callback into Context, which allows
the user to implement a custom way to format errors, such as putting
them into an embed.
This commit is contained in:
diamondburned 2021-04-08 13:09:10 -07:00
parent 728bc5c472
commit cafff103e5
2 changed files with 15 additions and 2 deletions

View file

@ -127,6 +127,15 @@ type Context struct {
// MessageCreate events.
ReplyError bool
// ErrorReplier is an optional function that allows changing how the error
// is replied. It overrides ReplyError and is only used for MessageCreate
// events.
//
// Note that errors that are passed in here will bypas FormatError; in other
// words, the implementation might only care about ErrorReplier and leave
// FormatError as it is.
ErrorReplier func(err error, src *gateway.MessageCreateEvent) api.SendMessageData
// EditableCommands when true will also listen for MessageUpdateEvent and
// treat them as newly created messages. This is convenient if you want
// to quickly edit a message and re-execute the command.

View file

@ -114,14 +114,18 @@ func (ctx *Context) callMessageCreate(
return nil
}
if err != nil && !ctx.ReplyError {
if err != nil && !ctx.ReplyError && ctx.ErrorReplier == nil {
return err
}
var data api.SendMessageData
if err != nil {
data.Content = ctx.FormatError(err)
if ctx.ErrorReplier != nil {
data = ctx.ErrorReplier(err, mc)
} else {
data.Content = ctx.FormatError(err)
}
} else {
switch v := v.(type) {
case string: