onelib

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2023 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AliasTable = "onelib_aliases"
)
View Source
const (
	// DEBUG is a relic constant, it will be removed in favour of something else in the future.
	DEBUG = 1
)

Variables

View Source
var (
	// Error is used for logging errors. It outputs to stderr and file.
	Error *logger
	// Info is used for logging information. It outputs to stdout and file.
	Info *logger
	// Debug is used for logging miscellaneous things, mostly for debugging code. It outputs to stdout.
	Debug *logger
)
View Source
var (
	// DefaultPrefix is the default prefix used to trigger commands. (Ex: In ",say Hello" the "," would be the prefix)
	DefaultPrefix string
	// DefaultNickname is the default display name for the bot.
	DefaultNickname string
	// DefaultAvatar is a URL to the default avatar for the bot.
	DefaultAvatar string

	// Protocols is a map of loaded Protocols. Key is protocol name (ex: "discord")
	Protocols *ProtocolMap
	// Plugins is a map of loaded Plugins. Key is plugin name (ex: "admin_tools")
	Plugins *PluginMap
	// Commands is a map of loaded Commands. Key is command trigger (ex: "help")
	Commands *CommandMap
	// Monitors is a slice of loaded Monitors.
	Monitors *MonitorSlice

	// Db is configured via config file only.
	Db Database

	// DbEngine is the database type currently in use.
	DbEngine string
	// PluginDir is the directory plugins are stored.
	PluginDir string
	// PluginLoadList is only used for loading the default plugins.
	PluginLoadList []string
	// ProtocolDir is the directory protocols are stored.
	ProtocolDir string
	// ProtocolLoadList is only used for loading the default protocols.
	ProtocolLoadList []string

	// Quit - the main thread will watch this to terminate the process.
	Quit chan os.Signal
)
View Source
var Alias *userStore

Functions

func GetBoolConfig added in v0.0.4

func GetBoolConfig(plugin, key string) bool

GetBoolConfig returns a config value, checking the DB first, expecting a boolean.

func GetIntConfig added in v0.0.3

func GetIntConfig(plugin, key string) (int, error)

GetIntConfig returns a config value, checking the DB first, expecting an int.

func GetTextConfig added in v0.0.1

func GetTextConfig(plugin, key string) string

GetTextConfig returns a config value, checking the DB first, expecting a string.

func InitLoggers

func InitLoggers(logfile string)

InitLoggers is supposed to only be called once, it initializes the loggers, opening any related logfiles.

func LoadConfig

func LoadConfig()

LoadConfig loads the configuration file and inits the DB. This does not respect locks on config, do not run this while any goroutines are running. Ultimately this will check the DB before loading from the config file. TODO set default config path TODO add an option to check DB before config file

func LoadPlugin

func LoadPlugin(name string) (err error)

LoadPlugin loads a plugin by filename (minus extension)

func LoadPlugins

func LoadPlugins()

LoadPlugins loads all plugins in the plugin directory

func LoadProtocol

func LoadProtocol(name string) (err error)

LoadProtocol loads a protocol by filename (minus extension)

func LoadProtocols

func LoadProtocols()

LoadProtocols loads all protocols in the protocol directory

func ProcessMessage

func ProcessMessage(prefix []string, msg Message, sender Sender)

ProcessMessage processes command and monitor triggers, spawning a new goroutine for every trigger.

func ProcessUpdate added in v0.0.3

func ProcessUpdate(msg Message, sender Sender)

ProcessUpdate processes monitor trigger "mon.OnMessageUpdate"

func SetTextConfig added in v0.0.1

func SetTextConfig(plugin, key, text string)

SetTextConfig sets a string config value.

func UnloadPlugin

func UnloadPlugin(name string) error

UnloadPlugin removes a plugin from the active plugins map, returning an error if not loaded, calling the related delete methods.

func UnloadPlugins

func UnloadPlugins()

UnloadPlugins unloads every plugin, calling their unload routines.

func UnloadProtocols

func UnloadProtocols()

UnloadProtocols unloads every protocol, calling their unload routines.

Types

type Command

type Command func(msg Message, sender Sender)

Command is a function called when a certain key is triggered.

type CommandMap

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

CommandMap is a concurrent-safe map of commands

func NewCommandMap

func NewCommandMap() *CommandMap

NewCommandMap returns a new concurrent-safe CommandMap.

func (*CommandMap) Delete

func (cm *CommandMap) Delete(commandName string)

Delete removes the command from the active command list, calling the command's unload method via goroutine.

func (*CommandMap) DeleteAll

func (cm *CommandMap) DeleteAll()

DeleteAll removes all commands from the active command list.

func (*CommandMap) DeleteSet

func (cm *CommandMap) DeleteSet(set map[string]Command)

DeleteSet removes a map of commands from the active command list, calling the command's unload method via goroutine.

func (*CommandMap) Get

func (cm *CommandMap) Get(commandName string) Command

Get a command from the CommandMap.

func (*CommandMap) Put

func (cm *CommandMap) Put(commandName string, command Command)

Put a command into the CommandMap.

type Database

type Database interface {
	Get(table, key string) (map[string]interface{}, error)           // Retrieves value by key directly
	GetString(table, key string) (string, error)                     // Retrieve a string stored with PutString.
	GetInt(table, key string) (int, error)                           // Retrieve an int stored with PutInt.
	GetObj(table, key string, obj interface{}) error                 // Retrieve an object stored with PutObj.
	Search(table, field, key string) (map[string]interface{}, error) // Searches for key in field, containing key (IE: field:'username', key:'admin'), using an index if exists.
	Put(table string, data map[string]interface{}) ([]byte, error)   // Inserts data into database, using "_id" field as key, generating one if none exists. Returns key.
	PutString(table, key, text string) error                         // Inserts text at location "key" for retrieval via GetString
	PutInt(table, key string, i int) error                           // Inserts an integer at location "key" for retrieval via GetInt
	PutObj(table, key string, obj interface{}) error                 // Inserts an object at location "key" for retrieval via GetObj
	Remove(table, key string) error                                  // Removes an object at location "key"
	SetIndex(table, field string) error                              // Sets an index on field. If using LevelDB, values in this field must be unique.
	Close() error                                                    // Terminate a database session (only run if nothing is using the database).
}

Database represents a database connection. It's meant to be simple, to work for most general usage.

type Emoji added in v0.0.3

type Emoji struct {
	ID    UUID   // The UUID of the emoji, should never be blank.
	Name  string // Either a unicode representation of the emoji, or a name.
	Added bool   // If true, the emoji was just added as a reaction. If false, it was just removed. Ignore field on reaction lists (See Message.Reactions()).
}

Emoji contains data which should be useful around emojis.

type Location

type Location interface {
	DisplayName() string // Display name of the location
	Nickname() string    // The nickname of the bot in the location
	Topic() string       // The topic of the location
	// Picture // TODO The avatar of the location
	UUID() UUID                                   // Unique identifier for the location
	Send(msg Message)                             // Sends a message to the location
	SendText(text string)                         // Sends text to the location
	SendFormattedText(text, formattedText string) // Sends formatted text to the location (correctness might vary between protocols)
	Protocol() string                             // Returns the name of the protocol the location is in
}

Location represents a location in a protocol. Think like a room, or a group.

type Message

type Message interface {
	Text() string                      // The unformatted text being received (minus the trigger for commands)
	FormattedText() string             // the formatted text being received (minus trigger for commands)
	StripPrefix(prefix string) Message // Returns a copy of the message with `prefix + commandName + " "` stripped (Ex: "!say Hello" becomes "Hello")
	Raw() []byte                       // The raw data received
	UUID() UUID                        // Unique identifier for the message (can be empty)
	Mentioned() bool                   // True if the bot was mentioned in the message
	Reaction() *Emoji                  // Returns an emoji that was either added or removed, or nil if none

}

Message contains information either being sent or received

type Monitor

type Monitor struct {
	OnMessage         func(from Sender, msg Message)    // Called on every new message
	OnMessageWithText func(from Sender, msg Message)    // Called on every new message containing text
	OnMessageUpdate   func(from Sender, update Message) // Called on message update (IE: edit, reaction)

}

Monitor is a struct containing pointers to functions which are called on certain events (can be nil).

type MonitorSlice

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

MonitorSlice is a concurrent-safe slice of monitors.

func NewMonitorSlice

func NewMonitorSlice() *MonitorSlice

NewMonitorSlice returns a new concurrent-safe MonitorSlice.

func (*MonitorSlice) Delete

func (ms *MonitorSlice) Delete(monitor *Monitor)

Delete removes the monitor from the active monitor list.

func (MonitorSlice) DeleteAll

func (ms MonitorSlice) DeleteAll()

DeleteAll removes all monitors from the active monitor list.

func (*MonitorSlice) Get

func (ms *MonitorSlice) Get() []*Monitor

Get copy of monitor slice for reading.

func (*MonitorSlice) Put

func (ms *MonitorSlice) Put(monitor *Monitor)

Put a monitor into the MonitorSlice.

type Plugin

type Plugin interface {
	Name() string                               // The name of the plugin, used in the plugin map (should be same as filename, minus extension)
	LongName() string                           // The display name of the plugin
	Version() string                            // The version of the plugin
	Implements() (map[string]Command, *Monitor) // Returns a map of commands and monitor the plugin implements
	Remove()                                    // Called when the plugin is about to be terminated
}

Plugin is an object representing a OneBot plugin.

type PluginMap

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

PluginMap is a concurrent-safe map of plugins.

func NewPluginMap

func NewPluginMap() *PluginMap

NewPluginMap returns a new concurrent-safe PluginMap.

func (*PluginMap) Delete

func (pm *PluginMap) Delete(pluginName string)

Delete removes the plugin from the active plugin list, calling the plugin's unload method via goroutine

func (*PluginMap) DeleteAll

func (pm *PluginMap) DeleteAll()

DeleteAll removes all plugins from the active plugin list, calling the plugin's unload method via goroutine

func (*PluginMap) Get

func (pm *PluginMap) Get(pluginName string) Plugin

Get a plugin from the PluginMap

func (*PluginMap) Put

func (pm *PluginMap) Put(pluginName string, plugin Plugin)

Put an already loaded plugin into the PluginMap

type Protocol

type Protocol interface {
	Name() string                                          // The name of the protocol, used in the protocol map (should be same as filename, minus extension)
	LongName() string                                      // The display name of the protocol
	Version() string                                       // The version of the protocol
	NewMessage(raw []byte) Message                         // Returns a new Message object built from []byte (TODO: I hate this)
	Send(to UUID, msg Message)                             // Sends a Message to a location
	SendText(to UUID, text string)                         // Sends text to a location
	SendFormattedText(to UUID, text, formattedText string) // Sends formatted text to a location (correctness might vary between protocols)
	Remove()                                               // Called when the protocol is about to be terminated
}

Protocol contains information about a protocol plugin

type ProtocolMap

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

ProtocolMap is a concurrent-safe map of protocols.

func NewProtocolMap

func NewProtocolMap() *ProtocolMap

NewProtocolMap returns a new concurrent-safe ProtocolMap

func (*ProtocolMap) Delete

func (pm *ProtocolMap) Delete(protocolName string)

Delete removes the protocol from the active protocol list, calling the protocol's unload method via goroutine

func (*ProtocolMap) DeleteAll

func (pm *ProtocolMap) DeleteAll()

DeleteAll removes all protocols from the active protocol list, calling the protocol's unload method via goroutine

func (*ProtocolMap) Get

func (pm *ProtocolMap) Get(protocolName string) Protocol

Get a protocol from the ProtocolMap

func (*ProtocolMap) Put

func (pm *ProtocolMap) Put(protocolName string, protocol Protocol)

Put an already loaded protocol into the ProtocolMap

type Sender

type Sender interface {
	DisplayName() string // Display name of the sender
	Username() string    // Username of the sender (often unknown, should return an empty string if so)
	UUID() UUID          // Unique identifier for the sender
	// Picture // TODO The avatar of the sender
	Location() Location                           // The location where this sender sent the message from
	Protocol() string                             // Returns the protocol name responsible for the sender
	Self() bool                                   // Returns true if the sender is the bot
	Send(msg Message)                             // Sends a Message to the sender
	SendText(text string)                         // Sends text to the sender
	SendFormattedText(text, formattedText string) // Sends formatted text to the sender (correctness might vary between protocols)
}

Sender contains information about who and where a message came from

type UUID

type UUID string

UUID represents a unique identifier, usually for a Location (room) or a Sender (user).

type UserObject added in v0.0.3

type UserObject struct {
	Alias UUID `bson:"a"` // User alias, UUID
}

UserObject represents an account, and can be aliased to another UserObject via UUID.

Jump to

Keyboard shortcuts

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