bot

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2020 License: Apache-2.0 Imports: 12 Imported by: 6

Documentation

Index

Constants

View Source
const FlagSeparator = 'ー'

Variables

View Source
var ParseArgs = func(args string) ([]string, error) {

	r := csv.NewReader(strings.NewReader(args))
	r.Comma = ' '

	return r.Read()
}

Functions

func Start

func Start(token string, cmd interface{},
	opts func(*Context) error) (stop func() error, err error)

Start quickly starts a bot with the given command. It will prepend "Bot" into the token automatically. Refer to example/ for usage.

func Wait

func Wait()

Wait is a convenient function that blocks until a SIGINT is sent.

Types

type CommandContext

type CommandContext struct {
	Description string
	Flag        NameFlag
	// contains filtered or unexported fields
}

CommandContext is an internal struct containing fields to make this library work. As such, they're all unexported. Description, however, is exported for editing, and may be used to generate more informative help messages.

func (*CommandContext) Name

func (cctx *CommandContext) Name() string

func (*CommandContext) Usage

func (cctx *CommandContext) Usage() []string

type Context

type Context struct {
	*Subcommand
	*state.State

	// Descriptive (but optional) bot name
	Name string

	// Descriptive help body
	Description string

	// The prefix for commands
	Prefix string

	// FormatError formats any errors returned by anything, including the method
	// commands or the reflect functions. This also includes invalid usage
	// errors or unknown command errors. Returning an empty string means
	// ignoring the error.
	FormatError func(error) string

	// ErrorLogger logs any error that anything makes and the library can't
	// reply to the client. This includes any event callback errors that aren't
	// Message Create.
	ErrorLogger func(error)

	// ReplyError when true replies to the user the error.
	ReplyError bool

	// Subcommands contains all the registered subcommands.
	Subcommands []*Subcommand
}

Context is the bot state for commands and subcommands.

A command can be created by making it a method of Commands, or whatever struct was given to the constructor. This following example creates a command with a single integer argument (which can be ran with "~example 123"):

func (c *Commands) Example(m *gateway.MessageCreateEvent, i int) error {
    _, err := c.Ctx.SendMessage(m.ChannelID, fmt.Sprintf("You sent: %d", i))
    return err
}

Commands' exported methods will all be used as commands. Messages are parsed with its first argument (the command) mapped accordingly to c.MapName, which capitalizes the first letter automatically to reflect the exported method name.

func New

func New(s *state.State, cmd interface{}) (*Context, error)

New makes a new context with a "~" as the prefix. cmds must be a pointer to a struct with a *Context field. Example:

type Commands struct {
    Ctx *Context
}

cmds := &Commands{}
c, err := rfrouter.New(session, cmds)

The default prefix is "~", which means commands must start with "~" followed by the command name in the first argument, else it will be ignored.

c.Start() should be called afterwards to actually handle incoming events.

func (*Context) Call

func (ctx *Context) Call(event interface{}) error

Call should only be used if you know what you're doing.

func (*Context) Help

func (ctx *Context) Help() string

Help generates one. This function is used more for reference than an actual help message. As such, it only uses exported fields or methods.

func (*Context) RegisterSubcommand

func (ctx *Context) RegisterSubcommand(cmd interface{}) (*Subcommand, error)

func (*Context) Start

func (ctx *Context) Start() func()

Start adds itself into the discordgo Session handlers. This needs to be run. The returned function is a delete function, which removes itself from the Session handlers.

type Descriptor

type Descriptor interface {
	Description() string
}

Descriptor is optionally used to set the Description of a command context.

type ErrInvalidUsage

type ErrInvalidUsage struct {
	Args   []string
	Prefix string

	Index int
	Err   string
	// contains filtered or unexported fields
}

func (*ErrInvalidUsage) Error

func (err *ErrInvalidUsage) Error() string

type ErrUnknownCommand

type ErrUnknownCommand struct {
	Command string
	Parent  string

	Prefix string
	// contains filtered or unexported fields
}

func (*ErrUnknownCommand) Error

func (err *ErrUnknownCommand) Error() string

type ManualParseable

type ManualParseable interface {
	// $0 will have its prefix trimmed.
	ParseContent([]string) error
}

ManaulParseable implements a ParseContent(string) method. If the library sees this for an argument, it will send all of the arguments (including the command) into the method. If used, this should be the only argument followed after the Message Create event. Any more and the router will ignore.

type NameFlag

type NameFlag uint64
const (
	None NameFlag = 1 << iota

	Raw       // R
	AdminOnly // A
)

func ParseFlag

func ParseFlag(name string) (NameFlag, string)

func (NameFlag) Is

func (f NameFlag) Is(flag NameFlag) bool

type Namer

type Namer interface {
	Name() string
}

Namer is optionally used to override the command context's name.

type Parseable

type Parseable interface {
	Parse(string) error
}

Parseable implements a Parse(string) method for data structures that can be used as arguments.

type RawArguments

type RawArguments struct {
	Arguments []string
}

func (*RawArguments) ParseContent

func (r *RawArguments) ParseContent(args []string) error

type Subcommand

type Subcommand struct {
	Description string

	// Commands contains all the registered command contexts.
	Commands []*CommandContext

	// struct flags
	Flag NameFlag
	// contains filtered or unexported fields
}

func NewSubcommand

func NewSubcommand(cmd interface{}) (*Subcommand, error)

func (*Subcommand) InitCommands

func (sub *Subcommand) InitCommands(ctx *Context) error

InitCommands fills a Subcommand with a context. This shouldn't be called at all, rather you should use the RegisterSubcommand method of a Context.

func (*Subcommand) Name

func (sub *Subcommand) Name() string

Name returns the command name in lower case. This only returns non-zero for subcommands.

func (*Subcommand) NeedsName

func (sub *Subcommand) NeedsName()

NeedsName sets the name for this subcommand. Like InitCommands, this shouldn't be called at all, rather you should use RegisterSubcommand.

type Usager

type Usager interface {
	Usage() string
}

Usager is optionally used to override the generated usage for either an argument, or multiple (using ManualParseable).

Directories

Path Synopsis
extras

Jump to

Keyboard shortcuts

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