botcmd

package
v0.0.1-rc1 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRegistry = NewRegistry()

DefaultRegistry is the global registry instance used by default.

View Source
var ErrNilMessage = errors.New("message was nil")

Functions

func AddCommand

func AddCommand(cmd *BasicCommand) bool

AddCommand adds a command to the default registry.

func AddPattern

func AddPattern(cmd *PatternCommand) bool

AddPattern adds a pattern command to the default registry.

func AddReactionHandler

func AddReactionHandler(cmd *ReactionCommand) bool

AddReactionHandler adds a reaction command to the default registry.

func FormatTime

func FormatTime(d time.Time) string

FormatTime formats a given time.Time instance as UTC in a simple format.

func MustAddCommand

func MustAddCommand(cmd *BasicCommand)

MustAddCommand adds a command to the default registry or panics.

func MustAddPattern

func MustAddPattern(cmd *PatternCommand)

MustAddPattern adds a pattern command to the default registry or panics.

func MustAddReactionHandler

func MustAddReactionHandler(cmd *ReactionCommand)

AddReactionHandler adds a reaction command to the default registry or panics.

func ParseFlags

func ParseFlags(text string, flagSet *flag.FlagSet) string

parseFlags tries to parse the given text with the given flagset. It sets the flagset's Usage function to a bufferedHelp instance and when help is requested returns the buffer.

Types

type BasicCommand

type BasicCommand struct {
	// Name is the name of the command and how it is invoked.
	// TODO: Support aliases.
	Name string
	// Description is a short description of the command for help output.
	Description string
	// Icon is an emoji (no ":" delimiters) to use for this command in help
	// output.
	Icon string
	// Handler is a CommandHandler invoked when a command invocation for Name is
	// performed by a user.
	Handler CommandHandler
}

BasicCommand describes a basic bot command that can be invoked on demand with "!<cmd name>". Remaining text is passed to the command handler so that it can be parsed further (e.g. to have CLI options for the command).

type CommandHandler

type CommandHandler interface {
	Configurable
	// Run is called for command matches with the text that appeared in the
	// message after the command name and a run context.
	Run(text string, runCtx RunContext) (RunResult, error)
}

CommandHandler describes a configurable that expects to be run with some text when the associated basic command is invoked.

type CommandRegistry

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

CommandRegistry describes a collection of basic commands, pattern commands and reaction handlers. It is not thread safe - do not use concurrently.

func NewRegistry

func NewRegistry() *CommandRegistry

NewRegistry constructs a new CommandRegistry.

func (*CommandRegistry) AddCommand

func (c *CommandRegistry) AddCommand(cmd *BasicCommand) bool

AddCommand adds a basic command to the registry. It returns false if the command is invalid or if the name was already registered.

func (*CommandRegistry) AddPattern

func (c *CommandRegistry) AddPattern(cmd *PatternCommand) bool

AddPattern adds a pattern command to the registry. It returns false if the pattern command is invalid or if the pattern name was already registered.

func (*CommandRegistry) AddReactionHandler

func (c *CommandRegistry) AddReactionHandler(cmd *ReactionCommand) bool

AddReactionHandler adds a reaction handler to the registry. It returns false if the reaction command is invalid or if the reaction command name was already registered.

func (CommandRegistry) GetCommand

func (c CommandRegistry) GetCommand(cmdName string) *BasicCommand

GetCommand returns the BasicCommand registered with the given cmdName (or nil if there was no such cmd).

func (CommandRegistry) GetCommands

func (c CommandRegistry) GetCommands() []*BasicCommand

GetCommands returns a list of the registered BasicCommands.

func (CommandRegistry) GetConfigurables

func (c CommandRegistry) GetConfigurables() []Configurable

GetConfigurables returns a list of all of the configurables in the registry. This includes basic commands, patterns and reaction handlers.

func (CommandRegistry) GetPattern

func (c CommandRegistry) GetPattern(patternName string) *PatternCommand

GetPattern returns the Pattern Command registered with the given patternName (or nil if there was no such pattern).

func (CommandRegistry) GetPatterns

func (c CommandRegistry) GetPatterns() []*PatternCommand

GetPatterns returns a list of the registered PatternCommands.

func (CommandRegistry) GetReactionHandler

func (c CommandRegistry) GetReactionHandler(cmdName string) *ReactionCommand

GetReactionHandler returns the Reaction Command registered with the given cmdName (or nil if there was no such handler).

func (CommandRegistry) GetReactionHandlers

func (c CommandRegistry) GetReactionHandlers() []*ReactionCommand

GetReactionHandlers returns a list of the registered ReactionCommands.

type Configurable

type Configurable interface {
	// Configure is called with a log and config instance once they are available and
	// before any Run calls are made.
	Configure(log *logrus.Logger, c *config.Config) error
}

Configurable is a common interface for anything (cmd, pattern cmd, reaction handler, etc) that can be configured with a logger and config instance.

type PatternCommand

type PatternCommand struct {
	// Name of the PatternCommand. NOTE: used in help output.
	Name string
	// PatternHandler to invoke with matches.
	Handler PatternHandler
	// Pattern regexp. Messages matching this pattern will have the Handler invoked.
	Pattern *regexp.Regexp
}

PatternCommand describes a pattern handler and its associated pattern. Any messages that match the pattern will have the handler's Run function invoked with all of the submatches.

type PatternHandler

type PatternHandler interface {
	Configurable
	// Run accepts a list of all of the submatches from a regexp and a runCtx.
	Run(allSubmatches [][]string, runCtx RunContext) (RunResult, error)
}

PatternHandler describes a configurable that expects to be run with all submatches of a regex pattern.

type ReactionCommand

type ReactionCommand struct {
	// Name of the reaction handler. Used in help, should describe purpose of handler.
	Name string
	// Handler is invoked when reactions are added/removed.
	Handler ReactionHandler
}

ReactionCommand describes a named reacton handler that is run when reactions are added/removed.

type ReactionHandler

type ReactionHandler interface {
	Configurable
	// Run is called when a reaction is added or removed.
	Run(reaction *slack.Reaction, runCtx RunContext) error
}

ReactionHandler describes a configurable that has its Run function called when reactions are added/removed.

type RunContext

type RunContext struct {
	Message *slack.Message
	Storage storage.Storage
	Slack   slack.Client
}

RunContext binds together all of the contextual information a botcmd's Run function might need. Typically this is a message instance, a handle to storage, and a handle to a slack client to interact with.

type RunResult

type RunResult struct {
	Message string
	Reactji []string
}

RunResult is returned by a botcmd's Run function and can be used as a simple way to post a reply message and/or add reactions to the message that caused the botcmd to be run.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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