cmdroute

package
v3.3.6 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: ISC Imports: 6 Imported by: 17

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OverwriteCommands

func OverwriteCommands(client BulkCommandsOverwriter, cmds []api.CreateCommandData) error

OverwriteCommands overwrites all commands for the current application.

Types

type AutocompleteData

type AutocompleteData struct {
	discord.AutocompleteOption
	Event *discord.InteractionEvent
	Data  *discord.AutocompleteInteraction
}

AutocompleteData is passed to an Autocompleter's Autocomplete method.

type Autocompleter

type Autocompleter interface {
	// Autocomplete is expected to return a list of choices synchronously.
	// If nil is returned, then no responses will be sent. The function must
	// return an empty slice if there are no choices.
	Autocomplete(ctx context.Context, data AutocompleteData) api.AutocompleteChoices
}

Autocompleter is a type for an autocompleter.

type AutocompleterFunc

type AutocompleterFunc func(ctx context.Context, data AutocompleteData) api.AutocompleteChoices

AutocompleterFunc is a function that implements the Autocompleter interface.

func (AutocompleterFunc) Autocomplete

Autocomplete implements webhook.InteractionHandler.

type BulkCommandsOverwriter

type BulkCommandsOverwriter interface {
	CurrentApplication() (*discord.Application, error)
	BulkOverwriteCommands(appID discord.AppID, cmds []api.CreateCommandData) ([]discord.Command, error)
}

BulkCommandsOverwriter is an interface that allows to overwrite all commands at once. Everything *api.Client will implement this interface, including *state.State.

type CommandData

CommandData is passed to a CommandHandler's HandleCommand method.

type CommandHandler

type CommandHandler interface {
	// HandleCommand is expected to return a response synchronously, either to
	// be followed-up later by deferring the response or to be responded
	// immediately.
	//
	// All HandleCommand invocations are given a 3-second deadline. If the
	// handler does not return a response within the deadline, the response will
	// be automatically deferred in a goroutine, and the returned response will
	// be sent to the user through the API instead.
	HandleCommand(ctx context.Context, data CommandData) *api.InteractionResponseData
}

CommandHandler is a slash command handler.

type CommandHandlerFunc

type CommandHandlerFunc func(ctx context.Context, data CommandData) *api.InteractionResponseData

CommandHandlerFunc is a function that implements CommandHandler.

func (CommandHandlerFunc) HandleCommand

HandleCommand implements CommandHandler.

type ComponentData added in v3.3.3

type ComponentData struct {
	discord.ComponentInteraction
	Event *discord.InteractionEvent
}

ComponentData is passed to a ComponentHandler's HandleComponent method.

type ComponentHandler added in v3.3.3

type ComponentHandler interface {
	// HandleComponent is expected to return a response synchronously, either
	// to be followed-up later by deferring the response or to be responded
	// immediately.
	HandleComponent(ctx context.Context, data ComponentData) *api.InteractionResponse
}

ComponentHandler is a type for a component handler.

type ComponentHandlerFunc added in v3.3.3

type ComponentHandlerFunc func(ctx context.Context, data ComponentData) *api.InteractionResponse

ComponentHandlerFunc is a function that implements the ComponentHandler interface.

func (ComponentHandlerFunc) HandleComponent added in v3.3.3

HandleComponent implements ComponentHandler.

type DeferOpts

type DeferOpts struct {
	// Timeout is the timeout for the handler to return a response. If the
	// handler does not return within this timeout, then it is deferred.
	//
	// Defaults to 1.5 seconds.
	Timeout time.Duration
	// Flags is the flags to set on the response.
	Flags discord.MessageFlags
	// Error is called when a follow-up message fails to send. If nil, it does
	// nothing.
	Error func(err error)
	// Done is called when the handler is done. If nil, it does nothing.
	Done func(*discord.Message)
}

DeferOpts is the options for Deferrable().

type DeferTicket

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

DeferTicket is a ticket that can be used to defer a slash command. It can be used to manually send a response later.

func DeferTicketFromContext

func DeferTicketFromContext(ctx context.Context) DeferTicket

DeferTicketFromContext returns the DeferTicket from the context. If no ticket is found, it returns a zero-value ticket.

func (DeferTicket) Context

func (t DeferTicket) Context() context.Context

Context returns the context that is done when the handler is deferred. If DeferTicket is zero-value, it returns the background context.

func (DeferTicket) Defer

func (t DeferTicket) Defer()

Defer defers the response. If DeferTicket is zero-value, it does nothing.

func (DeferTicket) IsDeferred

func (t DeferTicket) IsDeferred() bool

IsDeferred returns true if the handler has been deferred.

type FollowUpSender

type FollowUpSender interface {
	FollowUpInteraction(appID discord.AppID, token string, data api.InteractionResponseData) (*discord.Message, error)
}

FollowUpSender is a type that can send follow-up messages. Usually, anything that extends *api.Client can be used as a FollowUpSender.

type InteractionHandler

type InteractionHandler interface {
	// HandleInteraction is expected to return a response synchronously, either
	// to be followed-up later by deferring the response or to be responded
	// immediately.
	HandleInteraction(context.Context, *discord.InteractionEvent) *api.InteractionResponse
}

InteractionHandler is similar to webhook.InteractionHandler, but it also includes a context.

type InteractionHandlerFunc

type InteractionHandlerFunc func(context.Context, *discord.InteractionEvent) *api.InteractionResponse

InteractionHandlerFunc is a function that implements InteractionHandler.

func (InteractionHandlerFunc) HandleInteraction

HandleInteraction implements InteractionHandler.

type Middleware

type Middleware = func(next InteractionHandler) InteractionHandler

Middleware is a function type that wraps a Handler. It can be used as a middleware for the handler.

func Deferrable

func Deferrable(client FollowUpSender, opts DeferOpts) Middleware

Deferrable marks a router as deferrable, meaning if the handler does not return a response within the deadline, the response will be automatically deferred.

func UseContext

func UseContext(ctx context.Context) Middleware

UseContext returns a middleware that override the handler context to the given context. This middleware should only be used once in the parent-most router.

type Router

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

Router is a router for slash commands. A zero-value Router is a valid router.

func NewRouter

func NewRouter() *Router

NewRouter creates a new Router.

func (*Router) Add

func (r *Router) Add(name string, h CommandHandler)

Add registers a slash command handler for the given command name.

func (*Router) AddAutocompleter

func (r *Router) AddAutocompleter(name string, ac Autocompleter)

AddAutocompleter registers an autocompleter for the given command name.

func (*Router) AddAutocompleterFunc

func (r *Router) AddAutocompleterFunc(name string, f AutocompleterFunc)

AddAutocompleterFunc is a convenience function that calls AddAutocompleter with an AutocompleterFunc.

func (*Router) AddComponent added in v3.3.3

func (r *Router) AddComponent(id string, f ComponentHandler)

AddComponent registers a component handler for the given component ID.

func (*Router) AddComponentFunc added in v3.3.3

func (r *Router) AddComponentFunc(id string, f ComponentHandlerFunc)

AddComponentFunc is a convenience function that calls Handle with a ComponentHandlerFunc.

func (*Router) AddFunc

func (r *Router) AddFunc(name string, f CommandHandlerFunc)

AddFunc is a convenience function that calls Handle with a CommandHandlerFunc.

func (*Router) Group added in v3.3.5

func (r *Router) Group(f func(r *Router))

Group creates a subrouter that handles certain commands within the parent command. This is useful for assigning middlewares to a group of commands that belong to the same parent command.

For example, consider the following:

r := cmdroute.NewRouter()
r.Group(func(r *cmdroute.Router) {
	r.Use(cmdroute.Deferrable(client, cmdroute.DeferOpts{}))
	r.Add("foo", handleFoo)
})
r.Add("bar", handleBar)

In this example, the middleware is only applied to the "foo" command, and not the "bar" command.

func (*Router) HandleAutocompletion deprecated

HandleAutocompletion handles an autocompletion event.

Deprecated: This function should not be used directly. Use HandleInteraction instead.

func (*Router) HandleCommand deprecated

HandleCommand implements CommandHandler. It applies middlewares onto the handler to be executed.

Deprecated: This function should not be used directly. Use HandleInteraction instead.

func (*Router) HandleInteraction

func (r *Router) HandleInteraction(ev *discord.InteractionEvent) *api.InteractionResponse

HandleInteraction implements webhook.InteractionHandler. It only handles events of type CommandInteraction, otherwise nil is returned.

func (*Router) Sub

func (r *Router) Sub(name string, f func(r *Router))

Sub creates a subrouter that handles all subcommands that are under the parent command of the given name.

func (*Router) Use

func (r *Router) Use(mws ...Middleware)

Use adds a middleware to the router. The middleware is applied to all subcommands and subrouters. Middlewares are applied in the order they are added, with the middlewares in the parent router being applied first.

func (*Router) With added in v3.3.5

func (r *Router) With(mws ...Middleware) *Router

With is similar to Group, but it returns a new router instead of calling a function with a new router. This is useful for chaining middlewares once, such as:

r := cmdroute.NewRouter()
r.With(cmdroute.Deferrable(client, cmdroute.DeferOpts{})).Add("foo", handleFoo)

Jump to

Keyboard shortcuts

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