Documentation ¶
Overview ¶
Package cmdhandler deals with handling bot commands
Index ¶
- Variables
- func ChannelMentionString(cid snowflake.Snowflake) string
- func ForceUserAccountMention(v string) (string, error)
- func ForceUserNicknameMention(v string) (string, error)
- func IsChannelMention(v string) bool
- func IsRoleMention(v string) bool
- func IsUserMention(v string) bool
- func RoleMentionString(rid snowflake.Snowflake) string
- func UserMentionString(uid snowflake.Snowflake) string
- type AutocompleteHandler
- type AutocompleteHandlerFunc
- type CommandHandler
- type EmbedField
- type EmbedResponse
- func (r *EmbedResponse) Channel() snowflake.Snowflake
- func (r *EmbedResponse) GetColor() int
- func (r *EmbedResponse) HasErrors() bool
- func (r *EmbedResponse) IncludeError(err error)
- func (r *EmbedResponse) MessageReactions() []string
- func (r *EmbedResponse) SetColor(color int)
- func (r *EmbedResponse) SetEphemeral(e bool)
- func (r *EmbedResponse) SetReplyTo(m Message)
- func (r *EmbedResponse) Split() []Response
- func (r *EmbedResponse) ToMessage() JSONMarshaler
- func (r *EmbedResponse) ToString() string
- type Interaction
- type InteractionCommandHandler
- type InteractionDispatcher
- func (i *InteractionDispatcher) Autocomplete(ix *Interaction) ([]entity.ApplicationCommandOptionChoice, error)
- func (i *InteractionDispatcher) Dispatch(ix *Interaction) (Response, []Response, error)
- func (i *InteractionDispatcher) GlobalCommands() []entity.ApplicationCommand
- func (i *InteractionDispatcher) GuildCommands() map[snowflake.Snowflake][]entity.ApplicationCommand
- func (i *InteractionDispatcher) LearnGlobalCommands(cmds []InteractionCommandHandler) error
- func (i *InteractionDispatcher) LearnGuildCommands(gid snowflake.Snowflake, cmds []InteractionCommandHandler) error
- type InteractionHandler
- type InteractionHandlerFunc
- type JSONMarshaler
- type Message
- func NewSimpleMessage(ctx context.Context, userID, guildID, channelID, messageID snowflake.Snowflake, ...) Message
- func NewWithContents(m Message, contents string) Message
- func NewWithContext(ctx context.Context, m Message) Message
- func NewWithTokens(m Message, tokens []string, contentErr error) Message
- type MessageHandler
- type MessageHandlerFunc
- type Options
- type ReplyTo
- type Response
- type SimpleEmbedResponse
- func (r *SimpleEmbedResponse) Channel() snowflake.Snowflake
- func (r *SimpleEmbedResponse) GetColor() int
- func (r *SimpleEmbedResponse) HasErrors() bool
- func (r *SimpleEmbedResponse) IncludeError(err error)
- func (r *SimpleEmbedResponse) MessageReactions() []string
- func (r *SimpleEmbedResponse) SetColor(color int)
- func (r *SimpleEmbedResponse) SetEphemeral(e bool)
- func (r *SimpleEmbedResponse) SetReplyTo(m Message)
- func (r *SimpleEmbedResponse) Split() []Response
- func (r *SimpleEmbedResponse) ToMessage() JSONMarshaler
- func (r *SimpleEmbedResponse) ToString() string
- type SimpleResponse
- func (r *SimpleResponse) Channel() snowflake.Snowflake
- func (r *SimpleResponse) GetColor() int
- func (r *SimpleResponse) HasErrors() bool
- func (r *SimpleResponse) IncludeError(err error)
- func (r *SimpleResponse) MessageReactions() []string
- func (r *SimpleResponse) SetColor(color int)
- func (r *SimpleResponse) SetEphemeral(e bool)
- func (r *SimpleResponse) SetReplyTo(m Message)
- func (r *SimpleResponse) Split() []Response
- func (r *SimpleResponse) ToMessage() JSONMarshaler
- func (r *SimpleResponse) ToString() string
Constants ¶
This section is empty.
Variables ¶
var ErrMalformedInteraction = errors.New("malformed interaction payload")
ErrMalformedInteraction is returned when an interaction payload is malformed
var ErrMissingHandler = errors.New("missing handler for command")
ErrMissingHandler is the error thrown when an event handler cannot be found
var ErrNotUserMention = errors.New("not a user mention")
ErrNotUserMention is the error returned when a user mention string is required but not provided
Functions ¶
func ChannelMentionString ¶
ChannelMentionString generates a string that discord interprets as a mention of a channel
func ForceUserAccountMention ¶
ForceUserAccountMention converts a user mention into an account mention (if it is not already an account mention)
func ForceUserNicknameMention ¶
ForceUserNicknameMention converts a user mention into a nickname mention (if it is not already a nickname mention)
func IsChannelMention ¶
IsChannelMention determines if a string is a mention of a channel
func IsRoleMention ¶
IsRoleMention determines if a string is a mention of a server role
func IsUserMention ¶
IsUserMention determines if a string is a mention of a user (either by nickname or account name)
func RoleMentionString ¶
RoleMentionString generates a string that discord interprets as a mention of a server role
func UserMentionString ¶
UserMentionString generates a string that discord interprets as a mention of a user by their server nickname
Types ¶
type AutocompleteHandler ¶
type AutocompleteHandler interface {
Autocomplete(*Interaction) ([]entity.ApplicationCommandOptionChoice, error)
}
AutocompleteHandler is the api of an autocomplete handler
func NewAutocompleteHandler ¶
func NewAutocompleteHandler(f AutocompleteHandlerFunc) AutocompleteHandler
NewAutocompleteHandler wraps an AutocompleteHandlerFunc into a AutocompleteHandler
type AutocompleteHandlerFunc ¶
type AutocompleteHandlerFunc func(*Interaction) ([]entity.ApplicationCommandOptionChoice, error)
AutocompleteHandlerFunc is the api of a function that handles autocomplete requests
type CommandHandler ¶
type CommandHandler struct {
// contains filtered or unexported fields
}
CommandHandler is a dispatcher for commands
func NewCommandHandler ¶
func NewCommandHandler(p parser.Parser, opts Options) (*CommandHandler, error)
NewCommandHandler creates a new CommandHandler from the given parser
NOTE: the parser's settings must match the Options.CaseSensitive value
func (*CommandHandler) CommandIndicator ¶
func (ch *CommandHandler) CommandIndicator() string
CommandIndicator returns the string prefix required for commands
func (*CommandHandler) HandleMessage ¶
func (ch *CommandHandler) HandleMessage(msg Message) (Response, error)
HandleMessage dispatches a Message to the relevant handler
func (*CommandHandler) SetHandler ¶
func (ch *CommandHandler) SetHandler(cmd string, handler MessageHandler)
SetHandler adds a handler function for the given command, overwriting any previously set ones
type EmbedField ¶
EmbedField is part of an EmbedResponse that represents an embed field
type EmbedResponse ¶
type EmbedResponse struct { To string Title string Description string Color int Fields []EmbedField ToChannel snowflake.Snowflake Reactions []string ReplyTo *ReplyTo Ephemeral bool // contains filtered or unexported fields }
EmbedResponse is a Response that is intended to present text in an discord embed box, including embed fields
func (*EmbedResponse) Channel ¶
func (r *EmbedResponse) Channel() snowflake.Snowflake
Channel returns the ToChannel value
func (*EmbedResponse) GetColor ¶
func (r *EmbedResponse) GetColor() int
GetColor is included for the Response API but always returns 0
func (*EmbedResponse) HasErrors ¶
func (r *EmbedResponse) HasErrors() bool
HasErrors returns whether or not the response already includes errors
func (*EmbedResponse) IncludeError ¶
func (r *EmbedResponse) IncludeError(err error)
IncludeError adds an error into the response
func (*EmbedResponse) MessageReactions ¶
func (r *EmbedResponse) MessageReactions() []string
MessageReactions returns the set of reactions for the response
func (*EmbedResponse) SetColor ¶
func (r *EmbedResponse) SetColor(color int)
SetColor sets the side color of the embed box
func (*EmbedResponse) SetEphemeral ¶
func (r *EmbedResponse) SetEphemeral(e bool)
SetEphemeral sets a flag that indicates the message is ephemeral for interactions
func (*EmbedResponse) SetReplyTo ¶
func (r *EmbedResponse) SetReplyTo(m Message)
SetReplyTo sets the ReplyTo parameters
func (*EmbedResponse) Split ¶
func (r *EmbedResponse) Split() []Response
Split separates the current response into possibly-several to account for response length limits
func (*EmbedResponse) ToMessage ¶
func (r *EmbedResponse) ToMessage() JSONMarshaler
ToMessage generates an object that can be marshaled as json and sent to the discord http API
func (*EmbedResponse) ToString ¶
func (r *EmbedResponse) ToString() string
ToString generates a plain-text representation of the response
type Interaction ¶
type Interaction struct { entity.Interaction Ctx context.Context }
Interaction represents an interaction request and its context
func (*Interaction) ChannelID ¶
func (ix *Interaction) ChannelID() snowflake.Snowflake
ChannelID returns the interaction channel ID
func (*Interaction) Context ¶
func (ix *Interaction) Context() context.Context
Context returns the interaction request context
func (*Interaction) GuildID ¶
func (ix *Interaction) GuildID() snowflake.Snowflake
GuildID returns the interaction guild ID
func (*Interaction) MessageID ¶
func (ix *Interaction) MessageID() snowflake.Snowflake
MessageID returns the interaction ID
func (*Interaction) UserID ¶
func (ix *Interaction) UserID() snowflake.Snowflake
UserID returns the interaction user ID
type InteractionCommandHandler ¶
type InteractionCommandHandler interface { Command() entity.ApplicationCommand Handler() InteractionHandler AutocompleteHandler() AutocompleteHandler }
InteractionCommandHandler is the interface for an interaction handler
type InteractionDispatcher ¶
type InteractionDispatcher struct {
// contains filtered or unexported fields
}
InteractionDispatcher is responsible for dispatching interaction requests to handlers
func NewInteractionDispatcher ¶
func NewInteractionDispatcher(globals []InteractionCommandHandler) (*InteractionDispatcher, error)
NewInteractionDispatcher creates a new interaction dispatcher
func (*InteractionDispatcher) Autocomplete ¶
func (i *InteractionDispatcher) Autocomplete(ix *Interaction) ([]entity.ApplicationCommandOptionChoice, error)
Autocomplete returns autocomplete information for the command
func (*InteractionDispatcher) Dispatch ¶
func (i *InteractionDispatcher) Dispatch(ix *Interaction) (Response, []Response, error)
Dispatch sends the interaction to the appropriate dispatcher
func (*InteractionDispatcher) GlobalCommands ¶
func (i *InteractionDispatcher) GlobalCommands() []entity.ApplicationCommand
GlobalCommands returns the global commands for the InteractionDispatcher
func (*InteractionDispatcher) GuildCommands ¶
func (i *InteractionDispatcher) GuildCommands() map[snowflake.Snowflake][]entity.ApplicationCommand
GuildCommands returns the guild commands for the InteractionDispatcher
func (*InteractionDispatcher) LearnGlobalCommands ¶
func (i *InteractionDispatcher) LearnGlobalCommands(cmds []InteractionCommandHandler) error
LearnGlobalCommands inserts the povided global commands into the dispatch table
func (*InteractionDispatcher) LearnGuildCommands ¶
func (i *InteractionDispatcher) LearnGuildCommands(gid snowflake.Snowflake, cmds []InteractionCommandHandler) error
LearnGuildCommands inserts the provided guild commands into the dispatch table
type InteractionHandler ¶
type InteractionHandler interface {
HandleInteraction(*Interaction) (Response, []Response, error)
}
InteractionHandler is the api of a message handler
func NewInteractionHandler ¶
func NewInteractionHandler(f InteractionHandlerFunc) InteractionHandler
NewInteractionHandler wraps a InteractionHandlerFunc into a InteractionHandler
type InteractionHandlerFunc ¶
type InteractionHandlerFunc func(*Interaction) (Response, []Response, error)
InteractionHandlerFunc is the api of a function that handles messages
type JSONMarshaler ¶
type JSONMarshaler interface {
MarshalToJSON() ([]byte, error) // yes, this is intentionally different than stdlib
}
JSONMarshaler is the interface implemented by types that can marshal themselves into valid JSON.
type Message ¶
type Message interface { Context() context.Context UserID() snowflake.Snowflake GuildID() snowflake.Snowflake ChannelID() snowflake.Snowflake MessageID() snowflake.Snowflake Contents() []string ContentErr() error }
Message is the api for a message that a command handler will respond to
func NewSimpleMessage ¶
func NewSimpleMessage(ctx context.Context, userID, guildID, channelID, messageID snowflake.Snowflake, contents string) Message
NewSimpleMessage creates a new Message object
func NewWithContents ¶
NewWithContents clones a given message object but substitutes the Contents() with the provided string
func NewWithContext ¶
NewWithContext duplicates a message using the provided context
type MessageHandler ¶
MessageHandler is the api of a message handler
func NewMessageHandler ¶
func NewMessageHandler(f MessageHandlerFunc) MessageHandler
NewMessageHandler wraps a MessageHandlerFunc into a MessageHandler
type MessageHandlerFunc ¶
MessageHandlerFunc is the api of a function that handles messages
type Options ¶
type Options struct { Placeholder string PreCommand string NoHelpOnUnknownCommands bool HelpOnEmptyCommands bool CaseSensitive bool }
Options provides a way to specify configurable values when creating a CommandHandler
- Placeholder is the string to be used to represent the "command" - PreCommand is a string representing the state of commands prior to this one - NoHelpOnUnknownCommands can be set to true to NOT display a help message when a command isn't known - HelpOnEmptyCommands can be set to true to display a help message when no command is provided - CaseSensitive can se set to true to make command recognition case-sensitive
type ReplyTo ¶
type ReplyTo struct { MessageID snowflake.Snowflake ChannelID snowflake.Snowflake GuildID snowflake.Snowflake }
ReplyTo is the information required to create a message as a reply
type Response ¶
type Response interface { SetColor(int) SetEphemeral(bool) GetColor() int IncludeError(err error) HasErrors() bool ToString() string ToMessage() JSONMarshaler Channel() snowflake.Snowflake Split() []Response MessageReactions() []string }
Response is the interface that should be returned from a command handler
type SimpleEmbedResponse ¶
type SimpleEmbedResponse struct { To string Title string Description string Color int ToChannel snowflake.Snowflake Reactions []string ReplyTo *ReplyTo Ephemeral bool // contains filtered or unexported fields }
SimpleEmbedResponse is a Response that is intended to present text in an discord embed box but not include any embed fields
func (*SimpleEmbedResponse) Channel ¶
func (r *SimpleEmbedResponse) Channel() snowflake.Snowflake
Channel returns the ToChannel value
func (*SimpleEmbedResponse) GetColor ¶
func (r *SimpleEmbedResponse) GetColor() int
GetColor returns the currently set color of the embed box
func (*SimpleEmbedResponse) HasErrors ¶
func (r *SimpleEmbedResponse) HasErrors() bool
HasErrors returns whether or not the response already includes errors
func (*SimpleEmbedResponse) IncludeError ¶
func (r *SimpleEmbedResponse) IncludeError(err error)
IncludeError adds an error into the response
func (*SimpleEmbedResponse) MessageReactions ¶
func (r *SimpleEmbedResponse) MessageReactions() []string
MessageReactions returns the set of reactions for the response
func (*SimpleEmbedResponse) SetColor ¶
func (r *SimpleEmbedResponse) SetColor(color int)
SetColor sets the side color of the embed box
func (*SimpleEmbedResponse) SetEphemeral ¶
func (r *SimpleEmbedResponse) SetEphemeral(e bool)
SetEphemeral sets a flag that indicates the message is ephemeral for interactions
func (*SimpleEmbedResponse) SetReplyTo ¶
func (r *SimpleEmbedResponse) SetReplyTo(m Message)
SetReplyTo sets the ReplyTo parameters
func (*SimpleEmbedResponse) Split ¶
func (r *SimpleEmbedResponse) Split() []Response
Split separates the current response into possibly-several to account for response length limits
func (*SimpleEmbedResponse) ToMessage ¶
func (r *SimpleEmbedResponse) ToMessage() JSONMarshaler
ToMessage generates an object that can be marshaled as json and sent to the discord http API
func (*SimpleEmbedResponse) ToString ¶
func (r *SimpleEmbedResponse) ToString() string
ToString generates a plain-text representation of the response
type SimpleResponse ¶
type SimpleResponse struct { To string Content string ToChannel snowflake.Snowflake Reactions []string ReplyTo *ReplyTo Ephemeral bool // contains filtered or unexported fields }
SimpleResponse is a Response that is intended to present plain text
func (*SimpleResponse) Channel ¶
func (r *SimpleResponse) Channel() snowflake.Snowflake
Channel returns the ToChannel value
func (*SimpleResponse) GetColor ¶
func (r *SimpleResponse) GetColor() int
GetColor is included for the Response API but always returns 0
func (*SimpleResponse) HasErrors ¶
func (r *SimpleResponse) HasErrors() bool
HasErrors returns whether or not the response includes errors
func (*SimpleResponse) IncludeError ¶
func (r *SimpleResponse) IncludeError(err error)
IncludeError adds an error into the response
func (*SimpleResponse) MessageReactions ¶
func (r *SimpleResponse) MessageReactions() []string
MessageReactions returns the set of reactions for the response
func (*SimpleResponse) SetColor ¶
func (r *SimpleResponse) SetColor(color int)
SetColor is included for the Response API but is a no-op
func (*SimpleResponse) SetEphemeral ¶
func (r *SimpleResponse) SetEphemeral(e bool)
SetEphemeral sets a flag that indicates the message is ephemeral for interactions
func (*SimpleResponse) SetReplyTo ¶
func (r *SimpleResponse) SetReplyTo(m Message)
SetReplyTo sets the ReplyTo parameters
func (*SimpleResponse) Split ¶
func (r *SimpleResponse) Split() []Response
Split separates the current response into possibly-several to account for response length limits
func (*SimpleResponse) ToMessage ¶
func (r *SimpleResponse) ToMessage() JSONMarshaler
ToMessage generates an object that can be marshaled as json and sent to the discord http API
func (*SimpleResponse) ToString ¶
func (r *SimpleResponse) ToString() string
ToString generates a plain-text representation of the response