router

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2020 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Weight

func Weight(tl TokenList) int

Weight determines the ordering weight of a TokenList, based on the number of tokens, less the number of wildcard tokens

Types

type Argument

type Argument string

Argument represents a parsed argument from a command

func (Argument) AsBool

func (a Argument) AsBool() (bool, bool)

AsBool() converts the argument to a bool, returning false in the second value if the conversion fails

func (Argument) AsFloat

func (a Argument) AsFloat() (float64, bool)

AsFloat() converts the argument to a float, returning false in the second value if the conversion fails

func (Argument) AsInt

func (a Argument) AsInt() (int64, bool)

AsInt() converts the argument to an integer, returning false in the second value if the conversion fails

func (Argument) String

func (a Argument) String() string

String() returns the original string value of the argument

type ArgumentList

type ArgumentList []Argument

func (*ArgumentList) Pop

func (args *ArgumentList) Pop() Argument

Pop removes and returns the first argument from the argument list

func (ArgumentList) String

func (args ArgumentList) String() string

String joins the string values of the arguments in the list together with spaces

type ChainedExecutionHandler added in v0.2.0

type ChainedExecutionHandler func(ctx *ExecutionContext) (proceed bool)

type Command

type Command struct {
	Name    string
	Handler ExecutionHandler
	Aliases []string
	// Tells the router the minimum number of arguments must be supplied to this command in order for it to function as intended.
	// This is used to break ties in command routing.
	MinimumArgumentCount int
	// Optional description of the command for built-in help functionality
	Description string
	// Optional example usage for built-in help functionality
	Usage       string
	SubCommands []Command
}

A Command represents functionality of a bot. It must have a Name and may have zero or more Aliases, all of which are a word or phrase that triggers the associated Handler.

Commands can be a single word, like `help` or `status`, or multi-word phrases, like `current uptime` or `current memory`, and may contain wildcards. When matching commands to incoming messages, the router tokenizes the command phrases and the incoming text by splitting on whitespace. The command with the most matching tokens is the one that is executed. If multiple commands have the same number of tokens, exact matches are selected in favor of wildcard matches, and remaining ambiguity is resolved in lexical order of the non-wildcard tokens.

Do NOT prepend a trigger or escape character, such as '!', to the name or aliases! This is handled by the Router itself.

Wildcards are allowed to match multiple input tokens, unless the input Token would match the following command Token. In other words, the command

say * to bob

would match the following:

say you're awesome to bob
say don't ever change to bob

But not:

say want to go to fred's? to bob

Input tokens matched to a wildcard are included in order of appearance at the beginning of the ExecutionContext's arguments.

You can define commands that take multiple options in several ways. First, by using two commands with the same names and/or aliases, such as the uptime and memory examples above. Second, by specifying only the `current` command, and providing logic to handle the first arguments of `uptime` and `memory`. Third, by defining the `current` command, and specifying two subcommands named `uptime` and `memory`, each with their own handler functions.

type ExecutionContext added in v0.2.0

type ExecutionContext struct {
	// The Name of the command, or the Alias that triggered ti
	Trigger string
	// The remaining text of the original message after removing the trigger, split on whitespace
	Arguments ArgumentList

	Message *discordgo.Message
	Session *discordgo.Session
	// contains filtered or unexported fields
}

ExecutionContext represents the state of the bot at the time of command execution, as well as providing access to the discordgo.Session, original discordgo.Message, any data injected by the middleware, and helper functions to quickly respond with Text or Embed replies.

func (ExecutionContext) Get added in v0.2.0

func (c ExecutionContext) Get(key string) (interface{}, bool)

Get returns the value of the given key, and a bool indicating the presence of the key

func (*ExecutionContext) Reply added in v0.2.0

func (c *ExecutionContext) Reply(text string) error

Reply sends the specified string to the channel on which this command was received

func (*ExecutionContext) ReplyEmbed added in v0.2.0

func (c *ExecutionContext) ReplyEmbed(embed *discordgo.MessageEmbed) error

ReplyEmbed sends the specified Embed to the channel on which this command was received

func (*ExecutionContext) Set added in v0.2.0

func (c *ExecutionContext) Set(key string, value interface{})

Set associates the given key with the given interface in the current ExecutionContext

type ExecutionHandler added in v0.2.0

type ExecutionHandler func(ctx *ExecutionContext)

ExecutionHandler is a function responsible for performing logic in response to a Command

type Router

type Router struct {
	// contains filtered or unexported fields
}

func New

func New(triggers []string, commands []Command, middleware []ChainedExecutionHandler) Router

func (Router) Start

func (r Router) Start(session *discordgo.Session)

Start adds this router's handler functions to the session

type Token

type Token string

func (Token) IsWildcard

func (t Token) IsWildcard() bool

IsWildcard returns true if this token is a wildcard

func (Token) Matches

func (t Token) Matches(t2 Token) bool

Matches returns true if the two tokens are a match, or if the first token is a wildcard

func (Token) String

func (t Token) String() string

type TokenList

type TokenList []Token

TokenList represents an ordered list of tokens used for matching

func Tokenize

func Tokenize(s string) TokenList

Tokenize turns a string into a TokenList by collapsing all whitespace into single spaces and splitting the result on space

func (TokenList) ExactlyMatches

func (tl TokenList) ExactlyMatches(tl2 TokenList) bool

ExactlyMatches returns true if two TokenList instances are the same length and all elements satisfy strings.EqualFold(list1[index].String(), list2[index].String())

func (TokenList) Match

func (tl TokenList) Match(inputTokens TokenList) TokenListMatch

Match builds a TokenListMatch from a TokenList and a string

func (TokenList) String

func (tl TokenList) String() string

type TokenListMatch

type TokenListMatch struct {
	// Input holds the original string that was matched
	Input string
	// MatchLength is the number of tokens in the list that matched
	MatchLength int
	// WildsCount is the number of tokens in the match that were wildcards
	WildsCount int
	// Arguments is an ArgumentList constructed from any matched wildcards and leftover tokens
	Arguments ArgumentList
}

TokenListMatch represents the results of matching a TokenList to a string

Jump to

Keyboard shortcuts

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