Documentation
¶
Index ¶
- Constants
- Variables
- func AsCode(s string) string
- func DefaultValue(s, def string) string
- func EscapeBackticks(s string) string
- func HasAnyPrefix(s string, prefixes ...string) bool
- func HasAnySuffix(s string, suffixes ...string) bool
- func PermStrings(p discord.Permissions) []string
- func SprintfAll(template string, in []string) []string
- func TrimPrefixesSpace(s string, prefixes ...string) string
- func Version() string
- type ArgTransformer
- type Args
- type Command
- type Commands
- type Context
- func (ctx *Context) AddMessageHandler(c discord.ChannelID, user discord.UserID, fn func(*Context, discord.Message))
- func (ctx *Context) AddReactionHandler(msg discord.MessageID, user discord.UserID, reaction string, ...)
- func (ctx *Context) CheckArgRange(min, max int) (err error)
- func (ctx *Context) CheckBotPerms(p discord.Permissions) (err error)
- func (ctx *Context) CheckMinArgs(c int) (err error)
- func (ctx *Context) CheckPerms() (err error)
- func (ctx *Context) CheckRequiredArgs(c int) (err error)
- func (ctx *Context) Edit(m *discord.Message, c string, embed *discord.Embed) (msg *discord.Message, err error)
- func (ctx *Context) GreedyChannelParser(args []string) (channels []*discord.Channel, n int)
- func (ctx *Context) GreedyMemberParser(args []string) (members []*discord.Member, n int)
- func (ctx *Context) GreedyRoleParser(args []string) (roles []*discord.Role, n int)
- func (ctx *Context) GreedyUserParser(args []string) (users []*discord.User, n int)
- func (ctx *Context) Help(path []string) (err error)
- func (ctx *Context) NewDM(user discord.UserID) *MessageSend
- func (ctx *Context) NewMessage(channel ...discord.ChannelID) *MessageSend
- func (ctx *Context) PagedEmbed(embeds []discord.Embed, extendedReactions bool) (msg *discord.Message, err error)
- func (ctx *Context) ParseChannel(s string) (c *discord.Channel, err error)
- func (ctx *Context) ParseMember(s string) (c *discord.Member, err error)
- func (ctx *Context) ParseRole(s string) (c *discord.Role, err error)
- func (ctx *Context) ParseUser(s string) (u *discord.User, err error)
- func (ctx *Context) Peek() string
- func (ctx *Context) Pop() string
- func (ctx *Context) Reply(content string, embed *discord.Embed) (m *discord.Message, err error)
- func (ctx *Context) Send(content string, embed *discord.Embed) (m *discord.Message, err error)
- func (ctx *Context) Sendf(template string, args ...interface{}) (m *discord.Message, err error)
- func (ctx *Context) YesNoHandler(msg discord.Message, user discord.UserID) (yes, timeout bool)
- type CooldownCache
- type CustomPerms
- type MessageSend
- func (m *MessageSend) AddFile(name string, reader io.Reader) *MessageSend
- func (m *MessageSend) AllowedMentions(a *api.AllowedMentions) *MessageSend
- func (m *MessageSend) BlockMentions() *MessageSend
- func (m *MessageSend) Channel(c discord.ChannelID) *MessageSend
- func (m *MessageSend) Content(c string) *MessageSend
- func (m *MessageSend) Embed(e *discord.Embed) *MessageSend
- func (m *MessageSend) Reference(id discord.MessageID) *MessageSend
- func (m *MessageSend) Send() (msg *discord.Message, err error)
- func (m *MessageSend) TogglePermCheck() *MessageSend
- type PermError
- type Prefixer
- type Router
- func (r *Router) AddCommand(c *Command) *Command
- func (r *Router) Alias(name string, aliases, path []string, argTransform ArgTransformer) (*Command, error)
- func (r *Router) AliasMust(name string, aliases, path []string, argTransform ArgTransformer) *Command
- func (r *Router) Commands() []*Command
- func (r *Router) DefaultPrefixer(m discord.Message) int
- func (r *Router) DeleteReactions(m discord.MessageID)
- func (r *Router) Execute(ctx *Context) (err error)
- func (r *Router) GetCommand(name string) *Command
- func (r *Router) MatchPrefix(m discord.Message) bool
- func (r *Router) MessageCreate(m *gateway.MessageCreateEvent)
- func (r *Router) MsgHandlerCreate(e *gateway.MessageCreateEvent)
- func (r *Router) NewContext(m *gateway.MessageCreateEvent) (ctx *Context, err error)
- func (r *Router) ReactionAdd(e *gateway.MessageReactionAddEvent)
- func (r *Router) ReactionMessageDelete(m *gateway.MessageDeleteEvent)
- func (r *Router) SetBotUser() error
Constants ¶
const RequiredIntents = gateway.IntentGuildMessages | gateway.IntentGuildMessageReactions | gateway.IntentDirectMessages | gateway.IntentDirectMessageReactions | gateway.IntentGuilds
RequiredIntents are the intents required for the command handler
Variables ¶
var ( ErrNoPath = errors.New("alias: no path supplied") ErrNilCommand = errors.New("alias: command was nil") )
Errors related to creating aliases
var ( ErrorNotEnoughArgs = errors.New("not enough arguments") ErrorTooManyArgs = errors.New("too many arguments") )
Errors
var ( ErrChannel = errors.New("context: couldn't get channel") ErrNoBotUser = errors.New("context: couldn't get bot user") ErrEmptyMessage = errors.New("context: message was empty") )
Errors related to getting the context
var ( ErrInvalidMention = errors.New("invalid mention") ErrChannelNotFound = errors.New("channel not found") ErrMemberNotFound = errors.New("member not found") ErrUserNotFound = errors.New("user not found") ErrRoleNotFound = errors.New("role not found") )
Errors related to parsing
var (
ErrBotMissingPermissions = errors.New("bot is missing permissions")
)
Errors related to sending messages
var ErrNoEmbeds = errors.New("PagedEmbed: no embeds")
ErrNoEmbeds is returned if PagedEmbed() is called without any embeds
Functions ¶
func DefaultValue ¶
DefaultValue returns the second value if the first is empty
func EscapeBackticks ¶
EscapeBackticks escapes backticks in strings
func HasAnyPrefix ¶
HasAnyPrefix checks if the string has *any* of the given prefixes
func HasAnySuffix ¶
HasAnySuffix checks if the string has *any* of the given suffixes
func PermStrings ¶
func PermStrings(p discord.Permissions) []string
PermStrings gets the permission strings for all required permissions
func SprintfAll ¶
SprintfAll takes a slice of strings and uses them as input for Sprintf, returning a slice of strings
func TrimPrefixesSpace ¶
TrimPrefixesSpace trims all given prefixes as well as whitespace from the given string
Types ¶
type ArgTransformer ¶
ArgTransformer is used in Alias, passing in the context's RawArgs, which are then split again.
func DefaultArgTransformer ¶
func DefaultArgTransformer(prefix, suffix string) ArgTransformer
DefaultArgTransformer adds a prefix or suffix (or both!) to the current args
type Args ¶ added in v0.8.0
type Args [2]int
Args is a minimum/maximum argument count. If either is -1, it's treated as "no minimum" or "no maximum". This replaces the Check* functions in Context.
func ArgRange ¶ added in v0.8.0
ArgRange returns an *Args with both a minimum and maximum number of arguments.
func ExactArgs ¶ added in v0.8.0
ExactArgs returns an *Args with an exact number of required arguments.
type Command ¶
type Command struct { Name string Aliases []string // Blacklistable commands use the router's blacklist function to check if they can be run Blacklistable bool // Summary is used in the command list Summary string // Description is used in the help command Description string // Usage is appended to the command name in help commands Usage string // Hidden commands are not returned from (*Router).Commands() Hidden bool Args *Args CustomPermissions CustomPerms Permissions discord.Permissions Command func(*Context) error GuildOnly bool OwnerOnly bool Cooldown time.Duration // contains filtered or unexported fields }
Command is a single command, or a group
func (*Command) AddSubcommand ¶
AddSubcommand adds a subcommand to a command
func (*Command) GetCommand ¶
GetCommand gets a command by name
type Context ¶
type Context struct { // Command and Prefix contain the invoked command's name and prefix, respectively. // Note that Command won't be accurate if the invoked command was a subcommand, use FullCommandPath for that. Command string Prefix string FullCommandPath []string Args []string RawArgs string Session *state.State Bot *discord.User // Info about the message Message discord.Message Channel *discord.Channel Author discord.User // Note: Member is nil for non-guild messages Member *discord.Member // The command and the router used Cmd *Command Router *Router AdditionalParams map[string]interface{} // contains filtered or unexported fields }
Context is a command context
func (*Context) AddMessageHandler ¶
func (ctx *Context) AddMessageHandler( c discord.ChannelID, user discord.UserID, fn func(*Context, discord.Message), )
AddMessageHandler adds a message handler for the given user/channel
func (*Context) AddReactionHandler ¶
func (ctx *Context) AddReactionHandler( msg discord.MessageID, user discord.UserID, reaction string, deleteOnTrigger, deleteReaction bool, fn func(*Context), )
AddReactionHandler adds a reaction handler for the given message
func (*Context) CheckArgRange ¶
CheckArgRange checks if the number of arguments is within the given range
func (*Context) CheckBotPerms ¶
func (ctx *Context) CheckBotPerms(p discord.Permissions) (err error)
CheckBotPerms checks the bot's permissions in the current channel
func (*Context) CheckMinArgs ¶
CheckMinArgs checks if the argument count is less than the given count
func (*Context) CheckPerms ¶
CheckPerms checks the user's permissions in the current channel
func (*Context) CheckRequiredArgs ¶
CheckRequiredArgs checks if the arg count is exactly the given count
func (*Context) Edit ¶
func (ctx *Context) Edit(m *discord.Message, c string, embed *discord.Embed) (msg *discord.Message, err error)
Edit the given message
func (*Context) GreedyChannelParser ¶
GreedyChannelParser parses all arguments until it finds an error. Returns the parsed channels and the position at which it stopped. If all arguments were parsed as channels, returns -1.
func (*Context) GreedyMemberParser ¶
GreedyMemberParser parses all arguments until it finds an error. Returns the parsed members and the position at which it stopped. If all arguments were parsed as members, returns -1.
func (*Context) GreedyRoleParser ¶
GreedyRoleParser parses all arguments until it finds an error. Returns the parsed roles and the position at which it stopped. If all arguments were parsed as roles, returns -1.
func (*Context) GreedyUserParser ¶
GreedyUserParser parses all arguments until it finds an error. Returns the parsed users and the position at which it stopped. If all arguments were parsed as users, returns -1.
func (*Context) NewDM ¶ added in v0.9.1
func (ctx *Context) NewDM(user discord.UserID) *MessageSend
NewDM creates a new MessageSend object for the given user. If the user has closed DMs, this will not error until the Send() call.
func (*Context) NewMessage ¶ added in v0.9.0
func (ctx *Context) NewMessage(channel ...discord.ChannelID) *MessageSend
NewMessage creates a new MessageSend object. Only the *first* channel argument is used, if omitted it's set to the current channel.
func (*Context) PagedEmbed ¶
func (ctx *Context) PagedEmbed(embeds []discord.Embed, extendedReactions bool) (msg *discord.Message, err error)
PagedEmbed sends a slice of embeds, and attaches reaction handlers to flip through them. if extendedReactions is true, also add delete, first page, and last page reactions.
func (*Context) ParseChannel ¶
ParseChannel parses a channel mention/id/name
func (*Context) ParseMember ¶
ParseMember parses a member mention/id/name
func (*Context) Peek ¶ added in v0.10.0
Peek gets the next argument from the context's Args without removing it
func (*Context) Pop ¶ added in v0.10.0
Pop gets the next argument from the context's Args and removes it from the slice
func (*Context) YesNoHandler ¶ added in v0.12.0
YesNoHandler adds a reaction handler for the given message. This handler times out after one minute. If it timed out, `false` and `true` are returned, respectively.
type CooldownCache ¶
type CooldownCache struct {
// contains filtered or unexported fields
}
CooldownCache holds cooldowns for commands
type CustomPerms ¶
type CustomPerms interface { // The string used for the permissions if the check fails String() string // Returns true if the user has permission to run the command Check(*Context) (bool, error) }
CustomPerms is a custom permission checker
type MessageSend ¶ added in v0.9.0
type MessageSend struct { Data api.SendMessageData // contains filtered or unexported fields }
MessageSend is a helper struct for sending messages. By default, it will send a message to the current channel, and check permissions (unless the target channel is the current channel and is a DM channel). These can be overridden with the Channel(id) and TogglePermCheck() methods. Alternatively, you can get the base SendMessageData struct and use that manually.
func (*MessageSend) AddFile ¶ added in v0.9.1
func (m *MessageSend) AddFile(name string, reader io.Reader) *MessageSend
AddFile adds a file to the message
func (*MessageSend) AllowedMentions ¶ added in v0.9.0
func (m *MessageSend) AllowedMentions(a *api.AllowedMentions) *MessageSend
AllowedMentions sets the message's allowed mentions
func (*MessageSend) BlockMentions ¶ added in v0.9.0
func (m *MessageSend) BlockMentions() *MessageSend
BlockMentions blocks all mentions from this message
func (*MessageSend) Channel ¶ added in v0.9.0
func (m *MessageSend) Channel(c discord.ChannelID) *MessageSend
Channel sets the channel to send the message to
func (*MessageSend) Content ¶ added in v0.9.0
func (m *MessageSend) Content(c string) *MessageSend
Content sets the message content
func (*MessageSend) Embed ¶ added in v0.9.0
func (m *MessageSend) Embed(e *discord.Embed) *MessageSend
Embed sets the message embed
func (*MessageSend) Reference ¶ added in v0.9.0
func (m *MessageSend) Reference(id discord.MessageID) *MessageSend
Reference sets the message this message will reply to
func (*MessageSend) Send ¶ added in v0.9.0
func (m *MessageSend) Send() (msg *discord.Message, err error)
Send sends the message
func (*MessageSend) TogglePermCheck ¶ added in v0.9.0
func (m *MessageSend) TogglePermCheck() *MessageSend
TogglePermCheck toggles whether or not to check permissions for the destination channel
type PermError ¶
type PermError struct {
// contains filtered or unexported fields
}
PermError is a permission error
type Prefixer ¶ added in v0.11.0
Prefixer returns the prefix used and the length. If the message doesn't start with a valid prefix, it returns -1. Note that this function should still use the built-in r.Prefixes for mention prefixes
type Router ¶
type Router struct { BotOwners []string Prefixes []string Prefixer Prefixer Session *state.State Bot *discord.User BlacklistFunc func(*Context) bool HelpCommand func(*Context) error DefaultMentions *api.AllowedMentions EmbedColor discord.Color // contains filtered or unexported fields }
Router is the command router
func NewWithIntents ¶ added in v0.10.0
func NewWithIntents(token string, owners []discord.UserID, prefixes []string, intents gateway.Intents) (*Router, error)
NewWithIntents creates a new router with a state, with the specified intents. The token is automatically prefixed with `Bot `.
func NewWithState ¶
NewWithState creates a new router with a state. The token is automatically prefixed with `Bot `.
func (*Router) AddCommand ¶
AddCommand adds a command to the router
func (*Router) Alias ¶
func (r *Router) Alias(name string, aliases, path []string, argTransform ArgTransformer) (*Command, error)
Alias creates an alias to the command `path`, and transforms the arguments according to argTransform. argTransform is called with the context's RawArgs.
func (*Router) AliasMust ¶
func (r *Router) AliasMust(name string, aliases, path []string, argTransform ArgTransformer) *Command
AliasMust is a wrapper around Alias that panics if err is non-nil
func (*Router) DefaultPrefixer ¶ added in v0.11.0
DefaultPrefixer ...
func (*Router) DeleteReactions ¶
DeleteReactions deletes all reactions for a message
func (*Router) GetCommand ¶
GetCommand gets a command by name
func (*Router) MatchPrefix ¶
MatchPrefix returns true if the message content contains any of the prefixes
func (*Router) MessageCreate ¶
func (r *Router) MessageCreate(m *gateway.MessageCreateEvent)
MessageCreate gets called on new messages - makes sure the router has a bot user - checks if the message matches a prefix - runs commands
func (*Router) MsgHandlerCreate ¶
func (r *Router) MsgHandlerCreate(e *gateway.MessageCreateEvent)
MsgHandlerCreate runs when a new message is sent
func (*Router) NewContext ¶
func (r *Router) NewContext(m *gateway.MessageCreateEvent) (ctx *Context, err error)
NewContext returns a new message context
func (*Router) ReactionAdd ¶
func (r *Router) ReactionAdd(e *gateway.MessageReactionAddEvent)
ReactionAdd runs when a reaction is added to a message
func (*Router) ReactionMessageDelete ¶
func (r *Router) ReactionMessageDelete(m *gateway.MessageDeleteEvent)
ReactionMessageDelete cleans up old handlers on deleted messages
func (*Router) SetBotUser ¶
SetBotUser sets the router's bot user, returning any errors
Source Files
¶
- alias.go
- argCheck.go
- bcr.go
- command.go
- commands.go
- context.go
- cooldown.go
- ctxMessage.go
- ctxReaction.go
- ctx_args.go
- editMessage.go
- execute.go
- greedyParsers.go
- help.go
- helpCmd.go
- match.go
- messageCreate.go
- messageHandler.go
- messageSend.go
- pagedEmbed.go
- parsers.go
- permCheck.go
- reactionHandler.go
- reply.go
- strings.go
- util.go