commands

package
v0.0.0-...-a75bd19 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: MIT Imports: 9 Imported by: 0

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

Examples

Constants

View Source
const (
	// Prefix is the prefix for commands
	Prefix = "!"
	// MessageLimit is the character limit for messages
	MessageLimit = 2000
)

Variables

View Source
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{}
)
View Source
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 DBClose

func DBClose() error

DBClose closes the db

func DBGet

func DBGet(s Storer, key string, got Storer) error

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 DBLock

func DBLock()

DBLock locks the db

func DBNewOnce

func DBNewOnce()

DBNewOnce refreshes the once primitive

func DBOnce

func DBOnce(do func())

DBOnce uses the once primitive

func DBOpen

func DBOpen(path string) error

DBOpen opens the db at the given path

func DBSet

func DBSet(s Storer, key string) (previous string, replaced bool, err error)

DBSet is a Storer method that sets the given Storer in the db at the key.

func DBUnlock

func DBUnlock()

DBUnlock unlocks the db

func FillArgs

func FillArgs(c Command, args []string) error

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

func GetUsage(c Command) (usage string)

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

func InitGuilds(ses *discordgo.Session) error

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 NewSend

func NewSend(cid string) *CommandSend

NewSend Returns a send struct.

func NewSimpleSend

func NewSimpleSend(cid string, msg string) *CommandSend

NewSimpleSend Returns a send struct with the message content filled in.

func (*CommandSend) Embed

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.

func (*CommandSend) Send

func (c *CommandSend) Send(s *discordgo.Session) error

Send Sends the messages a command returns while also checking message length

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

Jump to

Keyboard shortcuts

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