diff --git a/command-manager.go b/command-manager.go index ca88753..bd69723 100644 --- a/command-manager.go +++ b/command-manager.go @@ -160,18 +160,15 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess guild, _ := session.Guild(m.GuildID) context := Context{ - Session: session, - Channel: channel, - Message: m.Message, - User: m.Author, - Guild: guild, - Member: m.Member, - Invoked: invoked, - } - - err = command.Invoke(context, cmd[1:]) - if err != nil && c.OnErrorFunc != nil { - c.OnErrorFunc(context, cmd[0], err) + Session: session, + Channel: channel, + Message: m.Message, + User: m.Author, + Guild: guild, + Member: m.Member, + Invoked: invoked, + ErrorChannel: c.ErrorChannel, } + go command.Invoke(context, cmd[1:]) } diff --git a/context.go b/context.go index d871a09..c7536ae 100644 --- a/context.go +++ b/context.go @@ -27,3 +27,16 @@ func (c *Context) SendFile(filename string, file io.Reader) (*discordgo.Message, } // TODO Combine these to all use ChannelMessageSendComplex + +// SendError makes a CommandError and sends it to the ErrorChannel. This includes the current context in the error. +// Will block if the channel buffer is full. It is up to the client to implement a channel for the errors as well as +// a function to handle the errors from said channel. If the ErrorChannel is nil then this does nothing. +func (c *Context) SendError(message string, err error) { + if c.ErrorChannel != nil { + c.ErrorChannel <- CommandError{ + Context: *c, + Message: message, + Error: err, + } + } +} diff --git a/structs.go b/structs.go index 4b256d0..19c824e 100644 --- a/structs.go +++ b/structs.go @@ -16,8 +16,8 @@ type CommandManager struct { Owners []string // Status Manager which will handle updating the status of the bot StatusManager StatusManager - // Function to call when there is an error with a command (not used currently) - OnErrorFunc OnErrorFunc + // Channel to send errors to + ErrorChannel chan CommandError // Map of the command names to the pointer of the command to call Commands map[string]*Command // Should we ignore bots when processing commands @@ -52,6 +52,16 @@ type Command struct { Invoke CommandInvokeFunc } +// CommandError contains all the information needed to process an error in a command +type CommandError struct { + // The Context the command was run in + Context Context + // Error Message + Message string + // The Error object + Error error +} + // Context contains all the context that a command needs to run type Context struct { // Discordgo Session Object @@ -68,4 +78,6 @@ type Context struct { Member *discordgo.Member // Name of the command as it was invoked (this is so you know what alias was used to call the command) Invoked string + // Error channel + ErrorChannel chan CommandError } diff --git a/types.go b/types.go index 2c514dd..1d32b6c 100644 --- a/types.go +++ b/types.go @@ -7,14 +7,11 @@ package disgoman */ // CommandInvokeFunc is the function type for commands -type CommandInvokeFunc func(Context, []string) error +type CommandInvokeFunc func(Context, []string) // PrefixesFunc gets the prefixes for the bot. type PrefixesFunc func(string) []string -// OnErrorFunc runs upon command error -type OnErrorFunc func(Context, string, error) - // Permission type to help with managing permissions for commands type Permission int