Documentation
¶
Overview ¶
All credits go to the pat package for these utilities https://github.com/bmizerany/pat/blob/0e6a57d3996914bbea76de5a2ce30fc1dbe82e9e/mux.go#L287
Index ¶
- Variables
- func Defer(ctx *Ctx, ephemeral ...bool) (err error)
- func ErrorResponse(ctx *Ctx, ephemeral ...bool) error
- func FollowUp(ctx *Ctx, response *discordgo.WebhookParams, ephemeral ...bool) (err error)
- func ForbiddenResponse(ctx *Ctx) error
- func GetInteractionName(data *discordgo.ApplicationCommandInteractionData, delimeter ...string) string
- func InteractionError(ctx *Ctx, isFollowup bool)
- func MessageComponentError(ctx *Ctx)
- func ModalRespond(ctx *Ctx, data *discordgo.InteractionResponseData, ephemeral ...bool) (err error)
- func ParseModalData(modalData discordgo.ModalSubmitInteractionData) map[string]string
- func Respond(ctx *Ctx, data *discordgo.InteractionResponseData, ephemeral ...bool) (err error)
- func Update(ctx *Ctx, data *discordgo.InteractionResponseData) (err error)
- type Bot
- type BulkCommandSyncer
- type Command
- type CommandSyncer
- type Ctx
- type Handler
- type HandlerFunc
- type MessageComponent
- type MessageCtx
- type MessageHandler
- type MessageHandlerConfig
- type MessageHandlerFunc
- type Modal
- type OptionsMap
- type Router
- func (r *Router) CoundModals() (c int)
- func (r *Router) Count() (c int)
- func (r *Router) CountMessageComponents() (c int)
- func (r *Router) Get(name string) *Command
- func (r *Router) GetMessageComponent(customID string) (*MessageComponent, map[string]string)
- func (r *Router) GetModal(customID string) (*Modal, map[string]string)
- func (r *Router) HandleInteraction(s *discordgo.Session, i *discordgo.InteractionCreate)
- func (r *Router) HandleInteractionMessageComponent(s *discordgo.Session, i *discordgo.InteractionCreate)
- func (r *Router) HandleInteractionModalSubmit(s *discordgo.Session, i *discordgo.InteractionCreate)
- func (r *Router) List() (list []*Command)
- func (r *Router) ListMessageComponents() (list []*MessageComponent)
- func (r *Router) ListModals() (list []*Modal)
- func (r *Router) MakeMessageHandler(cfg *MessageHandlerConfig) func(s *discordgo.Session, m *discordgo.MessageCreate)
- func (r *Router) Register(cmd *Command)
- func (r *Router) RegisterMessageComponent(cmpnt *MessageComponent)
- func (r *Router) RegisterModal(cmpnt *Modal)
- func (r *Router) Sync(s *discordgo.Session, application, guild string) error
- func (r *Router) Unregister(name string) (command *Command, existed bool)
- func (r *Router) UnregisterMessageComponent(customID string) (messageComponent *MessageComponent, existed bool)
- func (r *Router) UnregisterModal(customID string) (modal *Modal, existed bool)
- func (r *Router) Update(name string, newcmd *Command) (cmd *Command, err error)
- func (r *Router) UpdateMessageComponent(customID string, newcmpnt *MessageComponent) (cmd *MessageComponent, err error)
- func (r *Router) UpdateModal(customID string, newcmpnt *Modal) (cmd *Modal, err error)
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCommandNotExists means that the requested command does not exist. ErrCommandNotExists = errors.New("command not exists") // ErrMessageComponentNotExists means that the requested message component does not exist. ErrMessageComponentNotExists = errors.New("message component not exists") )
Functions ¶
func ErrorResponse ¶
func FollowUp ¶
func FollowUp( ctx *Ctx, response *discordgo.WebhookParams, ephemeral ...bool, ) (err error)
func ForbiddenResponse ¶
func GetInteractionName ¶
func GetInteractionName( data *discordgo.ApplicationCommandInteractionData, delimeter ...string, ) string
func InteractionError ¶
func MessageComponentError ¶
func MessageComponentError(ctx *Ctx)
func ModalRespond ¶
func ModalRespond( ctx *Ctx, data *discordgo.InteractionResponseData, ephemeral ...bool, ) (err error)
func ParseModalData ¶
func ParseModalData( modalData discordgo.ModalSubmitInteractionData, ) map[string]string
Types ¶
type BulkCommandSyncer ¶
type BulkCommandSyncer struct{}
BulkCommandSyncer syncs all the commands using ApplicationCommandBulkOverwrite function.
type Command ¶
type Command struct {
Name string
Description string
Options []*discordgo.ApplicationCommandOption
Type discordgo.ApplicationCommandType
Handler Handler
Middlewares []Handler
MessageHandler MessageHandler
MessageMiddlewares []MessageHandler
// NOTE: nesting of more than 3 level has no effect
SubCommands *Router
// Custom payload for the command. Useful for module names, and such stuff.
Custom interface{}
}
Command represents a command.
func (Command) ApplicationCommand ¶
func (cmd Command) ApplicationCommand() *discordgo.ApplicationCommand
ApplicationCommand converts Command to discordgo.ApplicationCommand.
func (Command) ApplicationCommandOption ¶
func (cmd Command) ApplicationCommandOption() *discordgo.ApplicationCommandOption
ApplicationCommandOption converts Command to discordgo.ApplicationCommandOption (subcommand).
type CommandSyncer ¶
type CommandSyncer interface {
Sync(r *Router, s *discordgo.Session, application, guild string) error
}
A CommandSyncer syncs all the commands with Discord.
type Ctx ¶
type Ctx struct {
*discordgo.Session `json:"-"`
Caller *Command `json:"caller"`
MessageComponent *MessageComponent `json:"message_component"`
Modal *Modal `json:"modal"`
Interaction *discordgo.Interaction `json:"interaction"`
Options OptionsMap `json:"options"`
OptionsRaw []*discordgo.ApplicationCommandInteractionDataOption `json:"options_raw"`
MessageComponentOptions map[string]string `json:"message_component_options"`
ModalData discordgo.ModalSubmitInteractionData `json:"modal_data"`
ModalOptions map[string]string `json:"modal_options"`
// contains filtered or unexported fields
}
Ctx is a context provided to a command. It embeds session for easier use, and contains interaction and preprocessed options.
func NewCtx ¶
func NewCtx( s *discordgo.Session, caller *Command, i *discordgo.Interaction, parent *discordgo.ApplicationCommandInteractionDataOption, handlers []Handler, ) *Ctx
NewCtx constructs ctx from given parameters.
func NewMessageComponentCtx ¶
func NewMessageComponentCtx( s *discordgo.Session, caller *MessageComponent, i *discordgo.Interaction, options map[string]string, handlers []Handler, ) *Ctx
NewCtx constructs ctx from given parameters.
func NewModalCTX ¶
func NewModalCTX( s *discordgo.Session, caller *Modal, i *discordgo.Interaction, options map[string]string, data discordgo.ModalSubmitInteractionData, handlers []Handler, ) *Ctx
NewCtx constructs ctx from given parameters.
type HandlerFunc ¶
type HandlerFunc func(ctx *Ctx)
HandlerFunc is a wrapper around Handler for functions
func (HandlerFunc) HandleCommand ¶
func (f HandlerFunc) HandleCommand(ctx *Ctx)
HandleCommand implements Handler interface and calls the function with provided context
type MessageComponent ¶
MessageComponent represents a message component
type MessageCtx ¶
type MessageCtx struct {
*discordgo.Session
Caller *Command
Message *discordgo.Message
Arguments []string
// contains filtered or unexported fields
}
MessageCtx is a context provided to message command handler. It contains the message
func NewMessageCtx ¶
func NewMessageCtx( s *discordgo.Session, caller *Command, m *discordgo.Message, arguments []string, handlers []MessageHandler, ) *MessageCtx
NewMessageCtx constructs context from a message. If argdelim is not empty it is a delimiter for the arguments, otherwise the arguments are split by a space.
func (*MessageCtx) Next ¶
func (ctx *MessageCtx) Next()
Next calls the next middleware / command handler.
func (*MessageCtx) Reply ¶
Reply sends and returns a simple (content-only) message replying to the command message. If mention is true the command author is mentioned in the reply. It is a wrapper for discordgo.Session.ChannelMessageSendReply.
func (*MessageCtx) ReplyComplex ¶
func (ctx *MessageCtx) ReplyComplex( message *discordgo.MessageSend, mention bool, ) (*discordgo.Message, error)
ReplyComplex sends and returns a complex (with embds, attachments, etc) message replying to the command message. If mention is true the command author is mentioned in the reply. It is a wrapper for discordgo.Session.ChannelMessageSendComplex
type MessageHandler ¶
type MessageHandler interface {
HandleMessageCommand(ctx *MessageCtx)
}
A MessageHandler processes the message command
type MessageHandlerConfig ¶
type MessageHandlerFunc ¶
type MessageHandlerFunc func(ctx *MessageCtx)
HandlerFunc is a wrapper around MessageHandler for functions
func (MessageHandlerFunc) HandleMessageCommand ¶
func (f MessageHandlerFunc) HandleMessageCommand(ctx *MessageCtx)
HandleCommand implements MessageHandler interface and calls the function with provided context
type OptionsMap ¶
type OptionsMap = map[string]*discordgo.ApplicationCommandInteractionDataOption
OptionsMap is an alias for map of discordgo.ApplicationCommandInteractionDataOption
type Router ¶
type Router struct {
// Commands is a map of registered commands.
// Key is command name. Value is command instance.
//
// NOTE: it is not recommended to use it directly, use Register, Get, Update, Unregister functions instead.
Commands map[string]*Command
// MessageComponents is a map of registered message components
// Key is the components custom ID. Value is the MessageComponent instance.
//
// NOTE: it is not recommended to use it directly, use RegisterMessageComponent, GetMessageComponent, UpdateMessageComponent, UnregisterMessageComponent functions instead.
MessageComponents map[string]*MessageComponent
// Modals is a map of registered modal components
// Key is the components custom ID. Value is the MessageComponent instance.
//
// NOTE: it is not recommended to use it directly, use RegisterMessageComponent, GetMessageComponent, UpdateMessageComponent, UnregisterMessageComponent functions instead.
Modals map[string]*Modal
Syncer CommandSyncer
}
A Router stores all the commands and routes the interactions
func (*Router) CoundModals ¶
CountModals returns amount of modals stored
func (*Router) CountMessageComponents ¶
CountMessageComponents returns amount of message components stored
func (*Router) GetMessageComponent ¶
func (r *Router) GetMessageComponent( customID string, ) (*MessageComponent, map[string]string)
GetMessageComponent returns a message component by specified CustomID. It also checks for params
func (*Router) GetModal ¶
GetModal returns a message component by specified CustomID. It also checks for params
func (*Router) HandleInteraction ¶
func (r *Router) HandleInteraction( s *discordgo.Session, i *discordgo.InteractionCreate, )
HandleInteraction is an interaction handler passed to discordgo.Session.AddHandler.
func (*Router) HandleInteractionMessageComponent ¶
func (r *Router) HandleInteractionMessageComponent( s *discordgo.Session, i *discordgo.InteractionCreate, )
HandleInteractionMessageComponent is an interaction handler passed to discordgo.Session.AddHandler.
func (*Router) HandleInteractionModalSubmit ¶
func (r *Router) HandleInteractionModalSubmit( s *discordgo.Session, i *discordgo.InteractionCreate, )
HandleInteractionModalSubmit is an interaction handler passed to discordgo.Session.AddHandler.
func (*Router) ListMessageComponents ¶
func (r *Router) ListMessageComponents() (list []*MessageComponent)
ListMessageComponents returns all registered message components
func (*Router) ListModals ¶
ListModals returns all registered modals
func (*Router) MakeMessageHandler ¶
func (r *Router) MakeMessageHandler( cfg *MessageHandlerConfig, ) func(s *discordgo.Session, m *discordgo.MessageCreate)
func (*Router) RegisterMessageComponent ¶
func (r *Router) RegisterMessageComponent(cmpnt *MessageComponent)
RegisterMessageComponent registers the message component.
func (*Router) RegisterModal ¶
RegisterMessageComponent registers the message component.
func (*Router) Unregister ¶
Unregister removes a command from router
func (*Router) UnregisterMessageComponent ¶
func (r *Router) UnregisterMessageComponent( customID string, ) (messageComponent *MessageComponent, existed bool)
UnregisterMessageComponent removes a message component from router
func (*Router) UnregisterModal ¶
UnregisterModal removes a modal from router
func (*Router) UpdateMessageComponent ¶
func (r *Router) UpdateMessageComponent( customID string, newcmpnt *MessageComponent, ) (cmd *MessageComponent, err error)
UpdateMessageComponent updates the message component and does all behind-the-scenes work.