bcr

package module
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2021 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

RequiredIntents are the intents required for the command handler

Variables

View Source
var (
	ErrNoPath     = errors.New("alias: no path supplied")
	ErrNilCommand = errors.New("alias: command was nil")
)

Errors related to creating aliases

View Source
var (
	ErrorNotEnoughArgs = errors.New("not enough arguments")
	ErrorTooManyArgs   = errors.New("too many arguments")
)

Errors

View Source
var (
	ErrChannel   = errors.New("context: couldn't get channel")
	ErrNoBotUser = errors.New("context: couldn't get bot user")
)

Errors related to getting the context

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

View Source
var (
	ErrBotMissingPermissions = errors.New("bot is missing permissions")
)

Errors related to sending messages

View Source
var ErrNoEmbeds = errors.New("PagedEmbed: no embeds")

ErrNoEmbeds is returned if PagedEmbed() is called without any embeds

Functions

func AsCode added in v0.4.2

func AsCode(s string) string

AsCode returns the given string as code, correctly escaped

func DefaultValue

func DefaultValue(s, def string) string

DefaultValue returns the second value if the first is empty

func EscapeBackticks

func EscapeBackticks(s string) string

EscapeBackticks escapes backticks in strings

func HasAnyPrefix

func HasAnyPrefix(s string, prefixes ...string) bool

HasAnyPrefix checks if the string has *any* of the given prefixes

func HasAnySuffix

func HasAnySuffix(s string, suffixes ...string) bool

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

func SprintfAll(template string, in []string) []string

SprintfAll takes a slice of strings and uses them as input for Sprintf, returning a slice of strings

func TrimPrefixesSpace

func TrimPrefixesSpace(s string, prefixes ...string) string

TrimPrefixesSpace trims all given prefixes as well as whitespace from the given string

func Version

func Version() string

Version returns the current brc version

Types

type ArgTransformer added in v0.7.0

type ArgTransformer func(string) string

ArgTransformer is used in Alias, passing in the context's RawArgs, which are then split again.

func DefaultArgTransformer added in v0.7.0

func DefaultArgTransformer(prefix, suffix string) ArgTransformer

DefaultArgTransformer adds a prefix or suffix (or both!) to the current args

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

	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

func (c *Command) AddSubcommand(sub *Command) *Command

AddSubcommand adds a subcommand to a command

func (*Command) GetCommand

func (c *Command) GetCommand(name string) *Command

GetCommand gets a command by name

type Context

type Context struct {
	Command string

	Args    []string
	RawArgs string

	Session *state.State
	Bot     *discord.User

	Message discord.Message
	Channel *discord.Channel
	Author  discord.User

	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) AddYesNoHandler added in v0.2.2

func (ctx *Context) AddYesNoHandler(
	msg discord.Message,
	user discord.UserID,
	yesFn func(*Context),
	noFn func(*Context),
)

AddYesNoHandler adds a reaction handler for the given message

func (*Context) CheckArgRange

func (ctx *Context) CheckArgRange(min, max int) (err error)

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

func (ctx *Context) CheckMinArgs(c int) (err error)

CheckMinArgs checks if the argument count is less than the given count

func (*Context) CheckPerms

func (ctx *Context) CheckPerms() (err error)

CheckPerms checks the user's permissions in the current channel

func (*Context) CheckRequiredArgs

func (ctx *Context) CheckRequiredArgs(c int) (err error)

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

func (ctx *Context) GreedyChannelParser(args []string) (channels []*discord.Channel, n int)

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

func (ctx *Context) GreedyMemberParser(args []string) (members []*discord.Member, n int)

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

func (ctx *Context) GreedyRoleParser(args []string) (roles []*discord.Role, n int)

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 added in v0.3.5

func (ctx *Context) GreedyUserParser(args []string) (users []*discord.User, n int)

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

func (ctx *Context) Help(path []string) (err error)

Help sends a help embed for the command

func (*Context) PagedEmbed

func (ctx *Context) PagedEmbed(embeds []discord.Embed) (msg *discord.Message, err error)

PagedEmbed sends a slice of embeds, and attaches reaction handlers to flip through them.

func (*Context) ParseChannel

func (ctx *Context) ParseChannel(s string) (c *discord.Channel, err error)

ParseChannel parses a channel mention/id/name

func (*Context) ParseMember

func (ctx *Context) ParseMember(s string) (c *discord.Member, err error)

ParseMember parses a member mention/id/name

func (*Context) ParseRole

func (ctx *Context) ParseRole(s string) (c *discord.Role, err error)

ParseRole parses a role mention/id/name

func (*Context) ParseUser added in v0.3.5

func (ctx *Context) ParseUser(s string) (u *discord.User, err error)

ParseUser finds a user by mention or ID

func (*Context) Reply

func (ctx *Context) Reply(content string, embed *discord.Embed) (m *discord.Message, err error)

Reply *replies* to the original message in the context channel

func (*Context) Send

func (ctx *Context) Send(content string, embed *discord.Embed) (m *discord.Message, err error)

Send sends a message to the context channel

func (*Context) Sendf added in v0.2.0

func (ctx *Context) Sendf(template string, args ...interface{}) (m *discord.Message, err error)

Sendf sends a message with Printf-like syntax

type CooldownCache

type CooldownCache struct {
	// contains filtered or unexported fields
}

CooldownCache holds cooldowns for commands

func (*CooldownCache) Get

func (c *CooldownCache) Get(cmdName string, userID discord.UserID, channelID discord.ChannelID) bool

Get returns true if the command is on cooldown

func (*CooldownCache) Set

func (c *CooldownCache) Set(cmdName string, userID discord.UserID, channelID discord.ChannelID, cooldown time.Duration)

Set sets a cooldown for a command

type CustomPerms added in v0.7.1

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 PermError

type PermError struct {
	// contains filtered or unexported fields
}

PermError is a permission error

func (*PermError) Error

func (p *PermError) Error() string

type Router

type Router struct {
	BotOwners []string
	Prefixes  []string

	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 NewRouter

func NewRouter(s *state.State, owners, prefixes []string) *Router

NewRouter creates a new router object

func NewWithState added in v0.3.5

func NewWithState(token string, owners, prefixes []string) (*Router, error)

NewWithState creates a new router with a state. The token is automatically prefixed with `Bot `.

func (*Router) AddCommand

func (r *Router) AddCommand(c *Command) *Command

AddCommand adds a command to the router

func (*Router) Alias added in v0.7.0

func (r *Router) Alias(name string, 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 added in v0.7.0

func (r *Router) AliasMust(name string, path []string, argTransform ArgTransformer) *Command

AliasMust is a wrapper around Alias that panics if err is non-nil

func (*Router) Commands added in v0.6.0

func (r *Router) Commands() []*Command

Commands returns a list of commands

func (*Router) DeleteReactions added in v0.2.2

func (r *Router) DeleteReactions(m discord.MessageID)

DeleteReactions deletes all reactions for a message

func (*Router) Execute

func (r *Router) Execute(ctx *Context) (err error)

Execute executes the command router

func (*Router) GetCommand

func (r *Router) GetCommand(name string) *Command

GetCommand gets a command by name

func (*Router) MatchPrefix

func (r *Router) MatchPrefix(content string) bool

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 discord.Message) (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

func (r *Router) SetBotUser() error

SetBotUser sets the router's bot user, returning any errors

Jump to

Keyboard shortcuts

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