cmd

package
v0.0.0-...-04b0558 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2022 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInsufficientArgs = errors.New("not enough arguments for command")

ErrInsufficientArgs is returned by argument parsing functions if it does not have sufficient arguments passed and is not optional.

Functions

func ByAlias

func ByAlias(alias string) (opt safetypes.Option[Command])

func Commands

func Commands() map[string]Command

func Init

func Init(cli bot.Client, prefix string)

func Register

func Register(command Command)

Types

type Allower

type Allower interface {
	// Allow checks if the Source passed is allowed to execute the command. True is returned if the Source is
	// allowed to execute the command.
	Allow(ctx Context) bool
}

Allower may be implemented by a type also implementing Runnable to limit the sources that may run the command.

type Command

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

Command is a wrapper around a Runnable. It provides additional identity and utility methods for the actual runnable command so that it may be identified more easily.

func New

func New(name string, desc string, aliases []string, r ...Runnable) Command

New returns a new Command using the name and description passed. The Runnable passed must be a (pointer to a) struct, with its fields representing the parameters of the command. When the command is run, the Run method of the Runnable will be called, after all fields have their values from the parsed command set. If r is not a struct or a pointer to a struct, New panics.

func (Command) Aliases

func (cmd Command) Aliases() []string

Aliases returns a list of aliases for the command. In addition to the name of the command, the command may be called using one of these aliases.

func (Command) Description

func (cmd Command) Description() string

Description returns the description of the command. The description is shown in the /help list, and provides information on the functionality of a command.

func (Command) Execute

func (cmd Command) Execute(args string, ctx Context) (res safetypes.Result[discord.MessageCreate])

Execute executes the Command as a source with the args passed. The args are parsed assuming they do not start with the command name. Execute will attempt to parse and execute one Runnable at a time. If one of the Runnable was able to parse args correctly, it will be executed and no more Runnables will be attempted to be run. If parsing of all Runnables was unsuccessful, a command output with an error message is sent to the Source passed, and the Run method of the Runnables are not called. The Source passed must not be nil. The method will panic if a nil Source is passed.

func (Command) Name

func (cmd Command) Name() string

Name returns the name of the command. The name is guaranteed to be lowercase and will never have spaces in it. This name is used to call the command, and is shown in the /help list.

func (Command) Params

func (cmd Command) Params(ctx Context) [][]ParamInfo

Params returns a list of all parameters of the runnables. No assumptions should be done on the values that they hold: Only the types are guaranteed to be consistent.

func (Command) Runnables

func (cmd Command) Runnables(ctx Context) map[int]Runnable

Runnables returns a map of all Runnable implementations of the Command that a Source can execute.

func (Command) String

func (cmd Command) String() string

String returns the usage of the command. The usage will be roughly equal to the one showed by the client.

func (Command) Usage

func (cmd Command) Usage() string

Usage returns the usage of the command. The usage will be roughly equal to the one showed by the client in-game.

type Context

type Context struct {
	Client bot.Client
	Member *discord.ResolvedMember
}

Context is a sturcture for passing data to command.

type Enum

type Enum interface {
	// Type returns the type of the enum. This type shows up client-side in the command usage, in the spot
	// where parameter types otherwise are.
	// Type names returned are used as an identifier for this enum type. Different Enum implementations must
	// return a different string in the Type method.
	Type() string
	// Options should return a list of options that show up on the client side. The command will ensure that
	// the argument passed to the enum parameter will be equal to one of these options. The provided Source
	// can also be used to change the enums for each player.
	Options() []string
}

Enum is an interface for enum-type parameters. Users may have types as command parameters that implement this parameter in order to allow a specific set of options only. Enum implementations must be of the type string, for example:

type GameMode string
func (GameMode) Type() string { return "GameMode" }
func (GameMode) Options(Source) []string { return []string{"survival", "creative"} }

Their values will then automatically be set to whichever option returned in Enum.Options is selected by the user.

type Line

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

Line represents a command line holding command arguments that were passed upon the execution of the command. It is a convenience wrapper around a string slice.

func (*Line) Leftover

func (line *Line) Leftover() []string

Leftover takes the leftover arguments from the command line.

func (*Line) Len

func (line *Line) Len() int

Len returns the leftover length of the arguments in the command line.

func (*Line) Next

func (line *Line) Next() (string, bool)

Next reads the next argument from the command line and returns it. If there were no more arguments to consume, false is returned.

func (*Line) NextN

func (line *Line) NextN(n int) ([]string, bool)

NextN reads the next N arguments from the command line and returns them. If there were not enough arguments (n arguments), false is returned.

func (*Line) RemoveN

func (line *Line) RemoveN(n int)

RemoveN consumes the next N arguments from the command line.

func (*Line) RemoveNext

func (line *Line) RemoveNext()

RemoveNext consumes the next argument from the command line.

type Optional

type Optional[T any] struct {
	// contains filtered or unexported fields
}

Optional is an argument type that may be used to make any of the available parameter types optional. Optional command parameters may only occur at the end of the Runnable struct. No non-optional parameter is allowed after an optional parameter.

func (Optional[T]) Load

func (o Optional[T]) Load() (T, bool)

Load returns the value specified upon executing the command and a bool that is true if the parameter was filled out by the Source.

func (Optional[T]) LoadOr

func (o Optional[T]) LoadOr(or T) T

LoadOr returns the value specified upon executing the command, or a value 'or' if the parameter was not filled out by the Source.

type ParamInfo

type ParamInfo struct {
	Name     string
	Value    any
	Optional bool
	Suffix   string
}

ParamInfo holds the information of a parameter in a Runnable. Information of a parameter may be obtained by calling Command.Params().

type Parameter

type Parameter interface {
	// Parse takes an arbitrary amount of arguments from the command Line passed and parses it, so that it can
	// store it to value v. If the arguments cannot be parsed from the Line, an error should be returned.
	Parse(line *Line, v reflect.Value) error
	// Type returns the type of the parameter. It will show up in the usage of the command, and, if one of the
	// known type names, will also show up client-side.
	Type() string
}

Parameter is an interface for a generic parameters. Users may have types as command parameters that implement this parameter.

type Runnable

type Runnable interface {
	// Run runs the Command, using the Context.
	Run(ctx Context) discord.MessageCreate
}

Runnable represents a Command that may be run by a Command source. The Command must be a struct type and its fields represent the parameters of the Command. When the Run method is called, these fields are set and may be used for behaviour in the Command. Fields unexported or ignored using the `cmd:"-"` struct tag (see below) have their values copied but retained. A Runnable may have exported fields only of the following types: int8, int16, int32, int64, int, uint8, uint16, uint32, uint64, uint, float32, float64, string, bool, mgl64.Vec3, Varargs, []Target, cmd.SubCommand, Optional[T] (to make a parameter optional), or a type that implements the cmd.Parameter or cmd.Enum interface. cmd.Enum implementations must be of the type string. Fields in the Runnable struct may have `cmd:` struct tag to specify the name and suffix of a parameter as such:

type T struct {
    Param int `cmd:"name,suffix"`
}

If no name is set, the field name is used. Additionally, the name as specified in the struct tag may be '-' to make the parser ignore the field. In this case, the field does not have to be of one of the types above.

type SubCommand

type SubCommand struct{}

SubCommand represents a subcommand that may be added as a static value that must be written. Adding multiple Runnable implementations to the command in New with different SubCommand fields as the first parameter allows for commands with subcommands.

type Varargs

type Varargs string

Varargs is an argument type that may be used to capture all arguments that follow. This is useful for, for example, messages and names.

Jump to

Keyboard shortcuts

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