crouter

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SuccessEmoji is the emoji used to designate a successful action
	SuccessEmoji = "✅"
	// ErrorEmoji is the emoji used for errors
	ErrorEmoji = "❌"
	// WarnEmoji is the emoji used to warn that a command failed
	WarnEmoji = "⚠️"
)

Variables

This section is empty.

Functions

func PrettyDurationString

func PrettyDurationString(duration time.Duration) (out string)

PrettyDurationString ...

Types

type AutoResponse

type AutoResponse struct {
	Triggers []string
	Regex    *regexp.Regexp
	Response func(s *discordgo.Session, m *discordgo.MessageCreate) error
}

AutoResponse is a single autoresponse, intended for very simple responses to exact messages that don't match commands

type Command

type Command struct {
	Name    string
	Aliases []string
	Regex   *regexp.Regexp

	Description     string
	LongDescription string
	Usage           string

	Command func(*Ctx) error

	Permissions PermLevel
	GuildOnly   bool
	Cooldown    time.Duration

	Router *Router
}

Command is a single command

type Ctx

type Ctx struct {
	GuildPrefix string
	Command     string
	Args        []string
	RawArgs     string

	Session  *discordgo.Session
	Bot      *bot.Bot
	BotUser  *discordgo.User
	Database *cbdb.Db
	BoltDb   *cbdb.BoltDb

	Message *discordgo.MessageCreate
	Channel *discordgo.Channel
	Author  *discordgo.User

	Handlers         *ttlcache.Cache
	AdditionalParams map[string]interface{}
	GuildSettings    *structs.GuildSettings
	Cmd              *Command
}

Ctx is the context for a command

func Context

func Context(prefix, messageContent string, m *discordgo.MessageCreate, b *bot.Bot) (ctx *Ctx, err error)

Context creates a new Ctx

func (*Ctx) AddReactionHandler

func (ctx *Ctx) AddReactionHandler(messageID, reaction string, f func(ctx *Ctx)) func()

AddReactionHandler adds a reaction handler function

func (*Ctx) AddReactionHandlerOnce

func (ctx *Ctx) AddReactionHandlerOnce(messageID, reaction string, f func(ctx *Ctx)) func()

AddReactionHandlerOnce adds a reaction handler function that is only called once

func (*Ctx) AddYesNoHandler

func (ctx *Ctx) AddYesNoHandler(messageID string, yesFunc, noFunc func(ctx *Ctx)) func()

AddYesNoHandler reacts with ✅ and ❌, and runs one of two functions depending on which one is used

func (*Ctx) Check

func (ctx *Ctx) Check(owners []string) (err error)

Check checks if the user has permissions to run a command

func (*Ctx) CheckArgRange

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

CheckArgRange checks if the number of arguments is within the given range

func (*Ctx) CheckMinArgs

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

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

func (*Ctx) CheckRequiredArgs

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

CheckRequiredArgs checks if the arg count is exactly the given count

func (*Ctx) CmdEmbed

func (ctx *Ctx) CmdEmbed(cmd *Command) *discordgo.MessageEmbed

CmdEmbed ...

func (*Ctx) CommandError

func (ctx *Ctx) CommandError(err error) (error, error)

CommandError sends an error message and optionally returns an error for logging purposes

func (*Ctx) Edit

func (ctx *Ctx) Edit(message *discordgo.Message, arg interface{}) (msg *discordgo.Message, err error)

Edit a message

func (*Ctx) EditEmbedf

func (ctx *Ctx) EditEmbedf(message *discordgo.Message, title, format string, args ...interface{}) (msg *discordgo.Message, err error)

EditEmbedf edits an embed with Embedf syntax

func (*Ctx) Editf

func (ctx *Ctx) Editf(message *discordgo.Message, format string, args ...interface{}) (msg *discordgo.Message, err error)

Editf edits a message with Sendf-like syntax

func (*Ctx) Embed

func (ctx *Ctx) Embed(title, description string, color int) (msg *discordgo.Message, err error)

Embed sends the input as an embed

func (*Ctx) EmbedNoXHandler

func (ctx *Ctx) EmbedNoXHandler(title, description string, color int) (msg *discordgo.Message, err error)

EmbedNoXHandler ...

func (*Ctx) Embedf

func (ctx *Ctx) Embedf(title, format string, args ...interface{}) (msg *discordgo.Message, err error)

Embedf sends a fmt.Sprintf-like input string, in an embed

func (*Ctx) EmbedfNoXHandler

func (ctx *Ctx) EmbedfNoXHandler(title, format string, args ...interface{}) (msg *discordgo.Message, err error)

EmbedfNoXHandler ...

func (*Ctx) GroupCmdEmbed

func (ctx *Ctx) GroupCmdEmbed(g *Group, cmd *Command) *discordgo.MessageEmbed

GroupCmdEmbed ...

func (*Ctx) GroupEmbed

func (ctx *Ctx) GroupEmbed(g *Group) *discordgo.MessageEmbed

GroupEmbed ...

func (*Ctx) Invite

func (ctx *Ctx) Invite() string

Invite returns an invite link for the bot

func (*Ctx) Match

func (ctx *Ctx) Match(cmds ...string) bool

Match checks if any of the given command aliases match

func (*Ctx) MatchPrefix

func (ctx *Ctx) MatchPrefix() bool

MatchPrefix checks if the message matched any prefix

func (*Ctx) MatchRegexp

func (ctx *Ctx) MatchRegexp(re *regexp.Regexp) bool

MatchRegexp checks if the command matches the given regex

func (*Ctx) PagedEmbed

func (ctx *Ctx) PagedEmbed(embeds []*discordgo.MessageEmbed) (msg *discordgo.Message, err error)

PagedEmbed ...

func (*Ctx) ParseChannel

func (ctx *Ctx) ParseChannel(channel string) (*discordgo.Channel, error)

ParseChannel takes a string and attempts to find a channel that matches that string

func (*Ctx) ParseMember

func (ctx *Ctx) ParseMember(member string) (*discordgo.Member, error)

ParseMember takes a string and attempts to find a member that matches that string

func (*Ctx) ParseRole

func (ctx *Ctx) ParseRole(role string) (*discordgo.Role, error)

ParseRole takes a string and attempts to find a role that matches that string

func (*Ctx) React

func (ctx *Ctx) React(emoji string) (err error)

React reacts to the triggering message

func (*Ctx) Send

func (ctx *Ctx) Send(arg interface{}) (message *discordgo.Message, err error)

Send a message to the context channel

func (*Ctx) SendNoAddXHandler

func (ctx *Ctx) SendNoAddXHandler(arg interface{}) (message *discordgo.Message, err error)

SendNoAddXHandler sends a message without adding a handler for :x:

func (*Ctx) Sendf

func (ctx *Ctx) Sendf(format string, args ...interface{}) (msg *discordgo.Message, err error)

Sendf sends a fmt.Sprintf-like input string

func (*Ctx) SendfNoAddXHandler

func (ctx *Ctx) SendfNoAddXHandler(format string, args ...interface{}) (msg *discordgo.Message, err error)

SendfNoAddXHandler ...

func (*Ctx) TriggerTyping

func (ctx *Ctx) TriggerTyping() (err error)

TriggerTyping triggers typing in the channel the command was invoked in.

type ErrorMissingRequiredArgs

type ErrorMissingRequiredArgs struct {
	RequiredArgs string
	MissingArgs  string
}

ErrorMissingRequiredArgs is called when one or more required arguments is missing

func (*ErrorMissingRequiredArgs) Error

func (e *ErrorMissingRequiredArgs) Error() string

type ErrorNoDMs

type ErrorNoDMs struct{}

ErrorNoDMs is called when the command cannot be used in a DM

func (*ErrorNoDMs) Error

func (e *ErrorNoDMs) Error() string

type ErrorNoPermissions

type ErrorNoPermissions struct {
	MissingPerms string
}

ErrorNoPermissions is called when the user doesn't have permission to execute the command

func (*ErrorNoPermissions) Error

func (e *ErrorNoPermissions) Error() string

type ErrorNotACommand

type ErrorNotACommand struct{}

ErrorNotACommand is called when the given string isn't a command.

func (*ErrorNotACommand) Error

func (e *ErrorNotACommand) Error() string

type ErrorNotEnoughArgs

type ErrorNotEnoughArgs struct {
	NumRequiredArgs int
	SuppliedArgs    int
}

ErrorNotEnoughArgs is called when not enough arguments were supplied

func (*ErrorNotEnoughArgs) Error

func (e *ErrorNotEnoughArgs) Error() string

type ErrorTooManyArguments

type ErrorTooManyArguments struct {
	MaxArgs      int
	SuppliedArgs int
}

ErrorTooManyArguments is called when there are too many arguments

func (*ErrorTooManyArguments) Error

func (e *ErrorTooManyArguments) Error() string

type Group

type Group struct {
	Name        string
	Aliases     []string
	Regex       *regexp.Regexp
	Description string
	Command     *Command
	Subcommands []*Command
	Router      *Router
	Cooldowns   *ttlcache.Cache
}

Group is a group of subcommands

func (*Group) AddCommand

func (g *Group) AddCommand(cmd *Command)

AddCommand adds a command to a group

func (*Group) Execute

func (g *Group) Execute(ctx *Ctx, guildSettings *structs.GuildSettings) (err error)

Execute executes any command given

func (*Group) GetCommand

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

GetCommand gets a command by name

type PermLevel

type PermLevel int

PermLevel is the permission level of the command

const (
	// PermLevelNone can be used by all users
	PermLevelNone PermLevel = iota
	// PermLevelHelper requires a helper role
	PermLevelHelper
	// PermLevelMod requires a moderator role
	PermLevelMod
	// PermLevelAdmin requires admin perms
	PermLevelAdmin
	// PermLevelOwner requires the person to be the bot owner
	PermLevelOwner
)

func (PermLevel) String

func (p PermLevel) String() string

String gives the string representation of a permission level

type Router

type Router struct {
	Commands      []*Command
	Groups        []*Group
	AutoResponses []*AutoResponse

	BotOwners []string
	Cooldowns *ttlcache.Cache
	Bot       *bot.Bot
}

Router is the command router

func NewRouter

func NewRouter(b *bot.Bot) *Router

NewRouter creates a Router object

func (*Router) AddCommand

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

AddCommand adds a command to the router

func (*Router) AddGroup

func (r *Router) AddGroup(group *Group) *Group

AddGroup adds a group to the router

func (*Router) AddResponse

func (r *Router) AddResponse(response *AutoResponse)

AddResponse adds an autoresponse to the router

func (*Router) Execute

func (r *Router) Execute(ctx *Ctx, guildSettings *structs.GuildSettings) (err error)

Execute actually executes the router

func (*Router) GetCommand

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

GetCommand gets a command by name

func (*Router) GetGroup

func (r *Router) GetGroup(name string) (group *Group)

GetGroup returns a group by name

func (*Router) Help

func (r *Router) Help(ctx *Ctx, guildSettings *structs.GuildSettings) (err error)

Help is the help command

func (*Router) MessageCreate

func (r *Router) MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate)

MessageCreate handles message create events

func (*Router) Respond

func (r *Router) Respond(s *discordgo.Session, m *discordgo.MessageCreate) (err error)

Respond checks if the message is any of the configured autoresponse triggers, and responds if it is

Jump to

Keyboard shortcuts

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