bot

package
v0.0.0-...-6b3fda4 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SliceEmptyErorr = "the bot's slice of default commands is empty"

Functions

func CreateViperConfig

func CreateViperConfig(path, name, configType, serverName string) (*viper.Viper, error)

CreateViperConfig creates a viper object. The path is the directory where the config should reside, and name is the filename.

func GetConfigDirectory

func GetConfigDirectory() (string, error)

GetConfigDirectory the "default" location to put in pleasantbot's config files, so Runners can use this function if desired.

func Itob

func Itob(i int) bool

Itob converts an integer (0 or 1) to a corresponding boolean. Mainly used for command moderator perms

func LoadBot

func LoadBot(bot *Bot) error

LoadBot populates many of the misc. struct field values

Types

type BadWord

type BadWord struct {
	Phrase   string
	Severity int // 0 for purge, 1 for perma ban
}

BadWord contains info useful for bannable / purgeable phrases

type Bot

type Bot struct {
	Name        string
	ChannelName string
	ServerName  string

	Config          *viper.Viper `json:"-"`
	Authenticated   bool
	Conn            net.Conn `json:"-"`
	Storage         *Database
	PurgeForLinks   bool
	PurgeForLongMsg bool
	LongMsgAmount   int
	EnableServer    bool
	PostLinkPerm    uint8
	Perms           []string                 `json:"-"` // holds a list of users that can post a link
	DefaultCommands []DefaultCommand         `json:"-"`
	Commands        map[string]*CommandValue `json:"-"`
	BadWords        []BadWord                `json:"-"`
	Quotes          map[int]*QuoteValues     `json:"-"`
	Timers          map[string]*TimedValue   `json:"-"`
	PermittedUsers  map[string]struct{}      // list of users that can post links
	// contains filtered or unexported fields
}

Bot struct contains the necessary data to run an instance of a bot

func CreateBot

func CreateBot(viper *viper.Viper, storage Database, dbInit storage.InitFunc, loader BotLoaderFunc, prepareFunc storage.DatabasePrepareFunc) (*Bot, error)

CreateBot creates an instance of a bot. The function makes no assumptions on what the viper config, Database, and loader funcs for the database and bot data loading should be. It just makes the assumption that these loaders MUST exist, so the caller may pass in custom ones based on the needs of the service (e.g. twitch, youtube, etc.) or it may use the default ones listed in this package.

func (*Bot) AddCommand

func (bot *Bot) AddCommand(item Item) error

AddCommandString takes in a string of the form !addcom !comtitle <command response>

func (*Bot) AddPermittedUser

func (bot *Bot) AddPermittedUser(username string)

AddPermittedUser adds a user to the PermittedUsers slice, allowing them to post a link without being purged

func (*Bot) AddQuote

func (bot *Bot) AddQuote(quote string, submitter string) error

AddQuote adds a quote to the bot's internal slice and the database

func (*Bot) AddTimer

func (bot *Bot) AddTimer(item Item) error

AddTimer takes in an item and parses it to add an associated timer. Will assume enabled by default.

func (*Bot) Connect

func (bot *Bot) Connect() error

Connect establishes a connection to the Twitch IRC server

func (*Bot) ConvertPermToInt

func (bot *Bot) ConvertPermToInt(perm string) (uint8, error)

ConvertPermToInt takes in a string "all", "moderator" etc and converts it to the associated int.

func (*Bot) DeletePermittedUser

func (bot *Bot) DeletePermittedUser(username string) bool

DeletePermittedUser deletes a user from the permittedusers map if they exist in it

func (*Bot) DeleteQuote

func (bot *Bot) DeleteQuote(quoteID string) error

func (*Bot) DeleteTimer

func (bot *Bot) DeleteTimer(item Item) error

DeleteTimer will delete a timer from the map and DB.

func (*Bot) DetectURL

func (bot *Bot) DetectURL(message string) bool

DetectURl uses urlRegex to determine if the passed in message is a URL. The caller than perform whatever action is desired with this information

func (*Bot) EditCommand

func (bot *Bot) EditCommand(item Item) error

func (*Bot) FindCommand

func (bot *Bot) FindCommand(key string) (bool, CommandValue)

FindCommand takes in a key (command name) and returns matching command, if found

func (*Bot) GetOAuth

func (bot *Bot) GetOAuth() string

GetOAuth returns the bot's oauth token

func (*Bot) GetQuote

func (bot *Bot) GetQuote(index int) (string, error)

GetQuote returns a quote of a specified index / id. Correlates to the automatically generated ID in sqlite.

func (*Bot) HandlePing

func (bot *Bot) HandlePing(message, pingIndicator, response string, messenger Messenger) (bool, error)

HandlePing will send a PONG as a response to a PING. Returns true if a PONG had to be sent pingIndicator is the string to check for

func (*Bot) IncrementCommandCount

func (bot *Bot) IncrementCommandCount(command string) error

IncrementCommandCount takes in a command name (key) and increments the associated count value in the DB

func (*Bot) LoadBadWords

func (bot *Bot) LoadBadWords() error

LoadBadWords loads all badwords from the databases TODO: generalize this for all bot data

func (*Bot) LoadCommands

func (bot *Bot) LoadCommands() error

LoadCommands queries the sqlite3 database for existing commands

func (*Bot) LoadQuotes

func (bot *Bot) LoadQuotes() error

LoadQuotes loads all quotes from the DB. TODO: generalize this for all loading functions

func (*Bot) LoadTimers

func (bot *Bot) LoadTimers() error

LoadTimers gets all of the timers that exist in storage

func (*Bot) ParseForBadWord

func (bot *Bot) ParseForBadWord(msg string) (bool, BadWord)

ParseForBadWord reads in a string and sees if a bad word was found and returns that bad word. Callers should check if the bool is true, then use the returned BadWord if true.

func (*Bot) RandomQuote

func (bot *Bot) RandomQuote() (string, error)

RandomQuote returns a random string, it does not print. It's up to the caller on what to do with it.

func (*Bot) RemoveCommand

func (bot *Bot) RemoveCommand(key string) (bool, error)

RemoveCommand takes in a command name as a string, presumably from the chat, and removes it

func (*Bot) RunDefaultCommands

func (bot *Bot) RunDefaultCommands(caller Item) (bool, error)

RunDefaultCommands will run a default command if caller contains the right invocation

func (*Bot) RunTimers

func (bot *Bot) RunTimers(messenger Messenger)

RunTimers will loop over and run any timers in the database that are enabled. A service who wants to use RunTimers must define a Messenger.Message definition so this function knows how to send the timer's contents.

func (*Bot) SetOAuth

func (bot *Bot) SetOAuth(token string)

SetOAuth creates or replaces the bot Oauth token.

func (*Bot) WriteToConn

func (bot *Bot) WriteToConn(msg string) error

WriteToConn when given a string sends a properly formatted message, easy replacement for using fmt.Fprintf

type BotLoaderFunc

type BotLoaderFunc func(bot *Bot) error

type CommandValue

type CommandValue struct {
	Response string `json:"response"`
	Perm     string `json:"perm"`
	Count    int    `json:"count"`
}

CommandValue makes up a single command, used as the value in the bot's underyling commands map

type Database

type Database struct {
	DB             storage.Sqlite `json:"-"`
	Path           string         `json:"-"` // path to the database file
	CommandColumns []string
	QuoteColumns   []string
	TimerColumns   []string
}

defines some DB options, specifically the columns for the various tables

type DefaultCommand

type DefaultCommand struct {
	Type     string
	Command  string
	ExecFunc DefaultFunc
}

type DefaultFunc

type DefaultFunc func(Item, *Bot) error

type FatalError

type FatalError struct {
	Err error
}

FatalError is an error that would warrant an immediate end to the bot

func (FatalError) Error

func (fe FatalError) Error() string

type Item

type Item struct {
	IsServerInfo bool // if true, consider item not from user and can be ignored
	Sender       User
	Type         string // ex: com
	Command      string // ex: add
	Key          string // ex: !somecommand
	Contents     string // ex: this is the value of some command
}

Item represents a new key / value item for the bot such as a command. e.g. a new item request would have a structure such as: !addcom !command <contents> This struct will hold the !command and <contents> values respectively. Each field will hold the value with any leading ! chars removed.

type Messenger

type Messenger interface {
	Message(msg string) error
}

Messenger is used in those times where a function in the bot package HAS to send a message within its definition. This is avoided whenever possible, since the caller (service) should be determining what to do. However there are rare cases, such as with RunTimers, when a interface for sending messages is needed.

type NonFatalError

type NonFatalError struct {
	Err error
}

NonFatalError explains that an error occurred due to a non fatal condition and shouldn't be considered a terrible issue.

func (NonFatalError) Error

func (nfe NonFatalError) Error() string

type QuoteValues

type QuoteValues struct {
	Quote     string
	Timestamp string
	Submitter string
}

QuoteValues represents the values associated with a quote. The ID in the DB will be the map key

type TimedValue

type TimedValue struct {
	Message string
	Minutes int
	Enabled bool
}

TimedValues contains the values needed to run a single timed command.

type User

type User struct {
	Name string
}

Jump to

Keyboard shortcuts

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