Documentation ¶
Index ¶
- Variables
- func FlagPerson(person *int64, f *Flags)
- func FlagPlace(place *int64, f *Flags)
- func Format(cmd CommandStatic, prefix string) string
- func OnlyOneBitSet(n int) bool
- func Split(text string, lenCnt func(string) int, lenLim int) []string
- func TypeFlag(p *CommandType, value CommandType, f *Flags)
- type Channel
- type Command
- type CommandRuntime
- type CommandStatic
- type CommandType
- type CommandsStatic
- type Flags
- type Message
- func (m *Message) Admin() bool
- func (m *Message) Author() (int64, error)
- func (m *Message) CommandParse() (*Message, error)
- func (m *Message) CommandRun() (*Message, error)
- func (m *Message) Fields() []string
- func (m *Message) FieldsSpace() []string
- func (m *Message) HereExact() (int64, error)
- func (m *Message) HereLogical() (int64, error)
- func (m *Message) Hooks()
- func (m *Message) Mod() bool
- func (m *Message) Prefixes() ([]Prefix, bool, error)
- func (m *Message) RawArgs(n int) string
- func (m *Message) Run()
- func (m *Message) Usage() any
- func (m *Message) Write(msg any, usrErr error) (*Message, error)
- type Messenger
- type Prefix
- type SQLDB
- func (db *SQLDB) Close() error
- func (db *SQLDB) Init(schema string) error
- func (db *SQLDB) PrefixList(scope int64) ([]Prefix, error)
- func (_ *SQLDB) ScopeAdd(tx *sql.Tx, id string, frontend int) (int64, error)
- func (db *SQLDB) ScopeFrontend(scope int64) (int64, error)
- func (db *SQLDB) ScopeID(scope int64) (string, error)
- type States
- type User
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingArgs = errors.New("not enough arguments provided") ErrSilence = errors.New("if this error is returned don't send any message") )
var Add = []string{
"add",
"new",
"create",
}
var Delete = []string{
"delete",
"del",
"remove",
"rm",
}
var Edit = []string{
"edit",
"modify",
"change",
}
var Hooks = hooks{}
Hooks are a list of functions that are applied one-by-one to incoming messages.
var (
Host string
)
var List = []string{
"list",
"ls",
}
var Prefixes = prefixes{}
var Show = []string{
"show",
"view",
"get",
}
Functions ¶
func FlagPerson ¶
func Format ¶
func Format(cmd CommandStatic, prefix string) string
Formats a static command into something that can be shown to a user. Generally used in help messages to point the user to a specific command in order to avoid hardcoding it. Returns the command in the following format:
<prefix><command> [sub-command...] <usage-args>
For example: !command delete <command>
func OnlyOneBitSet ¶
func Split ¶
Splits a message into submessages. Tries to not split words unless it absolutely has to in which case it splits based on grapheme clusters.
func TypeFlag ¶
func TypeFlag(p *CommandType, value CommandType, f *Flags)
Types ¶
type Command ¶
type Command struct { CommandStatic CommandRuntime }
type CommandRuntime ¶
type CommandRuntime struct { // The "path" taken to invoke the command, i.e. which names were used. // Includes all the sub-commands e.g. ["prefix", "add"] in order to be able // to display accurate help messages. Path []string // The arguments passed, includes everything that's not part of the // command's name. Args []string // The prefix used when the command was called. Prefix string }
A command's runtime information.
type CommandStatic ¶
type CommandStatic interface { // The command's type. Type() CommandType // Any other checks required for a command to be executed. Returns true if // the command is allowed to be executed. Usually used to chcek a user's // permissions or to restrict a command to specific frontends. Permitted(m *Message) bool // All the aliases a command has. The first item in the list is considered // the main name and so should be the simplest and most intuitive one for // the average person. For example if it's a delete subcommand the first // alias should be "delete" instead of "del" or "rm". Names() []string // A short description of what the command does. Description() string // Usage arguments. Should follow this format: // - <required> // - [optional] // - (literal-string) or (many | literals) UsageArgs() string // A command's parent, this is automatically set during bot startup. Parent() CommandStatic // The command's sub-commands. Children() CommandsStatic // This is executed during bot startup. Should be used to set things up // necessary for the command, for example DB schemas. Init() error // The function that is called to run the command. Run(m *Message) (resp any, usrErr error, err error) }
A command needs to implement this interface.
type CommandType ¶
type CommandType int
const ( // A simplified command with might not give full control over something but // it has a very easy to use API. Normal CommandType = 1 << iota // The full command and usually consists of many subcommands which makes it // less intuitive for the average person. Advanced // Bot admin only command used to perform actions like setting an arbitrary // person's options, etc. Admin All = Normal | Advanced | Admin )
The command types.
type CommandsStatic ¶
type CommandsStatic []CommandStatic
var Commands *CommandsStatic
func (*CommandsStatic) Match ¶
func (cmds *CommandsStatic) Match(t CommandType, m *Message, args []string) (CommandStatic, int, error)
Given a list of arguments match the appropriate command. The arguments don't have to correspond to a command exactly. For example,
`args = [prefix add abc]`.
In this case the prefix's subcommand `add` will be matched and returned. Alongside it the index of the last valid command will be returned (in this case the index of "add", which is 1). This makes it easy to know which aliases where used by the user when invoking a command.
func (CommandsStatic) Usage ¶
func (cmds CommandsStatic) Usage() string
Return the names of the children in a format that can be used in the UsageArgs function.
type Message ¶
type Message struct { ID string Frontend int Raw string User *User Channel *Channel Client Messenger Command *Command // contains filtered or unexported fields }
func Await ¶
Monitor incoming messages until `check` is true or until timeout. If nothing is matched then the returned object will be nil.
func (*Message) CommandParse ¶
func (*Message) CommandRun ¶
func (*Message) FieldsSpace ¶
Split text into fields that include all trailing whitespace. For example: "example of text" will be split into ["example ", "of ", "text"]
func (*Message) HereLogical ¶
Returns the logical here's scope.
func (*Message) Prefixes ¶
Returns the logical here's prefixes and also whether or not they were taken from the database (if not then that means the default ones were used).
type Messenger ¶
type Messenger interface { // Checks if the message's author is a bot admin BotAdmin() bool Parse() (*Message, error) // Returns the ID of the passed string. The returned ID must be valid. // Generally used for verifying an ID's validity and extracting IDs from // mentions. PlaceID(s string) (id string, err error) PersonID(s, placeID string) (id string, err error) // Gets the target's scope. If it doesn't exist it will create it and add // it to the database. Person(id string) (person int64, err error) // There exist 2 types of place scopes that are used, the exact place and // the logical place. The logical is the area where things are generally // expected to work. For example: if a user adds a custom command in a // server they would probably expect it to work in the entire server and not // just in the specific channel that they added it in. If on the other hand // someone adds a custom command in a discord DM message, then no guild // exists and thus the channel's scope would have to be used. On the other // hand `PlaceExact` returns exactly the scope of the id passed and does not // account for context. PlaceExact(id string) (place int64, err error) PlaceLogical(id string) (place int64, err error) Usage(usage string) any // Should return true only if a user has basically every permission. Admin() bool // General rule of thumb is that if they can ban people, they are mods. Mod() bool // Sends a message to the appropriate scope, `resp` could be `nil` depending // on the frontend. Send(msg any, usrErr error) (resp *Message, err error) // Same as `Send` except the user is also pinged. Ping(msg any, usrErr error) (resp *Message, err error) // Either calls `Send` or `Ping` depending on the frontend. This is what // should be used in most cases. Write(msg any, usrErr error) (resp *Message, err error) }
The frontend abstraction layer, a frontend needs to implement this in order to be added.
type Prefix ¶
type Prefix struct { Type CommandType Prefix string }
type SQLDB ¶
var DB *SQLDB
func (*SQLDB) PrefixList ¶
Returns the list of all prefixes for a specific scope.
func (*SQLDB) ScopeFrontend ¶
Returns then given scope's frontend id