command

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context added in v0.8.0

type Context struct {
	CommandName string                 // CommandName represents a name of a current command.
	PrivMsg     *twitch.PrivateMessage // PrivMsg represents metadata of the sent message.
	Logger      *zap.Logger            // Logger records and captures events.
}

Context represents a struct with helping fields for validating and recording a command workflow.

func NewContext added in v0.8.0

func NewContext(commandName string, privMsg *twitch.PrivateMessage, logger *zap.Logger) *Context

NewContext returns a new instance of a struct with metadata and a logger to a command lifecycle.

func UnwrapContext added in v0.8.0

func UnwrapContext(ctx context.Context) *Context

UnwrapContext returns Command Context from a context.

type Controller added in v0.3.1

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

Controller represents a manager to commands.

func NewController

func NewController(prefix string, logger *zap.Logger) *Controller

NewController creates an instance of Controller for managing commands.

func (*Controller) AddCommand added in v0.3.1

func (c *Controller) AddCommand(commandName string, handler Handler, filters []Filter)

AddCommand adds a command handler to a map in Controller. The handler is being wrapped with filters and middlewares, before it is added to commands. The order of functions in wrapped handler goes like this: Middlewares -> Filters -> Handler.

func (*Controller) CallCommand added in v0.3.1

func (c *Controller) CallCommand(ctx context.Context, userMessage string, privateMessage twitch.PrivateMessage, chatClient chatClient)

CallCommand searches for a command in the commands. If the method finds one, it sets up a context and executes the command.

func (*Controller) UseWith added in v0.5.0

func (c *Controller) UseWith(middleware Middleware)

UseWith adds a middleware to a middlewares. The order when a middleware is added matters.

type Filter added in v0.5.0

type Filter func(Handler) Handler

Filter represents a function that is called after all middlewares and before a command. It is used for validation.

func Cooldown added in v0.7.0

func Cooldown(cooldown time.Duration) Filter

Cooldown stops from calling a command, when not enough time passed.

func HasRole

func HasRole(roles []string) Filter

HasRole rejects user's command request, when the user does not have a role for that. The roles are compared with users's twitch badges.

type Handler added in v0.5.0

type Handler func(ctx context.Context, args []string, chatClient chatClient) error

Handler represents a function for a command.

var Ping Handler = func(ctx context.Context, _ []string, chatClient chatClient) error {
	_, span := tracer.Start(ctx, "ping")
	defer span.End()

	cmdCtx := UnwrapContext(ctx)
	chatClient.Say(cmdCtx.PrivMsg.Channel, fmt.Sprintf("Pong! @%s", cmdCtx.PrivMsg.User.DisplayName))

	span.SetStatus(codes.Ok, "successfully sent a ping")
	return nil
}

type Middleware added in v0.5.0

type Middleware func(Handler) Handler

Middleware represents a function that is called before any filters and a handler. It can be used for logging handler's parameters, error handling or measure how much time a command took to execute.

func ErrorHandler added in v0.5.0

func ErrorHandler() Middleware

ErrorHandler takes care of returned errors (if it's present) from a executed command.

Jump to

Keyboard shortcuts

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