catbase/plugins/babbler/commands.go

173 lines
3.9 KiB
Go
Raw Normal View History

package babbler
import (
"fmt"
2021-12-21 19:08:20 +00:00
"github.com/rs/zerolog/log"
"strings"
)
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) initializeBabbler(who string) string {
_, err := p.getOrCreateBabbler(who)
if err != nil {
2021-02-01 15:45:41 +00:00
return "babbler initialization failed."
}
2021-02-01 15:45:41 +00:00
return "okay."
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) addToBabbler(babblerName, whatWasSaid string) {
babblerID, err := p.getOrCreateBabbler(babblerName)
if err == nil {
if p.WithGoRoutines {
2021-12-21 19:08:20 +00:00
go func() {
if err := p.addToMarkovChain(babblerID, whatWasSaid); err != nil {
log.Error().Err(err).Msg("addToMarkovChain")
}
}()
} else {
2021-12-21 19:08:20 +00:00
err = p.addToMarkovChain(babblerID, whatWasSaid)
if err != nil {
log.Error().Err(err).Msg("error adding to chain")
}
}
2021-12-21 19:08:20 +00:00
} else {
log.Error().Err(err).Msg("error getting or creating babbler")
}
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) getBabble(who string, tokens []string) string {
_, err := p.getBabbler(who)
if err != nil {
if err == NO_BABBLER {
2021-12-21 19:08:20 +00:00
return fmt.Sprintf("%s babbler not found.", who)
}
2021-12-21 19:08:20 +00:00
log.Debug().Err(err).Msg("error getting babbler")
} else {
var saying string
2021-02-01 15:45:41 +00:00
if len(tokens) == 0 {
saying, err = p.babble(who)
} else {
2021-02-01 15:45:41 +00:00
saying, err = p.babbleSeed(who, tokens)
}
if err != nil {
if err == SAID_NOTHING {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens, " "))
2021-12-21 19:08:20 +00:00
} else {
log.Error().Err(err).Msgf("error babbling")
return fmt.Sprintf("babbler encountered an error for %s", who)
}
} else if saying != "" {
2021-02-01 15:45:41 +00:00
return saying
2021-12-21 19:08:20 +00:00
} else {
return "for some reason the saying was empty"
}
}
2021-12-21 19:08:20 +00:00
return "IDK how we got here"
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) getBabbleWithSuffix(who string, tokens []string) string {
_, err := p.getBabbler(who)
if err != nil {
if err == NO_BABBLER {
2017-06-16 19:02:28 +00:00
// return fmt.Sprintf("%s babbler not found.", who), true
2021-02-01 15:45:41 +00:00
return ""
}
} else {
2021-02-01 15:45:41 +00:00
saying, err := p.babbleSeedSuffix(who, tokens)
if err != nil {
if err == SAID_NOTHING {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens, " "))
}
} else if saying != "" {
2021-02-01 15:45:41 +00:00
return saying
}
}
2021-02-01 15:45:41 +00:00
return ""
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) getBabbleWithBookends(who string, start, end []string) string {
_, err := p.getBabbler(who)
if err != nil {
if err == NO_BABBLER {
2017-06-16 19:02:28 +00:00
// return fmt.Sprintf("%s babbler not found.", who), true
2021-02-01 15:45:41 +00:00
return ""
}
} else {
2021-02-01 15:45:41 +00:00
saying, err := p.babbleSeedBookends(who, start, end)
if err != nil {
if err == SAID_NOTHING {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s hasn't said anything yet.", who)
} else if err == NEVER_SAID {
2021-02-01 15:45:41 +00:00
seeds := append(start[1:], "...")
seeds = append(seeds, end...)
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s never said '%s'", who, strings.Join(seeds, " "))
}
} else if saying != "" {
2021-02-01 15:45:41 +00:00
return saying
}
}
2021-02-01 15:45:41 +00:00
return ""
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) batchLearn(tokens []string) string {
who := tokens[3]
babblerId, err := p.getOrCreateBabbler(who)
if err != nil {
2021-02-01 15:45:41 +00:00
return "batch learn failed."
}
body := strings.Join(tokens[4:], " ")
body = strings.ToLower(body)
for _, a := range strings.Split(body, ".") {
for _, b := range strings.Split(a, "!") {
for _, c := range strings.Split(b, "?") {
for _, d := range strings.Split(c, "\n") {
trimmed := strings.TrimSpace(d)
if trimmed != "" {
p.addToMarkovChain(babblerId, trimmed)
}
}
}
}
}
2021-02-01 15:45:41 +00:00
return "phew that was tiring."
}
2021-02-01 15:45:41 +00:00
func (p *BabblerPlugin) merge(who, into string) string {
if who == into {
2021-02-01 15:45:41 +00:00
return "that's annoying. stop it."
}
whoBabbler, err := p.getBabbler(who)
if err != nil {
if err == NO_BABBLER {
2021-02-01 15:45:41 +00:00
return fmt.Sprintf("%s babbler not found.", who)
}
2021-02-01 15:45:41 +00:00
return "merge failed."
}
intoBabbler, err := p.getOrCreateBabbler(into)
if err != nil {
2021-02-01 15:45:41 +00:00
return "merge failed."
}
err = p.mergeBabblers(intoBabbler, whoBabbler, into, who)
if err != nil {
2021-02-01 15:45:41 +00:00
return "merge failed."
}
2021-02-01 15:45:41 +00:00
return "mooooiggged"
}