routing

package
v0.0.0-20200518 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

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

func NewCommand(timeout time.Duration) *Command

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 NewRouter

func NewRouter(prefixes ...string) *Router

func (*Router) AddCommand

func (r *Router) AddCommand(command string, handler EventHandler)

func (*Router) AddCommandFunc

func (r *Router) AddCommandFunc(command string, handler func(*discordgo.Session, *discordgo.MessageCreate, *Command))

func (*Router) AddSubcommand

func (r *Router) AddSubcommand(command string, f func(r *Router))

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL