bot

package
v0.0.0-...-bf87dab Latest Latest
Warning

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

Go to latest
Published: May 30, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package bot contains some generically useful bot rigging for Discord chatbots.

This package works by defining command handlers in a CommandSet, and then dispatching based on message contents. If the bot's command prefix is `;`, then `;foo` activates the command handler for command `foo`.

A CommandSet has a mutex baked into it for convenience of command implementation.

Index

Constants

View Source
const (
	DefaultPrefix = "."
)

The default command prefix. Command `foo` becomes `.foo` in chat, etc.

Variables

View Source
var (
	ErrAlreadyExists     = errors.New("bot: command already exists")
	ErrNoSuchCommand     = errors.New("bot: no such command exists")
	ErrNoPermissions     = errors.New("bot: you do not have permissions for this command")
	ErrParvCountMismatch = errors.New("bot: parameter count mismatch")
)

Command handling errors.

View Source
var (
	DefaultCommandSet = NewCommandSet()
)

The "default" command set, useful for simple bot projects.

View Source
var (
	ErrRateLimitExceeded = errors.New("bot: per-command rate limit exceeded")
)

Functions

func NewCommand

func NewCommand(verb, helptext string, handler, permissions Handler) error

NewCommand creates an anonymous command and adds it to the default CommandSet.

func NoPermissions

func NoPermissions(s *discordgo.Session, m *discordgo.Message, parv []string) error

NoPermissions is a simple middelware function that allows all command invocations to pass the permissions check.

Types

type CommandHandler

type CommandHandler interface {
	Verb() string
	Helptext() string

	Handler(*discordgo.Session, *discordgo.Message, []string) error
	Permissions(*discordgo.Session, *discordgo.Message, []string) error
}

CommandHandler is a generic interface for types that implement a bot command. It is akin to http.Handler, but more comprehensive.

func NewBasicCommand

func NewBasicCommand(verb, helptext string, permissions, handler Handler) CommandHandler

NewBasicCommand creates a CommandHandler instance using the implementation functions supplied as arguments.

type CommandSet

type CommandSet struct {
	sync.Mutex

	Prefix string
	// contains filtered or unexported fields
}

CommandSet is a group of bot commands similar to an http.ServeMux.

func NewCommandSet

func NewCommandSet() *CommandSet

NewCommandSet creates a new command set with the `help` command pre-loaded.

func (*CommandSet) Add

func (cs *CommandSet) Add(h CommandHandler) error

Add adds a single command handler to the CommandSet. This can be done at runtime but it is suggested to only add commands on application boot.

func (*CommandSet) AddCmd

func (cs *CommandSet) AddCmd(verb, helptext string, permissions, handler Handler) error

AddCmd is syntactic sugar for cs.Add(NewBasicCommand(args...))

func (*CommandSet) Run

func (cs *CommandSet) Run(s *discordgo.Session, msg *discordgo.Message) error

Run makes a CommandSet compatible with discordgo event dispatching.

type Handler

type Handler func(*discordgo.Session, *discordgo.Message, []string) error

Handler is the type that bot command functions need to implement. Errors should be returned.

Jump to

Keyboard shortcuts

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