multiplexer

package module
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2021 License: GPL-3.0 Imports: 9 Imported by: 8

Documentation

Index

Constants

View Source
const AdminOnly = "This command is only available to system administrators!"

AdminOnly is the message sent when an unprivileged user invokes an admin-only request.

View Source
const ErrorOccurred = "Something went wrong and I am very confused! Please try again!"

ErrorOccurred is the message sent when the event handler catches an error.

View Source
const FeatureDisabled = "This feature is currently disabled."

FeatureDisabled is the message sent when a feature requested by the user is disabled.

View Source
const GuildOnly = "This command can only be issued from a guild."

GuildOnly is the message sent when a guild-only command is issued in private.

View Source
const InvalidArgument = "Invalid argument."

InvalidArgument is the message sent when the user passes an invalid argument.

View Source
const KappaColor = 0x3492c4

KappaColor is the primary color of the kappa.

View Source
const LackingPermission = "Lacking permission to perform specified action."

LackingPermission is the message sent when lacking permission for an operation.

View Source
const MissingUser = "Specified user does not exist."

MissingUser is the message sent when a specified user does not exist.

View Source
const OperatorOnly = "This command is only available to operators!"

OperatorOnly is the message sent when an unprivileged user invokes an operator-only request.

View Source
const PermissionDenied = "You are not allowed to issue this command!"

PermissionDenied is the message sent when the user invokes a request without sufficient permission.

Variables

View Source
var (
	AudioCategory = NewCategory("Audio",
		"Audio related utilities.")
	ExperienceCategory = NewCategory("Experience",
		"Chat experience and ranking system.")
	ManualsCategory = NewCategory("Manuals",
		"The operation manual pager utility.")
	MediaCategory = NewCategory("Media",
		"Media related utilities.")
	ModerationCategory = NewCategory("Moderation",
		"Chat moderation utilities.")
	SystemCategory = NewCategory("System",
		"System-related utilities.")
)

Predefined command categories.

View Source
var ErrUserNotFound = errors.New("user not found")

ErrUserNotFound represents the error returned when a user is not found.

View Source
var GetPrefix = func(context *Context) string {
	return context.Multiplexer.Prefix
}

GetPrefix is the function used to get a prefix of a guild.

View Source
var NoCommandMatched = func(context *Context) {
	context.SendMessage("Command not found.")
}

NoCommandMatched is called when no command is matched.

Functions

func GetChannel

func GetChannel(session *discordgo.Session, id string) *discordgo.Channel

GetChannel fetches channel from cache then API and returns nil if all fails.

func GetGuild

func GetGuild(session *discordgo.Session, id string) *discordgo.Guild

GetGuild fetches guild from cache then API and returns nil if all fails.

Types

type CommandCategory

type CommandCategory struct {
	Routes      []*Route
	Title       string
	Description string
}

CommandCategory represents a category of Route.

func NewCategory

func NewCategory(name string, description string) *CommandCategory

NewCategory returns a new command category

type CommandHandler

type CommandHandler func(*Context)

CommandHandler represents the handler function of a Route.

type Context

type Context struct {
	Multiplexer       *Multiplexer
	User              *discordgo.User
	Member            *discordgo.Member
	Message           *discordgo.Message
	Session           *discordgo.Session
	Guild             *discordgo.Guild
	Channel           *discordgo.Channel
	Event             interface{}
	Text              string
	Fields            []string
	IsPrivate         bool
	IsTargeted        bool
	HasPrefix         bool
	HasMention        bool
	HasLeadingMention bool
}

Context carries an event's information.

func (*Context) Ban

func (context *Context) Ban(query string) error

Ban creates a ban on the specified user.

func (*Context) GetChannel

func (context *Context) GetChannel(query string) *discordgo.Channel

GetChannel gets a channel from a string representing it.

func (*Context) GetMember

func (context *Context) GetMember(query string) *discordgo.Member

GetMember gets a member from a string representing it.

func (*Context) GetRole

func (context *Context) GetRole(query string) *discordgo.Role

GetRole gets a channel from a string representing it.

func (*Context) GetVoiceState

func (context *Context) GetVoiceState() (*discordgo.VoiceState, bool)

GetVoiceState returns the voice state of a user if found.

func (*Context) HandleError

func (context *Context) HandleError(err error) bool

HandleError handles a returned error and send the information of it if in debug mode.

func (*Context) HasPermission

func (context *Context) HasPermission(permission int) bool

HasPermission checks a user for a permission.

func (*Context) IsAdministrator

func (context *Context) IsAdministrator() bool

IsAdministrator checks of a user is the system administrator.

func (*Context) IsOperator

func (context *Context) IsOperator() bool

IsOperator checks of a user is an operator.

func (*Context) MakeVoiceConnection

func (context *Context) MakeVoiceConnection() (*discordgo.VoiceConnection, error)

MakeVoiceConnection returns the voice connection to a user's voice channel if join-able.

func (Context) NumericalRegex

func (Context) NumericalRegex() *regexp.Regexp

NumericalRegex returns a compiled regular expression that matches only numbers.

func (*Context) Prefix

func (context *Context) Prefix() string

Prefix returns the command prefix of a context.

func (*Context) SendEmbed

func (context *Context) SendEmbed(message string, embed embedutil.Embed) *discordgo.Message

SendEmbed sends an embed message in the current channel and returns the message.

func (*Context) SendMessage

func (context *Context) SendMessage(message string) *discordgo.Message

SendMessage sends a text message in the current channel and returns the message.

func (*Context) StitchFields

func (context *Context) StitchFields(start int) string

StitchFields stitches together fields of the message.

type Multiplexer

type Multiplexer struct {
	// Prefix is the default command prefix.
	Prefix string

	// Routes is a slice of pointers to command routes.
	Routes []*Route
	// Categories is a slice of pointers to CommandCategory.
	Categories []*CommandCategory

	// EventHandlers is a slice of event handler functions registered to the library directly
	EventHandlers []interface{}

	NotTargeted           []func(context *Context)
	Ready                 []func(context *Context)
	GuildMemberAdd        []func(context *Context)
	GuildMemberRemove     []func(context *Context)
	GuildDelete           []func(context *Context)
	MessageCreate         []func(context *Context)
	MessageDelete         []func(context *Context)
	MessageUpdate         []func(context *Context)
	MessageReactionAdd    []func(context *Context)
	MessageReactionRemove []func(context *Context)
	VoiceStateUpdate      []func(context *Context)

	// Administrator is the privileged administrator user with all privilege overrides and full access to all commands.
	Administrator *discordgo.User
	// Operator is a slice of operator users with all privilege overrides and access to some restricted commands.
	Operator []*discordgo.User
}

Multiplexer represents the event router.

func New

func New() *Multiplexer

New returns a command router.

func (*Multiplexer) IsAdministrator

func (mux *Multiplexer) IsAdministrator(id string) bool

IsAdministrator checks of a user is the system administrator.

func (*Multiplexer) IsOperator

func (mux *Multiplexer) IsOperator(id string) bool

IsOperator checks of a user is an operator.

func (*Multiplexer) MatchRoute

func (mux *Multiplexer) MatchRoute(message string) (*Route, []string)

MatchRoute fuzzy matches a message to a route.

func (*Multiplexer) NewContextMessage

func (mux *Multiplexer) NewContextMessage(session *discordgo.Session, message *discordgo.Message, event interface{}) *Context

NewContextMessage returns pointer to Context generated from a message.

func (*Multiplexer) Route

func (mux *Multiplexer) Route(route *Route) *Route

Route registers a route to the router.

func (*Multiplexer) SessionRegisterHandlers

func (mux *Multiplexer) SessionRegisterHandlers(session *discordgo.Session)

type Route

type Route struct {
	Pattern       string
	AliasPatterns []string
	Description   string
	Category      *CommandCategory
	Handler       CommandHandler
}

Route represents a command route.

Jump to

Keyboard shortcuts

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