cmdhandler

package
v16.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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 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
	// 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) 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

func (*EmbedResponse) SetColor

func (r *EmbedResponse) SetColor(color int)

SetColor sets the side color of the embed box

func (*EmbedResponse) SetReplyTo added in v16.1.0

func (r *EmbedResponse) SetReplyTo(m Message)

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 JSONMarshaler

type JSONMarshaler interface {
	MarshalJSON() ([]byte, error)
}

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

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 added in v16.1.0

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)
	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
	// 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) 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

func (*SimpleEmbedResponse) SetColor

func (r *SimpleEmbedResponse) SetColor(color int)

SetColor sets the side color of the embed box

func (*SimpleEmbedResponse) SetReplyTo added in v16.1.0

func (r *SimpleEmbedResponse) SetReplyTo(m Message)

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
	// 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) 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

func (*SimpleResponse) SetColor

func (r *SimpleResponse) SetColor(color int)

SetColor is included for the Response API but is a no-op

func (*SimpleResponse) SetReplyTo added in v16.1.0

func (r *SimpleResponse) SetReplyTo(m Message)

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