brain

package
v0.0.0-...-55a1825 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2022 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

Package brain implements the knowledge and configuration database for Robot.

For simplicity and portability, the brain currently holds all Markov chain knowledge as well as both global and channel-specific configuration in a single SQLite3 database.

Index

Constants

This section is empty.

Variables

View Source
var NoCopypasta = noCopypasta{}

NoCopypasta is a sentinel error returned by CheckCopypasta to indicate that a message is not a meme for a generic reason.

Functions

func Tokens

func Tokens(msg string) []string

Tokens converts a message into a list of its words.

func UserHash

func UserHash(channel, name string) [32]byte

UserHash obfuscates a username for inclusion in history.

Types

type Brain

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

Brain learns Markov chains over arbitrary and generates text from them. It also manages channel configuration, history, and quotes.

func Configure

func Configure(ctx context.Context, source, me string, order int) (*Brain, error)

Configure loads a brain database with the given username and order by connecting to source. In the current implementation, source should be the path of an SQLite database.

func Open

func Open(ctx context.Context, source string) (*Brain, error)

Open loads a brain database by connecting to source, using the last settings set by Configure for username and order. If no such settings have been set, then the returned error is sql.ErrNoRows. The source must have all tables initialized, as by Configure.

func (*Brain) Activity

func (b *Brain) Activity(ctx context.Context, channel string, f func(float64) float64) (float64, error)

Activity sets the random response rate to a given function of its current response rate. If f returns a value not in the interval [0, 1], an error is returned. Otherwise, the new probability is returned.

func (*Brain) AddAffection

func (b *Brain) AddAffection(ctx context.Context, channel, uid string, score int64) error

AddAffection adds to a user's score. If the user does not have a score in the given channel, this silently does nothing. If uid is the empty string, this adds score to all users in the channel.

func (*Brain) Affection

func (b *Brain) Affection(ctx context.Context, channel, uid string) (int64, error)

Affection gets a user's score. If the user does not have a score in the given channel, this returns 0, nil.

func (*Brain) Audit

func (b *Brain) Audit(ctx context.Context, msg irc.Message, cmd string) error

Audit records a priviliged command activation for audit.

func (*Brain) Channels

func (b *Brain) Channels() []string

Channels returns a list of all channels configured in the database.

func (*Brain) CheckCopypasta

func (b *Brain) CheckCopypasta(ctx context.Context, msg irc.Message) error

CheckCopypasta returns nil if the message is copypasta that the bot should repeat, considering channel settings and whether the bot has already copypasted it, or an error explaining why the bot should not repeat it.

func (*Brain) ClearChat

func (b *Brain) ClearChat(ctx context.Context, channel, user string) error

ClearChat unlearns all recent messages from a given user in a channel.

func (*Brain) ClearMsg

func (b *Brain) ClearMsg(ctx context.Context, msgid string) error

ClearMsg unlearns a single message from history by Twitch message ID.

func (*Brain) ClearPattern

func (b *Brain) ClearPattern(ctx context.Context, channel, pattern string) (int64, error)

ClearPattern unlearns all messages in a channel matching a given pattern and returns the number of messages deleted.

func (*Brain) ClearSince

func (b *Brain) ClearSince(ctx context.Context, channel string, since time.Time) error

ClearSince unlearns all messages in a channel more recent than a given time. Note that a trigger deletes messages older than fifteen minutes on insertion into the bot's history.

func (*Brain) ClearText

func (b *Brain) ClearText(ctx context.Context, channel, text string) error

ClearText unlearns the tuples in an arbitrary string.

func (*Brain) Close

func (b *Brain) Close() error

Close closes the brain's database connection.

func (*Brain) Debug

func (b *Brain) Debug(channel string) (status, block, privs string)

Debug returns strings describing the current status of a channel. If the channel name is unknown, the results are the empty string.

func (*Brain) DebugTag

func (b *Brain) DebugTag(tag string) (emotes, effects []string)

DebugTag returns lists of emotes and effects for a send tag. If the tag is not used for any channel, the results are empty.

func (*Brain) DidSay

func (b *Brain) DidSay(ctx context.Context, channel, msg string) (bool, error)

DidSay detects whether the given message was recently generated by the bot.

func (*Brain) EchoTo

func (b *Brain) EchoTo(channel string) string

EchoTo returns the directory for echoing messages generated for the given channel. If no such directory is set, then this returns the empty string.

func (*Brain) Effect

func (b *Brain) Effect(ctx context.Context, tag string) string

Effect selects a random effect from the given tag. The result is the empty string if the tag is unused by any channel or if the selected effect corresponds to a null SQL text.

func (*Brain) EffectIn

func (b *Brain) EffectIn(ctx context.Context, channel string) string

EffectIn calls Effect with the given channel's send tag.

func (*Brain) Emote

func (b *Brain) Emote(ctx context.Context, tag string) string

Emote selects a random emote from the given tag. The result is the empty string if the tag is unused for sending by any channel or if the selected emote corresponds to a null SQL text.

func (*Brain) EmoteIn

func (b *Brain) EmoteIn(ctx context.Context, channel string) string

EmoteIn calls Emote with the given channel's send tag.

func (*Brain) Exec

func (b *Brain) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes a generic SQL statement on the brain's database.

func (*Brain) Join

func (b *Brain) Join(ctx context.Context, channel, learn, send string) error

Join creates a new entry for a channel if one does not already exist. If learn or send are nonempty, then they are used as the learn or send tag, respectively, updating the existing values if the channel is already known.

func (*Brain) Learn

func (b *Brain) Learn(ctx context.Context, msg irc.Message) error

Learn adds a message to the history and its chains to the tuples database. If the channel to which it was sent is configured to ignore the message for any reason, then this is a no-op.

func (*Brain) LearnTuple

func (b *Brain) LearnTuple(ctx context.Context, tag string, pre []sql.NullString, suf sql.NullString) (sql.Result, error)

LearnTuple directly learns a single chain. Panics if len(pre) is not exactly the chain order. It exists to facilitate converting ancient chain formats to the modern one.

func (*Brain) Marriage

func (b *Brain) Marriage(ctx context.Context, channel string) (uid string, when time.Time, score int64, err error)

Marriage gets the current marriage info for a channel.

func (*Brain) Marry

func (b *Brain) Marry(ctx context.Context, channel, uid string, when time.Time) error

Marry sets the marriage for channel. If uid is the empty string, divorce the current marriage in channel instead.

func (*Brain) Name

func (b *Brain) Name() string

Name returns the username associated with the brain.

func (*Brain) Order

func (b *Brain) Order() int

Order returns the chain order associated with the brain.

func (*Brain) Privilege

func (b *Brain) Privilege(ctx context.Context, channel, nick string, badges []string) (string, error)

Privilege returns the privilege level associated with a user in a channel. If the channel is unrecognized, the privilege level is always "ignore".

func (*Brain) Privmsg

func (b *Brain) Privmsg(ctx context.Context, to, msg string) irc.Message

Privmsg creates an IRC PRIVMSG with a random emote appended to the message.

func (*Brain) Query

func (b *Brain) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query executes a generic SQL query on the brain's database.

func (*Brain) QueryRow

func (b *Brain) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRow executes a generic SQL query on the brain's database, expecting at most one resulting row.

func (*Brain) Said

func (b *Brain) Said(ctx context.Context, channel, msg string) error

Said marks an arbitrary message as having been recently generated, so that the bot will not re-learn it.

func (*Brain) SendTag

func (b *Brain) SendTag(channel string) (string, bool)

SendTag gets the send tag associated with the given channel. The second returned value is false when the channel has no tag.

func (*Brain) SetEchoDir

func (b *Brain) SetEchoDir(dir string)

SetEchoDir sets the directory for echoing.

func (*Brain) SetFallbackWait

func (b *Brain) SetFallbackWait(minor, major *rate.Limiter)

SetFallbackWait sets the hard rate limits for messages for which there is no channel config. This primarily applies to whispers. The default hard rate limits correspond to one message per third of a second and 100 messages per minute.

It is a race condition to call SetFallbackWait when any goroutine might call Wait.

func (*Brain) SetOnline

func (b *Brain) SetOnline(channel string, online bool)

SetOnline marks a channel as online to enable learning or offline to disable it. All channels are offline until this method is used to set them to online. If the channel is not found, this has no effect.

func (*Brain) SetPriv

func (b *Brain) SetPriv(ctx context.Context, user, channel, priv string) error

SetPriv sets a new privilege level for a user.

func (*Brain) SetWait

func (b *Brain) SetWait(ctx context.Context, channel string, limit rate.Limit)

SetWait sets the hard rate limiter for a channel. If the new rate equals the old one, or if the channel is not found, this has no effect. If SetWait would set a new rate limit, it first waits for the old one.

func (*Brain) ShouldTalk

func (b *Brain) ShouldTalk(ctx context.Context, msg irc.Message, random bool) error

ShouldTalk determines whether the given message should trigger talking. A non-nil returned error indicates the reason the message was denied. If random is true, then this incorporates the prob channel config setting; otherwise, this incorporates the respond setting.

func (*Brain) Silence

func (b *Brain) Silence(ctx context.Context, channel string, until time.Time) error

Silence sets a silent time on a channel. If until is the zero time, then null is used instead.

func (*Brain) Talk

func (b *Brain) Talk(ctx context.Context, tag string, chain []string, n int) string

Talk constructs a string up to length n from the database, starting with the given chain. chain may be any length or nil; if it is shorter than b's order, it is padded with nulls as needed, and if it is longer, then only the last (order) elements are taken (but the generated message still contains the earlier ones). If the resulting walk has no more words than the starting chain, then the result is the empty string.

func (*Brain) TalkIn

func (b *Brain) TalkIn(ctx context.Context, channel string, chain []string) string

TalkIn calls Talk with a given channel's tag and limit settings. The result is an empty string if the channel does not exist or has no send tag, or if any other error occurs.

func (*Brain) TrackAffection

func (b *Brain) TrackAffection(ctx context.Context, channel, uid string) (added bool, err error)

TrackAffection begins tracking a user's score in a channel. added is false if the user already has their score tracked.

func (*Brain) Tx

func (b *Brain) Tx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

Tx returns a transaction for the brain's database.

func (*Brain) Update

func (b *Brain) Update(ctx context.Context, channel string) error

Update updates config for a single channel, including user privileges and emotes (the latter for all channels which share a send tag with channel).

func (*Brain) UpdateAll

func (b *Brain) UpdateAll(ctx context.Context) error

UpdateAll fetches configuration from the database for all channels. This can be used to synchronize the brain's configuration with the database during run time.

func (*Brain) Wait

func (b *Brain) Wait(ctx context.Context, channel string)

Wait waits for the channel-specific rate limit, or for the global one if there is no channel-specific one.

Jump to

Keyboard shortcuts

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