cmdhandler

package
v24.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package cmdhandler deals with handling bot commands

Index

Constants

This section is empty.

Variables

View Source
var ErrMalformedInteraction = errors.New("malformed interaction payload")

ErrMalformedInteraction is returned when an interaction payload is malformed

View Source
var ErrMissingHandler = errors.New("missing handler for command")

ErrMissingHandler is the error thrown when an event handler cannot be found

View Source
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

func ChannelMentionString(cid snowflake.Snowflake) string

ChannelMentionString generates a string that discord interprets as a mention of a channel

func ForceUserAccountMention

func ForceUserAccountMention(v string) (string, error)

ForceUserAccountMention converts a user mention into an account mention (if it is not already an account mention)

func ForceUserNicknameMention

func ForceUserNicknameMention(v string) (string, error)

ForceUserNicknameMention converts a user mention into a nickname mention (if it is not already a nickname mention)

func IsChannelMention

func IsChannelMention(v string) bool

IsChannelMention determines if a string is a mention of a channel

func IsRoleMention

func IsRoleMention(v string) bool

IsRoleMention determines if a string is a mention of a server role

func IsUserMention

func IsUserMention(v string) bool

IsUserMention determines if a string is a mention of a user (either by nickname or account name)

func RoleMentionString

func RoleMentionString(rid snowflake.Snowflake) string

RoleMentionString generates a string that discord interprets as a mention of a server role

func UserMentionString

func UserMentionString(uid snowflake.Snowflake) string

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

func (*CommandHandler) SetParser

func (ch *CommandHandler) SetParser(p parser.Parser) error

SetParser sets the parser for the command handler

type EmbedField

type EmbedField struct {
	Name string
	Val  string
}

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
	FooterText  string
	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

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

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

func NewWithContents(m Message, contents string) Message

NewWithContents clones a given message object but substitutes the Contents() with the provided string

func NewWithContext

func NewWithContext(ctx context.Context, m Message) Message

NewWithContext duplicates a message using the provided context

func NewWithTokens

func NewWithTokens(m Message, tokens []string, contentErr error) Message

NewWithTokens clones a given message object but substitutes the Contents() and ContentErr()

type MessageHandler

type MessageHandler interface {
	HandleMessage(Message) (Response, error)
}

MessageHandler is the api of a message handler

func NewMessageHandler

func NewMessageHandler(f MessageHandlerFunc) MessageHandler

NewMessageHandler wraps a MessageHandlerFunc into a MessageHandler

type MessageHandlerFunc

type MessageHandlerFunc func(Message) (Response, error)

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
	FooterText  string
	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

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

Jump to

Keyboard shortcuts

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