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 ¶
- Variables
- func Tokens(msg string) []string
- func UserHash(channel, name string) [32]byte
- type Brain
- func (b *Brain) Activity(ctx context.Context, channel string, f func(float64) float64) (float64, error)
- func (b *Brain) AddAffection(ctx context.Context, channel, uid string, score int64) error
- func (b *Brain) Affection(ctx context.Context, channel, uid string) (int64, error)
- func (b *Brain) Audit(ctx context.Context, msg irc.Message, cmd string) error
- func (b *Brain) Channels() []string
- func (b *Brain) CheckCopypasta(ctx context.Context, msg irc.Message) error
- func (b *Brain) ClearChat(ctx context.Context, channel, user string) error
- func (b *Brain) ClearMsg(ctx context.Context, msgid string) error
- func (b *Brain) ClearPattern(ctx context.Context, channel, pattern string) (int64, error)
- func (b *Brain) ClearSince(ctx context.Context, channel string, since time.Time) error
- func (b *Brain) ClearText(ctx context.Context, channel, text string) error
- func (b *Brain) Close() error
- func (b *Brain) Debug(channel string) (status, block, privs string)
- func (b *Brain) DebugTag(tag string) (emotes, effects []string)
- func (b *Brain) DidSay(ctx context.Context, channel, msg string) (bool, error)
- func (b *Brain) EchoTo(channel string) string
- func (b *Brain) Effect(ctx context.Context, tag string) string
- func (b *Brain) EffectIn(ctx context.Context, channel string) string
- func (b *Brain) Emote(ctx context.Context, tag string) string
- func (b *Brain) EmoteIn(ctx context.Context, channel string) string
- func (b *Brain) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (b *Brain) Join(ctx context.Context, channel, learn, send string) error
- func (b *Brain) Learn(ctx context.Context, msg irc.Message) error
- func (b *Brain) LearnTuple(ctx context.Context, tag string, pre []sql.NullString, suf sql.NullString) (sql.Result, error)
- func (b *Brain) Marriage(ctx context.Context, channel string) (uid string, when time.Time, score int64, err error)
- func (b *Brain) Marry(ctx context.Context, channel, uid string, when time.Time) error
- func (b *Brain) Name() string
- func (b *Brain) Order() int
- func (b *Brain) Privilege(ctx context.Context, channel, nick string, badges []string) (string, error)
- func (b *Brain) Privmsg(ctx context.Context, to, msg string) irc.Message
- func (b *Brain) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (b *Brain) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (b *Brain) Said(ctx context.Context, channel, msg string) error
- func (b *Brain) SendTag(channel string) (string, bool)
- func (b *Brain) SetEchoDir(dir string)
- func (b *Brain) SetFallbackWait(minor, major *rate.Limiter)
- func (b *Brain) SetOnline(channel string, online bool)
- func (b *Brain) SetPriv(ctx context.Context, user, channel, priv string) error
- func (b *Brain) SetWait(ctx context.Context, channel string, limit rate.Limit)
- func (b *Brain) ShouldTalk(ctx context.Context, msg irc.Message, random bool) error
- func (b *Brain) Silence(ctx context.Context, channel string, until time.Time) error
- func (b *Brain) Talk(ctx context.Context, tag string, chain []string, n int) string
- func (b *Brain) TalkIn(ctx context.Context, channel string, chain []string) string
- func (b *Brain) TrackAffection(ctx context.Context, channel, uid string) (added bool, err error)
- func (b *Brain) Tx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (b *Brain) Update(ctx context.Context, channel string) error
- func (b *Brain) UpdateAll(ctx context.Context) error
- func (b *Brain) Wait(ctx context.Context, channel string)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Affection gets a user's score. If the user does not have a score in the given channel, this returns 0, nil.
func (*Brain) CheckCopypasta ¶
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) ClearPattern ¶
ClearPattern unlearns all messages in a channel matching a given pattern and returns the number of messages deleted.
func (*Brain) ClearSince ¶
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) Debug ¶
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 ¶
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) EchoTo ¶
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 ¶
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) Emote ¶
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) Join ¶
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 ¶
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 ¶
Marry sets the marriage for channel. If uid is the empty string, divorce the current marriage in channel instead.
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) QueryRow ¶
QueryRow executes a generic SQL query on the brain's database, expecting at most one resulting row.
func (*Brain) Said ¶
Said marks an arbitrary message as having been recently generated, so that the bot will not re-learn it.
func (*Brain) SendTag ¶
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 ¶
SetEchoDir sets the directory for echoing.
func (*Brain) SetFallbackWait ¶
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 ¶
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) SetWait ¶
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 ¶
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 ¶
Silence sets a silent time on a channel. If until is the zero time, then null is used instead.
func (*Brain) Talk ¶
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 ¶
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 ¶
TrackAffection begins tracking a user's score in a channel. added is false if the user already has their score tracked.
func (*Brain) Update ¶
Update updates config for a single channel, including user privileges and emotes (the latter for all channels which share a send tag with channel).