Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Argument ¶
type Argument string
Argument represents a parsed argument from a command
func (Argument) AsBool ¶
AsBool() converts the argument to a bool, returning false in the second value if the conversion fails
func (Argument) AsFloat ¶
AsFloat() converts the argument to a float, returning false in the second value if the conversion fails
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
}
type Token ¶
type Token string
func (Token) IsWildcard ¶
IsWildcard returns true if this token is a wildcard
type TokenList ¶
type TokenList []Token
TokenList represents an ordered list of tokens used for matching
func Tokenize ¶
Tokenize turns a string into a TokenList by collapsing all whitespace into single spaces and splitting the result on space
func (TokenList) ExactlyMatches ¶
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
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