Remove some unneeded types and implement an error channel

master
DustyP 6 years ago
parent b3f35172c9
commit 67c4f7f2ab

@ -160,18 +160,15 @@ func (c *CommandManager) OnMessage(session *discordgo.Session, m *discordgo.Mess
guild, _ := session.Guild(m.GuildID) guild, _ := session.Guild(m.GuildID)
context := Context{ context := Context{
Session: session, Session: session,
Channel: channel, Channel: channel,
Message: m.Message, Message: m.Message,
User: m.Author, User: m.Author,
Guild: guild, Guild: guild,
Member: m.Member, Member: m.Member,
Invoked: invoked, Invoked: invoked,
} ErrorChannel: c.ErrorChannel,
err = command.Invoke(context, cmd[1:])
if err != nil && c.OnErrorFunc != nil {
c.OnErrorFunc(context, cmd[0], err)
} }
go command.Invoke(context, cmd[1:])
} }

@ -27,3 +27,16 @@ func (c *Context) SendFile(filename string, file io.Reader) (*discordgo.Message,
} }
// TODO Combine these to all use ChannelMessageSendComplex // 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,
}
}
}

@ -16,8 +16,8 @@ type CommandManager struct {
Owners []string Owners []string
// Status Manager which will handle updating the status of the bot // Status Manager which will handle updating the status of the bot
StatusManager StatusManager StatusManager StatusManager
// Function to call when there is an error with a command (not used currently) // Channel to send errors to
OnErrorFunc OnErrorFunc ErrorChannel chan CommandError
// Map of the command names to the pointer of the command to call // Map of the command names to the pointer of the command to call
Commands map[string]*Command Commands map[string]*Command
// Should we ignore bots when processing commands // Should we ignore bots when processing commands
@ -52,6 +52,16 @@ type Command struct {
Invoke CommandInvokeFunc 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 // Context contains all the context that a command needs to run
type Context struct { type Context struct {
// Discordgo Session Object // Discordgo Session Object
@ -68,4 +78,6 @@ type Context struct {
Member *discordgo.Member Member *discordgo.Member
// Name of the command as it was invoked (this is so you know what alias was used to call the command) // Name of the command as it was invoked (this is so you know what alias was used to call the command)
Invoked string Invoked string
// Error channel
ErrorChannel chan CommandError
} }

@ -7,14 +7,11 @@ package disgoman
*/ */
// CommandInvokeFunc is the function type for commands // 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. // PrefixesFunc gets the prefixes for the bot.
type PrefixesFunc func(string) []string 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 // Permission type to help with managing permissions for commands
type Permission int type Permission int

Loading…
Cancel
Save