Documentation ¶
Overview ¶
Package commands implements a command interface for pcsocgo with helper structs and funcs for sending discordgo messages, and high-level abstractions of buntdb
Example ¶
package main import ( "fmt" "github.com/unswpcsoc/pcsocgo/commands" ) type Person struct { Name string `json:"name"` Age int `json:"age"` // DO NOT DO THIS //c string `json:"unexported"` } // Index implements commands.Storer // for thing func (p *Person) Index() string { return "person" } func main() { commands.DBOpen(":memory:") defer commands.DBClose() exp := Person{ Name: "Bob", Age: 42, } fmt.Printf("exp = %#v\n", exp) // Our commands.Storer can be entered into the db by calling the interface "method" DBSet commands.DBSet(&exp, "0") // Our commands.Storer can be accessed from the db by calling the interface "method" DBGet var got Person commands.DBGet(&Person{}, "0", &got) fmt.Printf("got = %#v\n", got) }
Output: exp = commands_test.Person{Name:"Bob", Age:42} got = commands_test.Person{Name:"Bob", Age:42}
Index ¶
- Constants
- Variables
- func CleanArgs(c Command)
- func DBClose() error
- func DBGet(s Storer, key string, got Storer) error
- func DBLock()
- func DBNewOnce()
- func DBOnce(do func())
- func DBOpen(path string) error
- func DBSet(s Storer, key string) (previous string, replaced bool, err error)
- func DBUnlock()
- func FillArgs(c Command, args []string) error
- func GetUsage(c Command) (usage string)
- func InitGuilds(ses *discordgo.Session) error
- type Command
- type CommandSend
- type Storer
Examples ¶
Constants ¶
const ( // Prefix is the prefix for commands Prefix = "!" // MessageLimit is the character limit for messages MessageLimit = 2000 )
Variables ¶
var ( // ErrSendLimit means the message was too long ErrSendLimit = errors.New("message exceeds send limit of 2000 characters") // ErrNotEnoughArgs means the user did not provide enough arguments to the command ErrNotEnoughArgs = errors.New("not enough arguments provided") // Guild is the guild to use (this bot is designed for only one server) Guild = &discordgo.UserGuild{} )
var ( // DB is the database DB *buntdb.DB // ErrDBNotOpen means db wasn't opened when trying to use it ErrDBNotOpen = errors.New("db not open, use DBOpen()") // ErrDBClosed means db was closed already ErrDBClosed = errors.New("db already closes") // ErrDBValueNil means you tried to set a nil value into the db ErrDBValueNil = errors.New("cannot set nil value") // ErrDBKeyEmpty means you tried to set a value without a key ErrDBKeyEmpty = errors.New("cannot set value with empty key") // ErrDBNotPtr means you didn't give a pointer ErrDBNotPtr = errors.New("want pointer arg, got something else") // ErrDBNotFound means there is no db ErrDBNotFound = buntdb.ErrNotFound // ErrStorerNil means you have made bad life decisions ErrStorerNil = errors.New("storer method received nil") )
Functions ¶
func CleanArgs ¶
func CleanArgs(c Command)
CleanArgs cleans arg-fields from commands after they've been handled
This should be called after your Command is done handling the message.
Also should be called on Command creation if you don't trust your programmers
func DBGet ¶
DBGet gets the Storer at the given key and puts it into got. Ignores expiry.
If got is not a pointer, DBGet will throw ErrDBNotPtr
func FillArgs ¶
FillArgs tries to fill the given command's struct fields with the args given
FillArgs will return a strconv error if types cannot be matched and will panic if there are unexported arg fields or if variable args are done incorrectly or if input is generally messed up
func GetUsage ¶
GetUsage generates the usage message from a Command in the following format
!alias0 (type0) __arg0__ (type1) __arg1__ ... description of command __Aliases__ | !alias1 | !alias2 ...
func InitGuilds ¶
InitGuilds initialises guilds for the modules to use
Types ¶
type Command ¶
type Command interface { Aliases() []string // Aliases Desc() string // Description Subcommands() []Command // Slice of subcommands, if any Roles() []string // Roles required Chans() []string // Channels required MsgHandle(*discordgo.Session, *discordgo.Message) (*CommandSend, error) // Handler for MessageCreate event }
Command is the interface that all commands implement.
type CommandSend ¶
type CommandSend struct {
// contains filtered or unexported fields
}
CommandSend is a helper struct that buffers things commands need to send.
func NewSimpleSend ¶
func NewSimpleSend(cid string, msg string) *CommandSend
NewSimpleSend Returns a send struct with the message content filled in.
func (*CommandSend) Embed ¶
func (c *CommandSend) Embed(emb *discordgo.MessageEmbed) *CommandSend
Embed Adds an embed-only message to be sent.
func (*CommandSend) Message ¶
func (c *CommandSend) Message(msg string) *CommandSend
Message Adds another simple message to be sent.
func (*CommandSend) MessageSend ¶
func (c *CommandSend) MessageSend(send *discordgo.MessageSend) *CommandSend
MessageSend Adds a discordgo MessageSend.
type Storer ¶
type Storer interface {
Index() string // Determines db index
}
Storer is the interface for structs that will be stored into the db
You MUST export ALL fields in a Storer, otherwise the JSON Marshaller will freak out there are workarounds, but they require more effort than we need. Read https://stackoverflow.com/a/49372417 if you're interested