Documentation ¶
Index ¶
- type Command
- type EventHandler
- type Middleware
- type Router
- func (r *Router) AddCommand(command string, handler EventHandler)
- func (r *Router) AddCommandFunc(command string, ...)
- func (r *Router) AddSubcommand(command string, f func(r *Router))
- func (r *Router) Handler(discord *discordgo.Session, event *discordgo.MessageCreate, command *Command)
- func (r *Router) HandlerBootstrap(discord *discordgo.Session, event *discordgo.MessageCreate)
- func (r *Router) Use(m Middleware)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct { Command string Arguments string Ctx context.Context Cancel context.CancelFunc }
func NewCommand ¶
type EventHandler ¶
type EventHandler interface { // An event handler, see the discordgo documentation for details on how event handling works // This entry being the most relevant: https://godoc.org/github.com/bwmarrin/discordgo#Session.AddHandler Handler(*discordgo.Session, *discordgo.MessageCreate, *Command) }
An event handler is any type that can receive discordgo events The event handler is typically going to be either a router or a wrapper created by EventHandlerFunc
func EventHandlerFunc ¶
func EventHandlerFunc(f func(*discordgo.Session, *discordgo.MessageCreate, *Command)) EventHandler
creates a wrapper around
type Middleware ¶
type Middleware func(EventHandler) EventHandler
type Router ¶
type Router struct { // The timeout for commands handled by this router, defaults to time.Second Timeout time.Duration // Specify whether or not to ignore commands issued by bots. // Regardless of the setting of this value, the bot will always ignore it's own messages. // This defaults to true when the router is created via NewRouter IgnoreBots bool // contains filtered or unexported fields }
A simple router for commands, typical of what you would find in an HTTP router.
func (*Router) AddCommand ¶
func (r *Router) AddCommand(command string, handler EventHandler)
func (*Router) AddCommandFunc ¶
func (*Router) AddSubcommand ¶
Adds a subcommand to the routes, where in "$x y z" y is a subcommand.
Example ¶
package main import ( "github.com/bwmarrin/discordgo" "github.com/getynge/chatterbot/routing" ) // blank functions for use in examples func userHandler(_ *discordgo.Session, _ *discordgo.MessageCreate, _ *routing.Command) {} func main() { router := routing.NewRouter("$") router.AddSubcommand("add", func(r *routing.Router) { r.AddCommandFunc("user", userHandler) }) }
Output:
Example (NestedRoutes) ¶
package main import ( "github.com/bwmarrin/discordgo" "github.com/getynge/chatterbot/routing" ) // blank functions for use in examples func userHandler(_ *discordgo.Session, _ *discordgo.MessageCreate, _ *routing.Command) {} func main() { router := routing.NewRouter("$") router.AddSubcommand("add", func(r *routing.Router) { r.AddSubcommand("users", func(r2 *routing.Router) { r2.AddCommandFunc("named", userHandler) }) }) }
Output:
func (*Router) Handler ¶
func (r *Router) Handler(discord *discordgo.Session, event *discordgo.MessageCreate, command *Command)
Never call this function directly. All routes have leading and trailing whitespace removed, along with the prefix. Assuming the prefix is "$", then all of the following are the same:
"$ Hi" "$Hi" "$ Hi "
Note that if you have multiple prefixes, only one of them will be recognized at a time.
func (*Router) HandlerBootstrap ¶
func (r *Router) HandlerBootstrap(discord *discordgo.Session, event *discordgo.MessageCreate)
Never call this function directly Pass this function as an argument to discordgo.AddHandler in order to bootstrap the router
func (*Router) Use ¶
func (r *Router) Use(m Middleware)
Adds a middleware function to the chain. Middleware is called in the order that it is defined.