route

package module
v0.0.0-...-d9994b5 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2020 License: MIT Imports: 6 Imported by: 2

README

route

Command router for Snart.

Documentation

Overview

Package route contains a command router for a Snart Bot.

Index

Constants

This section is empty.

Variables

View Source
var Log = minori.GetLogger("route")

Log is the logger for the route package.

Functions

func Split

func Split(s string) []string

Split splits a string using a backtick quoting method.

Types

type Ctx

type Ctx struct {
	Prefix      string
	CleanPrefix string
	Session     *dg.Session
	Message     *dg.Message
	Flags       *Flags
	Route       *Route
}

Ctx holds a command context.

func (*Ctx) Reply

func (c *Ctx) Reply() *Reply

Reply gets a Reply for the Ctx.

func (*Ctx) Run

func (c *Ctx) Run() error

Run is a shortcut to c.Route.Func(c).

func (*Ctx) Wait

func (c *Ctx) Wait(general Okay, specific Okay) *Wait

Wait creates a Wait from the Ctx and two Okays, with cancellation enabled.

func (*Ctx) WaitCancel

func (c *Ctx) WaitCancel(general Okay, specific Okay, cancel bool) *Wait

WaitCancel creates a Wait from the Ctx and two Okays, with optional cancellation.

type Flags

type Flags struct {
	*flag.FlagSet
	// contains filtered or unexported fields
}

Flags holds a FlagSet for a Ctx.

func NewFlags

func NewFlags(ctx *Ctx, name string, args []string) *Flags

NewFlags creates a Flags.

func (*Flags) Output

func (f *Flags) Output() string

Output retrieves the Flags' FlagSet's Output as a string.

func (*Flags) Parse

func (f *Flags) Parse() error

Parse parses the arguments given to the Flags.

func (*Flags) Usage

func (f *Flags) Usage() *Reply

Usage generates a *Reply containing usage info from the Flags.

type Okay

type Okay func(*Ctx) bool

Okay is a function which checks if a Ctx should be used.

var False Okay = func(*Ctx) bool {
	return false
}

False is an Okay that always returns false.

var GuildAdmin Okay = func(c *Ctx) bool {
	_f := "GuildAdmin"

	perm, err := c.Session.UserChannelPermissions(c.Message.Author.ID, c.Message.ChannelID)
	if err != nil {
		err = fmt.Errorf("perm: %w", err)
		Log.Error(_f, err)
		return false
	}

	return perm&(dg.PermissionAdministrator|
		dg.PermissionManageServer) > 0
}

GuildAdmin is an Okay that checks if the user has administrator privileges on the guild.

var True Okay = func(*Ctx) bool {
	return true
}

True is an Okay that always returns true.

func All

func All(chs ...Okay) Okay

All is a logical AND of Okays.

func Any

func Any(chs ...Okay) Okay

Any is a logical OR of Okays.

type Reply

type Reply struct {
	*dg.MessageSend

	Session   *dg.Session
	ChannelID string
}

Reply wraps a message to be sent to a given ChannelID using a given Session.

func (*Reply) Send

func (r *Reply) Send() error

Send is a shortcut for SendMsg that logs a warning on error and elides the resulting *dg.Message.

func (*Reply) SendMsg

func (r *Reply) SendMsg() (*dg.Message, error)

SendMsg sends the Reply.

type Route

type Route struct {
	Name  string
	Match string

	Cat  string
	Desc string
	Okay Okay
	Func func(*Ctx) error
	// contains filtered or unexported fields
}

Route is a command route.

type Router

type Router []*Route

Router is a slice of Routes.

func NewRouter

func NewRouter() *Router

NewRouter creates a Router.

func (*Router) Add

func (rr *Router) Add(rs ...*Route)

Add adds a Route to the Router.

func (*Router) Ctx

func (rr *Router) Ctx(pfx, cpfx string, s *dg.Session, m *dg.Message, line string) *Ctx

Ctx gets a Ctx by finding an appropriate Route for a given prefix, session, message, etc.

type Wait

type Wait struct {
	General  Okay
	Specific Okay
	Return   chan *Ctx
	Cancel   func()
}

Wait contains two Okays, a Return channel, and a handler cancel function.

func (*Wait) Handle

func (w *Wait) Handle(s *dg.Session, m *dg.MessageCreate)

Handle handles MessageCreate events and passes them to the Wait's Return chan.

Jump to

Keyboard shortcuts

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