handler

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package handler is a provides a command handler in the style of an HTTP router. It is inspired by the github.com/go-chi/chi package.

The handler allows mapping command names (e.g. "/ping" or "/info/commands") and component custom IDs (e.g. "/button1" or "/menu/close/{id}") to handlers. Paths can contain variables (e.g. "/user/{id}") but need to be a full path segment. The variables can be accessed via the Vars field in the event.

Middlewares can be used to intercept and short-circuit the interactions. One thing to be aware of is that middlewares are executed while the tree is being traversed. This means you can't access all path variables in a middleware, only the ones that have been matched so far in the path.

The handler also supports sub-routers, which can be used to group handlers and middlewares. e.g.:

r := handler.New()

r.Route("/info", func(r handler.Router) {
	r.Use(someMiddleware)
	r.SlashCommand("/commands", someCommandHandler)
})

r.Group(func(r handler.Router) {
	r.Use(someMiddleware2)
	r.SlashCommand("/options", someCommandHandler2)
})

To register a handler, you can use the following methods: - [Router.Interaction]: for any interaction type handlers

- [Router.Command]: for application command handlers - [Router.SlashCommand]: for slash command handlers - [Router.UserCommand]: for user command handlers - [Router.MessageCommand]: for message command handlers - [Router.EntryPointCommand]: for entry point command handlers - [Router.Autocomplete]: for autocomplete command handlers

- [Router.Component]: for component handlers - [Router.ButtonComponent]: for button component handlers - [Router.SelectMenuComponent]: for select menu component handlers

- [Router.Modal]: for modal handlers

To register a middleware, you can use the following methods: - [Router.Use]: to add a middleware to the current router - [Router.With]: to create a new router with the given middlewares

To create a sub-router, you can use the following methods: - [Router.Group]: to create a new router and add it to the current router - [Router.Route]: to create a new sub-router with the given pattern and add it to the current router - [Router.Mount]: to mount the given router with the given pattern to the current router

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SyncCommands

func SyncCommands(client *bot.Client, commands []discord.ApplicationCommandCreate, guildIDs []snowflake.ID, opts ...rest.RequestOpt) error

SyncCommands sets the given commands for the given guilds or globally if no guildIDs are empty. It will return on the first error for multiple guilds.

Types

type AutocompleteEvent

type AutocompleteEvent struct {
	*events.AutocompleteInteractionCreate
	Vars map[string]string
	Ctx  context.Context
}

AutocompleteEvent allows to handle autocomplete interactions.

func (*AutocompleteEvent) CreateFollowupMessage

func (e *AutocompleteEvent) CreateFollowupMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*AutocompleteEvent) DeleteFollowupMessage

func (e *AutocompleteEvent) DeleteFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) error

func (*AutocompleteEvent) GetFollowupMessage

func (e *AutocompleteEvent) GetFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error)

func (*AutocompleteEvent) UpdateFollowupMessage

func (e *AutocompleteEvent) UpdateFollowupMessage(messageID snowflake.ID, messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

type AutocompleteHandler

type AutocompleteHandler func(e *AutocompleteEvent) error

AutocompleteHandler is a function that handles autocomplete interactions.

type ButtonComponentHandler

type ButtonComponentHandler func(data discord.ButtonInteractionData, e *ComponentEvent) error

ButtonComponentHandler is a function that handles button component interactions.

type CommandEvent

type CommandEvent struct {
	*events.ApplicationCommandInteractionCreate
	Vars map[string]string
	Ctx  context.Context
}

CommandEvent allows to handle all types of application command interactions.

func (*CommandEvent) CreateFollowupMessage

func (e *CommandEvent) CreateFollowupMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*CommandEvent) DeleteFollowupMessage

func (e *CommandEvent) DeleteFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) error

func (*CommandEvent) DeleteInteractionResponse

func (e *CommandEvent) DeleteInteractionResponse(opts ...rest.RequestOpt) error

func (*CommandEvent) GetFollowupMessage

func (e *CommandEvent) GetFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error)

func (*CommandEvent) GetInteractionResponse

func (e *CommandEvent) GetInteractionResponse(opts ...rest.RequestOpt) (*discord.Message, error)

func (*CommandEvent) UpdateFollowupMessage

func (e *CommandEvent) UpdateFollowupMessage(messageID snowflake.ID, messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*CommandEvent) UpdateInteractionResponse

func (e *CommandEvent) UpdateInteractionResponse(messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

type CommandHandler

type CommandHandler func(e *CommandEvent) error

CommandHandler is a function that handles all types of application command interactions.

type ComponentEvent

type ComponentEvent struct {
	*events.ComponentInteractionCreate
	Vars map[string]string
	Ctx  context.Context
}

ComponentEvent allows to handle all types of component interactions.

func (*ComponentEvent) CreateFollowupMessage

func (e *ComponentEvent) CreateFollowupMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*ComponentEvent) DeleteFollowupMessage

func (e *ComponentEvent) DeleteFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) error

func (*ComponentEvent) DeleteInteractionResponse

func (e *ComponentEvent) DeleteInteractionResponse(opts ...rest.RequestOpt) error

func (*ComponentEvent) GetFollowupMessage

func (e *ComponentEvent) GetFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error)

func (*ComponentEvent) GetInteractionResponse

func (e *ComponentEvent) GetInteractionResponse(opts ...rest.RequestOpt) (*discord.Message, error)

func (*ComponentEvent) UpdateFollowupMessage

func (e *ComponentEvent) UpdateFollowupMessage(messageID snowflake.ID, messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*ComponentEvent) UpdateInteractionResponse

func (e *ComponentEvent) UpdateInteractionResponse(messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

type ComponentHandler

type ComponentHandler func(e *ComponentEvent) error

ComponentHandler is a function that handles all types of component interactions.

type EntryPointCommandHandler

type EntryPointCommandHandler func(data discord.EntryPointCommandInteractionData, e *CommandEvent) error

EntryPointCommandHandler is a function that handles entry point command interactions.

type ErrorHandler

type ErrorHandler func(e *InteractionEvent, err error)

ErrorHandler is a function that is called when an error occurs during handling an interaction.

type Handler

type Handler func(e *InteractionEvent) error

Handler is a function that handles an interaction event.

type InteractionEvent

type InteractionEvent struct {
	*events.InteractionCreate
	Vars map[string]string
	Ctx  context.Context
}

InteractionEvent allows to handle all types of interactions.

func (*InteractionEvent) AutocompleteResult

func (e *InteractionEvent) AutocompleteResult(choices []discord.AutocompleteChoice, opts ...rest.RequestOpt) error

func (*InteractionEvent) CreateFollowupMessage

func (e *InteractionEvent) CreateFollowupMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*InteractionEvent) CreateMessage

func (e *InteractionEvent) CreateMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) error

CreateMessage responds to the interaction with a new message.

func (*InteractionEvent) DeferCreateMessage

func (e *InteractionEvent) DeferCreateMessage(ephemeral bool, opts ...rest.RequestOpt) error

DeferCreateMessage responds to the interaction with a "bot is thinking..." message which should be edited later.

func (*InteractionEvent) DeferUpdateMessage

func (e *InteractionEvent) DeferUpdateMessage(opts ...rest.RequestOpt) error

DeferUpdateMessage responds to the interaction with nothing.

func (*InteractionEvent) DeleteFollowupMessage

func (e *InteractionEvent) DeleteFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) error

func (*InteractionEvent) DeleteInteractionResponse

func (e *InteractionEvent) DeleteInteractionResponse(opts ...rest.RequestOpt) error

func (*InteractionEvent) DeleteReply added in v1.0.2

func (e *InteractionEvent) DeleteReply(opts ...rest.RequestOpt) error

DeleteReply deletes the original interaction response message.

func (*InteractionEvent) FollowUp added in v1.0.2

func (e *InteractionEvent) FollowUp(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) (*discord.Message, error)

FollowUp creates a follow-up message for the interaction.

func (*InteractionEvent) GetFollowupMessage

func (e *InteractionEvent) GetFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error)

func (*InteractionEvent) GetInteractionResponse

func (e *InteractionEvent) GetInteractionResponse(opts ...rest.RequestOpt) (*discord.Message, error)

func (*InteractionEvent) LaunchActivity

func (e *InteractionEvent) LaunchActivity(opts ...rest.RequestOpt) error

LaunchActivity responds to the interaction by launching activity associated with the app.

func (*InteractionEvent) Modal

func (e *InteractionEvent) Modal(modalCreate discord.ModalCreate, opts ...rest.RequestOpt) error

Modal responds to the interaction with a new modal.

func (*InteractionEvent) UpdateFollowUp added in v1.0.2

func (e *InteractionEvent) UpdateFollowUp(messageID snowflake.ID, messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

UpdateFollowUp updates a follow-up message for the interaction.

func (*InteractionEvent) UpdateFollowupMessage

func (e *InteractionEvent) UpdateFollowupMessage(messageID snowflake.ID, messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*InteractionEvent) UpdateInteractionResponse

func (e *InteractionEvent) UpdateInteractionResponse(messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*InteractionEvent) UpdateMessage

func (e *InteractionEvent) UpdateMessage(messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) error

UpdateMessage responds to the interaction with updating the message the component is from.

func (*InteractionEvent) UpdateReply added in v1.0.2

func (e *InteractionEvent) UpdateReply(messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

UpdateReply updates the original interaction response message.

type InteractionHandler

type InteractionHandler func(e *InteractionEvent) error

InteractionHandler is a function that handles all types of interactions.

type MessageCommandHandler

type MessageCommandHandler func(data discord.MessageCommandInteractionData, e *CommandEvent) error

MessageCommandHandler is a function that handles message command interactions.

type Middleware

type Middleware func(next Handler) Handler

Middleware is a function that wraps a handler to intercept and short-circuit the interactions.

type Middlewares

type Middlewares []Middleware

Middlewares is a list of middlewares.

type ModalEvent

type ModalEvent struct {
	*events.ModalSubmitInteractionCreate
	Vars map[string]string
	Ctx  context.Context
}

ModalEvent allows to handle modal interactions.

func (*ModalEvent) CreateFollowupMessage

func (e *ModalEvent) CreateFollowupMessage(messageCreate discord.MessageCreate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*ModalEvent) DeleteFollowupMessage

func (e *ModalEvent) DeleteFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) error

func (*ModalEvent) DeleteInteractionResponse

func (e *ModalEvent) DeleteInteractionResponse(opts ...rest.RequestOpt) error

func (*ModalEvent) GetFollowupMessage

func (e *ModalEvent) GetFollowupMessage(messageID snowflake.ID, opts ...rest.RequestOpt) (*discord.Message, error)

func (*ModalEvent) GetInteractionResponse

func (e *ModalEvent) GetInteractionResponse(opts ...rest.RequestOpt) (*discord.Message, error)

func (*ModalEvent) UpdateFollowupMessage

func (e *ModalEvent) UpdateFollowupMessage(messageID snowflake.ID, messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

func (*ModalEvent) UpdateInteractionResponse

func (e *ModalEvent) UpdateInteractionResponse(messageUpdate discord.MessageUpdate, opts ...rest.RequestOpt) (*discord.Message, error)

type ModalHandler

type ModalHandler func(e *ModalEvent) error

ModalHandler is a function that handles modals.

type Mux

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

Mux is a basic Router implementation.

func New

func New() *Mux

New returns a new Router.

func (*Mux) Autocomplete

func (r *Mux) Autocomplete(pattern string, h AutocompleteHandler)

Autocomplete registers the given AutocompleteHandler to the current Router.

func (*Mux) ButtonComponent

func (r *Mux) ButtonComponent(pattern string, h ButtonComponentHandler)

ButtonComponent registers the given ButtonComponentHandler to the current Router.

func (*Mux) Command

func (r *Mux) Command(pattern string, h CommandHandler)

Command registers the given CommandHandler to the current Router.

func (*Mux) Component

func (r *Mux) Component(pattern string, h ComponentHandler)

Component registers the given ComponentHandler to the current Router.

func (*Mux) DefaultContext

func (r *Mux) DefaultContext(ctx func() context.Context)

DefaultContext sets the default context for this router. This context will be used for all interaction events.

func (*Mux) EntryPointCommand

func (r *Mux) EntryPointCommand(pattern string, h EntryPointCommandHandler)

EntryPointCommand registers the given EntryPointCommandHandler to the current Router.

func (*Mux) Error

func (r *Mux) Error(h ErrorHandler)

Error sets the ErrorHandler for this router. This handler only works for the root router and will be ignored for sub routers.

func (*Mux) Group

func (r *Mux) Group(fn func(router Router))

Group creates a new Router and adds it to the current Router.

func (*Mux) Handle

func (r *Mux) Handle(path string, event *InteractionEvent) error

Handle handles the given interaction event.

func (*Mux) Interaction

func (r *Mux) Interaction(pattern string, h InteractionHandler)

Interaction registers the given InteractionHandler to the current Router. This is a shortcut for Command, Autocomplete, Component and Modal.

func (*Mux) Match

func (r *Mux) Match(path string, t discord.InteractionType, t2 int) bool

Match returns true if the given path matches the Route.

func (*Mux) MessageCommand

func (r *Mux) MessageCommand(pattern string, h MessageCommandHandler)

MessageCommand registers the given MessageCommandHandler to the current Router.

func (*Mux) Modal

func (r *Mux) Modal(pattern string, h ModalHandler)

Modal registers the given ModalHandler to the current Router.

func (*Mux) Mount

func (r *Mux) Mount(pattern string, router Router)

Mount mounts the given router with the given pattern to the current Router.

func (*Mux) NotFound

func (r *Mux) NotFound(h NotFoundHandler)

NotFound sets the NotFoundHandler for this router. This handler only works for the root router and will be ignored for sub routers.

func (*Mux) OnEvent

func (r *Mux) OnEvent(event bot.Event)

OnEvent is called when a new event is received.

func (*Mux) Route

func (r *Mux) Route(pattern string, fn func(r Router)) Router

Route creates a new sub-router with the given pattern and adds it to the current Router.

func (*Mux) SelectMenuComponent

func (r *Mux) SelectMenuComponent(pattern string, h SelectMenuComponentHandler)

SelectMenuComponent registers the given SelectMenuComponentHandler to the current Router.

func (*Mux) SlashCommand

func (r *Mux) SlashCommand(pattern string, h SlashCommandHandler)

SlashCommand registers the given SlashCommandHandler to the current Router.

func (*Mux) Use

func (r *Mux) Use(middlewares ...Middleware)

Use adds the given middlewares to the current Router.

func (*Mux) UserCommand

func (r *Mux) UserCommand(pattern string, h UserCommandHandler)

UserCommand registers the given UserCommandHandler to the current Router.

func (*Mux) With

func (r *Mux) With(middlewares ...Middleware) Router

With returns a new Router with the given middlewares.

type NotFoundHandler

type NotFoundHandler func(e *InteractionEvent) error

NotFoundHandler is a function that is called when no route was found.

type Route

type Route interface {
	// Match returns true if the given path matches the Route.
	Match(path string, t discord.InteractionType, t2 int) bool

	// Handle handles the given interaction event.
	Handle(path string, e *InteractionEvent) error
}

Route is a basic interface for a route in a Router.

type Router

type Router interface {
	bot.EventListener
	Route

	// Use adds the given middlewares to the current Router.
	Use(middlewares ...Middleware)

	// With returns a new Router with the given middlewares.
	With(middlewares ...Middleware) Router

	// Group creates a new Router and adds it to the current Router.
	Group(fn func(r Router))

	// Route creates a new sub-router with the given pattern and adds it to the current Router.
	Route(pattern string, fn func(r Router)) Router

	// Mount mounts the given router with the given pattern to the current Router.
	Mount(pattern string, r Router)

	// Interaction registers the given InteractionHandler to the current Router.
	Interaction(pattern string, h InteractionHandler)

	// Command registers the given CommandHandler to the current Router.
	Command(pattern string, h CommandHandler)

	// SlashCommand registers the given SlashCommandHandler to the current Router.
	SlashCommand(pattern string, h SlashCommandHandler)

	// UserCommand registers the given UserCommandHandler to the current Router.
	UserCommand(pattern string, h UserCommandHandler)

	// MessageCommand registers the given MessageCommandHandler to the current Router.
	MessageCommand(pattern string, h MessageCommandHandler)

	// EntryPointCommand registers the given EntryPointCommandHandler to the current Router.
	EntryPointCommand(pattern string, h EntryPointCommandHandler)

	// Autocomplete registers the given AutocompleteHandler to the current Router.
	Autocomplete(pattern string, h AutocompleteHandler)

	// Component registers the given ComponentHandler to the current Router.
	Component(pattern string, h ComponentHandler)

	// ButtonComponent registers the given ButtonComponentHandler to the current Router.
	ButtonComponent(pattern string, h ButtonComponentHandler)

	// SelectMenuComponent registers the given SelectMenuComponentHandler to the current Router.
	SelectMenuComponent(pattern string, h SelectMenuComponentHandler)

	// Modal registers the given ModalHandler to the current Router.
	Modal(pattern string, h ModalHandler)
}

Router provides with the core routing functionality. It is used to register handlers and middlewares and sub-routers.

type SelectMenuComponentHandler

type SelectMenuComponentHandler func(data discord.SelectMenuInteractionData, e *ComponentEvent) error

SelectMenuComponentHandler is a function that handles select menu component interactions.

type SlashCommandHandler

type SlashCommandHandler func(data discord.SlashCommandInteractionData, e *CommandEvent) error

SlashCommandHandler is a function that handles slash command interactions.

type UserCommandHandler

type UserCommandHandler func(data discord.UserCommandInteractionData, e *CommandEvent) error

UserCommandHandler is a function that handles user command interactions.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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