core

package
v0.0.0-...-e03fed0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2022 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingArgs = errors.New("not enough arguments provided")
	ErrSilence     = errors.New("if this error is returned don't send any message")
)
View Source
var Add = []string{
	"add",
	"new",
	"create",
}
View Source
var Delete = []string{
	"delete",
	"del",
	"remove",
	"rm",
}
View Source
var Edit = []string{
	"edit",
	"modify",
	"change",
}
View Source
var Hooks = hooks{}

Hooks are a list of functions that are applied one-by-one to incoming messages.

View Source
var (
	Host string
)
View Source
var List = []string{
	"list",
	"ls",
}
View Source
var Prefixes = prefixes{}
View Source
var Show = []string{
	"show",
	"view",
	"get",
}

Functions

func FlagPerson

func FlagPerson(person *int64, f *Flags)

func FlagPlace

func FlagPlace(place *int64, f *Flags)

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 OnlyOneBitSet(n int) bool

func Split

func Split(text string, lenCnt func(string) int, lenLim int) []string

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 Channel

type Channel struct {
	ID   string
	Name string
}

type Command

type Command struct {
	CommandStatic
	CommandRuntime
}

func (*Command) Usage

func (cmd *Command) Usage() string

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 Flags

type Flags struct {
	FlagSet *flag.FlagSet
	Msg     *Message
	// contains filtered or unexported fields
}

func NewFlags

func NewFlags(m *Message) *Flags

func (*Flags) Parse

func (f *Flags) Parse() ([]string, error)

func (*Flags) Usage

func (f *Flags) Usage()

func (*Flags) Write

func (f *Flags) Write(p []byte) (int, error)

required for the flagSet SetOutput option

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

func Await(timeout time.Duration, check func(*Message) bool) *Message

Monitor incoming messages until `check` is true or until timeout. If nothing is matched then the returned object will be nil.

func (*Message) Admin

func (m *Message) Admin() bool

func (*Message) Author

func (m *Message) Author() (int64, error)

Return's the author's scope.

func (*Message) CommandParse

func (m *Message) CommandParse() (*Message, error)

func (*Message) CommandRun

func (m *Message) CommandRun() (*Message, error)

func (*Message) Fields

func (m *Message) Fields() []string

func (*Message) FieldsSpace

func (m *Message) FieldsSpace() []string

Split text into fields that include all trailing whitespace. For example: "example of text" will be split into ["example ", "of ", "text"]

func (*Message) HereExact

func (m *Message) HereExact() (int64, error)

Return's the exact here's scope.

func (*Message) HereLogical

func (m *Message) HereLogical() (int64, error)

Returns the logical here's scope.

func (*Message) Hooks

func (m *Message) Hooks()

func (*Message) Mod

func (m *Message) Mod() bool

func (*Message) Prefixes

func (m *Message) Prefixes() ([]Prefix, bool, error)

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).

func (*Message) RawArgs

func (m *Message) RawArgs(n int) string

Return the arguments including the whitespace between them. Skip over first n args. Pass 0 to not skip any.

func (*Message) Run

func (m *Message) Run()

func (*Message) Usage

func (m *Message) Usage() any

func (*Message) Write

func (m *Message) Write(msg any, usrErr error) (*Message, error)

Sends a message.

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
}

func PlacePrefixes

func PlacePrefixes(place int64) ([]Prefix, bool, error)

Returns the given place's prefixes and also whether or not they were taken from the database (if not then that means the default ones were used).

type SQLDB

type SQLDB struct {
	Lock sync.RWMutex
	DB   *sql.DB
}
var DB *SQLDB

func Open

func Open(driver, source string) (*SQLDB, error)

func (*SQLDB) Close

func (db *SQLDB) Close() error

func (*SQLDB) Init

func (db *SQLDB) Init(schema string) error

func (*SQLDB) PrefixList

func (db *SQLDB) PrefixList(scope int64) ([]Prefix, error)

Returns the list of all prefixes for a specific scope.

func (*SQLDB) ScopeAdd

func (_ *SQLDB) ScopeAdd(tx *sql.Tx, id string, frontend int) (int64, error)

func (*SQLDB) ScopeFrontend

func (db *SQLDB) ScopeFrontend(scope int64) (int64, error)

Returns then given scope's frontend id

func (*SQLDB) ScopeID

func (db *SQLDB) ScopeID(scope int64) (string, error)

Returns the given scope's frontend specific ID

type States

type States struct {
	gosafe.Slice[string]
}

OAuth state parameter

func (*States) Delete

func (s *States) Delete(token string)

func (*States) Generate

func (s *States) Generate() (string, error)

type User

type User struct {
	ID          string
	Name        string
	DisplayName string
	Mention     string
}

Jump to

Keyboard shortcuts

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