From cafff103e51ce658d536b66149dd048d6696803f Mon Sep 17 00:00:00 2001 From: diamondburned Date: Thu, 8 Apr 2021 13:09:10 -0700 Subject: [PATCH] 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. --- bot/ctx.go | 9 +++++++++ bot/ctx_call.go | 8 ++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/bot/ctx.go b/bot/ctx.go index 864983e..07cdbb1 100644 --- a/bot/ctx.go +++ b/bot/ctx.go @@ -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. diff --git a/bot/ctx_call.go b/bot/ctx_call.go index 4519154..20b129a 100644 --- a/bot/ctx_call.go +++ b/bot/ctx_call.go @@ -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: