mirror of https://github.com/velour/catbase.git
Compare commits
No commits in common. "a6847a996fcf9ac13f1a55a670147677992d8026" and "00dae33e8a5bc35002b14004475d49f96991a459" have entirely different histories.
a6847a996f
...
00dae33e8a
11
bot/bot.go
11
bot/bot.go
|
@ -230,17 +230,6 @@ func (b *bot) RegisterFilter(name string, f func(string) string) {
|
||||||
b.filters[name] = f
|
b.filters[name] = f
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterTable registers multiple regex handlers at a time
|
|
||||||
func (b *bot) RegisterTable(p Plugin, handlers HandlerTable) {
|
|
||||||
for _, h := range handlers {
|
|
||||||
if h.IsCmd {
|
|
||||||
b.RegisterRegexCmd(p, h.Kind, h.Regex, h.Handler)
|
|
||||||
} else {
|
|
||||||
b.RegisterRegex(p, h.Kind, h.Regex, h.Handler)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterRegex does what register does, but with a matcher
|
// RegisterRegex does what register does, but with a matcher
|
||||||
func (b *bot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, resp ResponseHandler) {
|
func (b *bot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, resp ResponseHandler) {
|
||||||
t := reflect.TypeOf(p).String()
|
t := reflect.TypeOf(p).String()
|
||||||
|
|
|
@ -57,15 +57,15 @@ func (b *bot) runCallback(conn Connector, plugin Plugin, evt Kind, message msg.M
|
||||||
t := reflect.TypeOf(plugin).String()
|
t := reflect.TypeOf(plugin).String()
|
||||||
for r, cbs := range b.callbacks[t][evt] {
|
for r, cbs := range b.callbacks[t][evt] {
|
||||||
if r.MatchString(message.Body) {
|
if r.MatchString(message.Body) {
|
||||||
req := Request{
|
|
||||||
Conn: conn,
|
|
||||||
Kind: evt,
|
|
||||||
Msg: message,
|
|
||||||
Values: ParseValues(r, message.Body),
|
|
||||||
Args: args,
|
|
||||||
}
|
|
||||||
for _, cb := range cbs {
|
for _, cb := range cbs {
|
||||||
if cb(req) {
|
resp := Request{
|
||||||
|
Conn: conn,
|
||||||
|
Kind: evt,
|
||||||
|
Msg: message,
|
||||||
|
Values: ParseValues(r, message.Body),
|
||||||
|
Args: args,
|
||||||
|
}
|
||||||
|
if cb(resp) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,14 +61,6 @@ type Callback func(Connector, Kind, msg.Message, ...interface{}) bool
|
||||||
type ResponseHandler func(Request) bool
|
type ResponseHandler func(Request) bool
|
||||||
type CallbackMap map[string]map[Kind]map[*regexp.Regexp][]ResponseHandler
|
type CallbackMap map[string]map[Kind]map[*regexp.Regexp][]ResponseHandler
|
||||||
|
|
||||||
type HandlerSpec struct {
|
|
||||||
Kind Kind
|
|
||||||
IsCmd bool
|
|
||||||
Regex *regexp.Regexp
|
|
||||||
Handler ResponseHandler
|
|
||||||
}
|
|
||||||
type HandlerTable []HandlerSpec
|
|
||||||
|
|
||||||
type RegexValues map[string]string
|
type RegexValues map[string]string
|
||||||
|
|
||||||
// b interface serves to allow mocking of the actual bot
|
// b interface serves to allow mocking of the actual bot
|
||||||
|
@ -98,10 +90,6 @@ type Bot interface {
|
||||||
// The Kind arg should be one of bot.Message/Reply/Action/etc
|
// The Kind arg should be one of bot.Message/Reply/Action/etc
|
||||||
Receive(Connector, Kind, msg.Message, ...interface{}) bool
|
Receive(Connector, Kind, msg.Message, ...interface{}) bool
|
||||||
|
|
||||||
// Register a set of plugin callbacks
|
|
||||||
// Kind will be matched to the event for the callback
|
|
||||||
RegisterTable(Plugin, HandlerTable)
|
|
||||||
|
|
||||||
// Register a plugin callback
|
// Register a plugin callback
|
||||||
// Kind will be matched to the event for the callback
|
// Kind will be matched to the event for the callback
|
||||||
RegisterRegex(Plugin, Kind, *regexp.Regexp, ResponseHandler)
|
RegisterRegex(Plugin, Kind, *regexp.Regexp, ResponseHandler)
|
||||||
|
|
|
@ -54,7 +54,6 @@ func (mb *MockBot) Send(c Connector, kind Kind, args ...interface{}) (string, er
|
||||||
}
|
}
|
||||||
func (mb *MockBot) AddPlugin(f Plugin) {}
|
func (mb *MockBot) AddPlugin(f Plugin) {}
|
||||||
func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {}
|
func (mb *MockBot) Register(p Plugin, kind Kind, cb Callback) {}
|
||||||
func (mb *MockBot) RegisterTable(p Plugin, hs HandlerTable) {}
|
|
||||||
func (mb *MockBot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
|
func (mb *MockBot) RegisterRegex(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
|
||||||
func (mb *MockBot) RegisterRegexCmd(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
|
func (mb *MockBot) RegisterRegexCmd(p Plugin, kind Kind, r *regexp.Regexp, h ResponseHandler) {}
|
||||||
func (mb *MockBot) RegisterWeb(_, _ string) {}
|
func (mb *MockBot) RegisterWeb(_, _ string) {}
|
||||||
|
|
|
@ -30,37 +30,16 @@ type AdminPlugin struct {
|
||||||
quiet bool
|
quiet bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new AdminPlugin with the Plugin interface
|
// NewAdminPlugin creates a new AdminPlugin with the Plugin interface
|
||||||
func New(b bot.Bot) *AdminPlugin {
|
func New(b bot.Bot) *AdminPlugin {
|
||||||
p := &AdminPlugin{
|
p := &AdminPlugin{
|
||||||
bot: b,
|
bot: b,
|
||||||
db: b.DB(),
|
db: b.DB(),
|
||||||
cfg: b.Config(),
|
cfg: b.Config(),
|
||||||
}
|
}
|
||||||
|
b.Register(p, bot.Message, p.message)
|
||||||
b.RegisterRegex(p, bot.Message, comeBackRegex, p.comeBackCmd)
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, shutupRegex, p.shutupCmd)
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, addBlacklistRegex, p.isAdmin(p.addBlacklistCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, rmBlacklistRegex, p.isAdmin(p.rmBlacklistCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, addWhitelistRegex, p.isAdmin(p.addWhitelistCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, rmWhitelistRegex, p.isAdmin(p.rmWhitelistCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, allWhitelistRegex, p.isAdmin(p.allWhitelistCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, allUnWhitelistRegex, p.isAdmin(p.allUnWhitelistCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, getWhitelistRegex, p.isAdmin(p.getWhitelistCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, getPluginsRegex, p.isAdmin(p.getPluginsCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, rebootRegex, p.isAdmin(p.rebootCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, passwordRegex, p.passwordCmd)
|
|
||||||
b.RegisterRegex(p, bot.Message, variableSetRegex, p.variableSetCmd)
|
|
||||||
b.RegisterRegex(p, bot.Message, variableUnSetRegex, p.variableUnSetCmd)
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, unsetConfigRegex, p.isAdmin(p.unsetConfigCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, setConfigRegex, p.isAdmin(p.setConfigCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, pushConfigRegex, p.isAdmin(p.pushConfigCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, setKeyConfigRegex, p.isAdmin(p.setKeyConfigCmd))
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, getConfigRegex, p.isAdmin(p.getConfigCmd))
|
|
||||||
|
|
||||||
b.Register(p, bot.Help, p.help)
|
b.Register(p, bot.Help, p.help)
|
||||||
p.registerWeb()
|
p.registerWeb()
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,279 +51,270 @@ var forbiddenKeys = map[string]bool{
|
||||||
"meme.memes": true,
|
"meme.memes": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
var shutupRegex = regexp.MustCompile(`(?i)^shut up$`)
|
var addBlacklist = regexp.MustCompile(`(?i)disable plugin (.*)`)
|
||||||
var comeBackRegex = regexp.MustCompile(`(?i)^come back$`)
|
var rmBlacklist = regexp.MustCompile(`(?i)enable plugin (.*)`)
|
||||||
|
|
||||||
var addBlacklistRegex = regexp.MustCompile(`(?i)disable plugin (?P<plugin>.*)$`)
|
var addWhitelist = regexp.MustCompile(`(?i)^whitelist plugin (.*)`)
|
||||||
var rmBlacklistRegex = regexp.MustCompile(`(?i)enable plugin (?P<plugin>.*)$`)
|
var rmWhitelist = regexp.MustCompile(`(?i)^unwhitelist plugin (.*)`)
|
||||||
|
var allWhitelist = regexp.MustCompile(`(?i)^whitelist all`)
|
||||||
|
var allUnwhitelist = regexp.MustCompile(`(?i)^unwhitelist all`)
|
||||||
|
var getWhitelist = regexp.MustCompile(`(?i)^list whitelist`)
|
||||||
|
var getPlugins = regexp.MustCompile(`(?i)^list plugins`)
|
||||||
|
|
||||||
var addWhitelistRegex = regexp.MustCompile(`(?i)^whitelist plugin (?P<plugin>.*)$`)
|
// Message responds to the bot hook on recieving messages.
|
||||||
var rmWhitelistRegex = regexp.MustCompile(`(?i)^unwhitelist plugin (?P<plugin>.*)$`)
|
// This function returns true if the plugin responds in a meaningful way to the users message.
|
||||||
var allWhitelistRegex = regexp.MustCompile(`(?i)^whitelist all$`)
|
// Otherwise, the function returns false and the bot continues execution of other plugins.
|
||||||
var allUnWhitelistRegex = regexp.MustCompile(`(?i)^unwhitelist all$`)
|
func (p *AdminPlugin) message(conn bot.Connector, k bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||||
var getWhitelistRegex = regexp.MustCompile(`(?i)^list whitelist$`)
|
body := message.Body
|
||||||
var getPluginsRegex = regexp.MustCompile(`(?i)^list plugins$`)
|
|
||||||
|
|
||||||
var rebootRegex = regexp.MustCompile(`^reboot$`)
|
if p.quiet && message.Body != "come back" {
|
||||||
var passwordRegex = regexp.MustCompile(`(?i)^password$`)
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
var variableSetRegex = regexp.MustCompile(`(?i)^\$(?P<var>\S+)\s?=\s?(?P<value>\S+)$`)
|
if len(body) > 0 && body[0] == '$' {
|
||||||
var variableUnSetRegex = regexp.MustCompile(`(?i)^\$(?P<var>\S+)\s?!=\s?(?P<value>\S+)$`)
|
return p.handleVariables(conn, message)
|
||||||
|
}
|
||||||
|
|
||||||
var unsetConfigRegex = regexp.MustCompile(`(?i)^unset (?P<key>\S+)$`)
|
if !message.Command {
|
||||||
var setConfigRegex = regexp.MustCompile(`(?i)^set (?P<key>\S+) (?P<value>.*)$`)
|
return false
|
||||||
var pushConfigRegex = regexp.MustCompile(`(?i)^push (?P<key>\S+) (?P<value>.*)$`)
|
}
|
||||||
var setKeyConfigRegex = regexp.MustCompile(`(?i)^setkey (?P<key>\S+) (?P<name>\S+) (?P<value>.*)$`)
|
|
||||||
var getConfigRegex = regexp.MustCompile(`(?i)^get (?P<key>\S+)$`)
|
|
||||||
|
|
||||||
func (p *AdminPlugin) isAdmin(rh bot.ResponseHandler) bot.ResponseHandler {
|
if p.quiet && message.Body == "come back" {
|
||||||
return func(r bot.Request) bool {
|
p.quiet = false
|
||||||
if !p.bot.CheckAdmin(r.Msg.User.Name) {
|
p.bot.SetQuiet(false)
|
||||||
log.Debug().Msgf("User %s is not an admin", r.Msg.User.Name)
|
backMsg := p.bot.Config().Get("admin.comeback", "Okay, I'm back.")
|
||||||
return false
|
p.bot.Send(conn, bot.Message, message.Channel, backMsg)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.ToLower(body) == "shut up" {
|
||||||
|
dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute
|
||||||
|
log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur))
|
||||||
|
leaveMsg := p.bot.Config().Get("admin.shutup", "Okay. I'll be back later.")
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, leaveMsg)
|
||||||
|
p.quiet = true
|
||||||
|
p.bot.SetQuiet(true)
|
||||||
|
go func() {
|
||||||
|
select {
|
||||||
|
case <-time.After(dur):
|
||||||
|
p.quiet = false
|
||||||
|
p.bot.SetQuiet(false)
|
||||||
|
log.Info().Msg("Waking up from nap.")
|
||||||
|
backMsg := p.bot.Config().Get("admin.backmsg", "I'm back, bitches.")
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, backMsg)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if !p.bot.CheckAdmin(message.User.Name) {
|
||||||
|
log.Debug().Msgf("User %s is not an admin", message.User.Name)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.ToLower(body) == "reboot" {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, "brb")
|
||||||
|
log.Info().Msgf("Got reboot command")
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
if addBlacklist.MatchString(body) {
|
||||||
|
submatches := addBlacklist.FindStringSubmatch(message.Body)
|
||||||
|
plugin := submatches[1]
|
||||||
|
if err := p.addBlacklist(message.Channel, plugin); err != nil {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't add that item: %s", err))
|
||||||
|
log.Error().Err(err).Msgf("error adding blacklist item")
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
return rh(r)
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s disabled. Use `!enable plugin %s` to re-enable it.", plugin, plugin))
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) shutupCmd(r bot.Request) bool {
|
if rmBlacklist.MatchString(body) {
|
||||||
dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute
|
submatches := rmBlacklist.FindStringSubmatch(message.Body)
|
||||||
log.Info().Msgf("Going to sleep for %v, %v", dur, time.Now().Add(dur))
|
plugin := submatches[1]
|
||||||
leaveMsg := p.bot.Config().Get("admin.shutup", "Okay. I'll be back later.")
|
if err := p.rmBlacklist(message.Channel, plugin); err != nil {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, leaveMsg)
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't remove that item: %s", err))
|
||||||
p.quiet = true
|
log.Error().Err(err).Msgf("error removing blacklist item")
|
||||||
p.bot.SetQuiet(true)
|
return true
|
||||||
go func() {
|
|
||||||
select {
|
|
||||||
case <-time.After(dur):
|
|
||||||
p.quiet = false
|
|
||||||
p.bot.SetQuiet(false)
|
|
||||||
log.Info().Msg("Waking up from nap.")
|
|
||||||
backMsg := p.bot.Config().Get("admin.backmsg", "I'm back, bitches.")
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, backMsg)
|
|
||||||
}
|
}
|
||||||
}()
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s enabled. Use `!disable plugin %s` to disable it.", plugin, plugin))
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) comeBackCmd(r bot.Request) bool {
|
|
||||||
p.quiet = false
|
|
||||||
p.bot.SetQuiet(false)
|
|
||||||
backMsg := p.bot.Config().Get("admin.comeback", "Okay, I'm back.")
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, backMsg)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) addBlacklistCmd(r bot.Request) bool {
|
|
||||||
plugin := r.Values["plugin"]
|
|
||||||
if err := p.addBlacklist(r.Msg.Channel, plugin); err != nil {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't add that item: %s", err))
|
|
||||||
log.Error().Err(err).Msgf("error adding blacklist item")
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s disabled. Use `!enable plugin %s` to re-enable it.", plugin, plugin))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) rmBlacklistCmd(r bot.Request) bool {
|
if allWhitelist.MatchString(body) {
|
||||||
plugin := r.Values["plugin"]
|
plugins := p.bot.GetPluginNames()
|
||||||
if err := p.rmBlacklist(r.Msg.Channel, plugin); err != nil {
|
for _, plugin := range plugins {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't remove that item: %s", err))
|
if err := p.addWhitelist(plugin); err != nil {
|
||||||
log.Error().Err(err).Msgf("error removing blacklist item")
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't whitelist that item: %s", err))
|
||||||
|
log.Error().Err(err).Msgf("error adding whitelist item")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, "Enabled all plugins")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s enabled. Use `!disable plugin %s` to disable it.", plugin, plugin))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) addWhitelistCmd(r bot.Request) bool {
|
if allUnwhitelist.MatchString(body) {
|
||||||
plugin := r.Values["plugin"]
|
plugins := p.bot.GetPluginNames()
|
||||||
if err := p.addWhitelist(plugin); err != nil {
|
for _, plugin := range plugins {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't whitelist that item: %s", err))
|
if plugin == "admin" {
|
||||||
log.Error().Err(err).Msgf("error adding whitelist item")
|
continue
|
||||||
|
}
|
||||||
|
if err := p.rmWhitelist(plugin); err != nil {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't unwhitelist that item: %s", err))
|
||||||
|
log.Error().Err(err).Msgf("error removing whitelist item")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, "Disabled all plugins")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s enabled. Use `!unwhitelist plugin %s` to disable it.", plugin, plugin))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) rmWhitelistCmd(r bot.Request) bool {
|
if addWhitelist.MatchString(body) {
|
||||||
plugin := r.Values["plugin"]
|
submatches := addWhitelist.FindStringSubmatch(message.Body)
|
||||||
if err := p.rmWhitelist(plugin); err != nil {
|
plugin := submatches[1]
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't unwhitelist that item: %s", err))
|
|
||||||
log.Error().Err(err).Msgf("error removing whitelist item")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s disabled. Use `!whitelist plugin %s` to enable it.", plugin, plugin))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) allWhitelistCmd(r bot.Request) bool {
|
|
||||||
plugins := p.bot.GetPluginNames()
|
|
||||||
for _, plugin := range plugins {
|
|
||||||
if err := p.addWhitelist(plugin); err != nil {
|
if err := p.addWhitelist(plugin); err != nil {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't whitelist that item: %s", err))
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't whitelist that item: %s", err))
|
||||||
log.Error().Err(err).Msgf("error adding whitelist item")
|
log.Error().Err(err).Msgf("error adding whitelist item")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s enabled. Use `!unwhitelist plugin %s` to disable it.", plugin, plugin))
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Enabled all plugins")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) allUnWhitelistCmd(r bot.Request) bool {
|
if rmWhitelist.MatchString(body) {
|
||||||
plugins := p.bot.GetPluginNames()
|
submatches := rmWhitelist.FindStringSubmatch(message.Body)
|
||||||
for _, plugin := range plugins {
|
plugin := submatches[1]
|
||||||
if plugin == "admin" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if err := p.rmWhitelist(plugin); err != nil {
|
if err := p.rmWhitelist(plugin); err != nil {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("I couldn't unwhitelist that item: %s", err))
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("I couldn't unwhitelist that item: %s", err))
|
||||||
log.Error().Err(err).Msgf("error removing whitelist item")
|
log.Error().Err(err).Msgf("error removing whitelist item")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s disabled. Use `!whitelist plugin %s` to enable it.", plugin, plugin))
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Disabled all plugins")
|
|
||||||
return true
|
if getWhitelist.MatchString(body) {
|
||||||
|
list := p.bot.GetWhitelist()
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Whitelist: %v", list))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if getPlugins.MatchString(body) {
|
||||||
|
plugins := p.bot.GetPluginNames()
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Plugins: %v", plugins))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.ToLower(body) == "password" {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, p.bot.GetPassword())
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
verbs := map[string]bool{
|
||||||
|
"set": true,
|
||||||
|
"push": true,
|
||||||
|
"setkey": true,
|
||||||
|
}
|
||||||
|
|
||||||
|
parts := strings.Split(body, " ")
|
||||||
|
if verbs[parts[0]] && len(parts) > 2 && forbiddenKeys[parts[1]] {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, "You cannot access that key")
|
||||||
|
return true
|
||||||
|
} else if parts[0] == "unset" && len(parts) > 1 {
|
||||||
|
if err := p.cfg.Unset(parts[1]); err != nil {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Unset error: %s", err))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Unset %s", parts[1]))
|
||||||
|
return true
|
||||||
|
} else if parts[0] == "set" && len(parts) > 2 {
|
||||||
|
if err := p.cfg.Set(parts[1], strings.Join(parts[2:], " ")); err != nil {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set error: %s", err))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1]))
|
||||||
|
return true
|
||||||
|
} else if parts[0] == "push" && len(parts) > 2 {
|
||||||
|
items := p.cfg.GetArray(parts[1], []string{})
|
||||||
|
items = append(items, strings.Join(parts[2:], ""))
|
||||||
|
if err := p.cfg.Set(parts[1], strings.Join(items, ";;")); err != nil {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set error: %s", err))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1]))
|
||||||
|
return true
|
||||||
|
} else if parts[0] == "setkey" && len(parts) > 3 {
|
||||||
|
items := p.cfg.GetMap(parts[1], map[string]string{})
|
||||||
|
items[parts[2]] = strings.Join(parts[3:], " ")
|
||||||
|
if err := p.cfg.SetMap(parts[1], items); err != nil {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set error: %s", err))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1]))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if parts[0] == "get" && len(parts) == 2 && forbiddenKeys[parts[1]] {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, "You cannot access that key")
|
||||||
|
return true
|
||||||
|
} else if parts[0] == "get" && len(parts) == 2 {
|
||||||
|
v := p.cfg.Get(parts[1], "<unknown>")
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, fmt.Sprintf("%s: %s", parts[1], v))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *AdminPlugin) getWhitelistCmd(r bot.Request) bool {
|
func (p *AdminPlugin) handleVariables(conn bot.Connector, message msg.Message) bool {
|
||||||
list := p.bot.GetWhitelist()
|
if parts := strings.SplitN(message.Body, "!=", 2); len(parts) == 2 {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Whitelist: %v", list))
|
variable := strings.ToLower(strings.TrimSpace(parts[0]))
|
||||||
return true
|
value := strings.TrimSpace(parts[1])
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) getPluginsCmd(r bot.Request) bool {
|
_, err := p.db.Exec(`delete from variables where name=? and value=?`, variable, value)
|
||||||
plugins := p.bot.GetPluginNames()
|
if err != nil {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Plugins: %v", plugins))
|
p.bot.Send(conn, bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.")
|
||||||
return true
|
log.Error().Err(err)
|
||||||
}
|
} else {
|
||||||
|
p.bot.Send(conn, bot.Message, message.Channel, "Removed.")
|
||||||
|
}
|
||||||
|
|
||||||
func (p *AdminPlugin) rebootCmd(r bot.Request) bool {
|
return true
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "brb")
|
}
|
||||||
log.Info().Msgf("Got reboot command")
|
|
||||||
os.Exit(0)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) passwordCmd(r bot.Request) bool {
|
parts := strings.SplitN(message.Body, "=", 2)
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, p.bot.GetPassword())
|
if len(parts) != 2 {
|
||||||
return true
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *AdminPlugin) variableSetCmd(r bot.Request) bool {
|
variable := strings.ToLower(strings.TrimSpace(parts[0]))
|
||||||
variable := strings.ToLower(r.Values["var"])
|
value := strings.TrimSpace(parts[1])
|
||||||
value := r.Values["value"]
|
|
||||||
|
|
||||||
var count int64
|
var count int64
|
||||||
row := p.db.QueryRow(`select count(*) from variables where value = ?`, variable, value)
|
row := p.db.QueryRow(`select count(*) from variables where value = ?`, variable, value)
|
||||||
err := row.Scan(&count)
|
err := row.Scan(&count)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I'm broke and need attention in my variable creation code.")
|
p.bot.Send(conn, bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.")
|
||||||
log.Error().Err(err)
|
log.Error().Err(err)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if count > 0 {
|
if count > 0 {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I've already got that one.")
|
p.bot.Send(conn, bot.Message, message.Channel, "I've already got that one.")
|
||||||
} else {
|
} else {
|
||||||
_, err := p.db.Exec(`INSERT INTO variables (name, value) VALUES (?, ?)`, variable, value)
|
_, err := p.db.Exec(`INSERT INTO variables (name, value) VALUES (?, ?)`, variable, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I'm broke and need attention in my variable creation code.")
|
p.bot.Send(conn, bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.")
|
||||||
log.Error().Err(err)
|
log.Error().Err(err)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Added.")
|
p.bot.Send(conn, bot.Message, message.Channel, "Added.")
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *AdminPlugin) variableUnSetCmd(r bot.Request) bool {
|
|
||||||
variable := strings.ToLower(r.Values["var"])
|
|
||||||
value := r.Values["value"]
|
|
||||||
|
|
||||||
_, err := p.db.Exec(`delete from variables where name=? and value=?`, variable, value)
|
|
||||||
if err != nil {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "I'm broke and need attention in my variable creation code.")
|
|
||||||
log.Error().Err(err)
|
|
||||||
} else {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "Removed.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) unsetConfigCmd(r bot.Request) bool {
|
|
||||||
key := r.Values["key"]
|
|
||||||
if forbiddenKeys[key] {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if err := p.cfg.Unset(key); err != nil {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Unset error: %s", err))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Unset %s", key))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) setConfigCmd(r bot.Request) bool {
|
|
||||||
key, value := r.Values["key"], r.Values["value"]
|
|
||||||
if forbiddenKeys[key] {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if err := p.cfg.Set(key, value); err != nil {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set error: %s", err))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set %s", key))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) pushConfigCmd(r bot.Request) bool {
|
|
||||||
key, value := r.Values["key"], r.Values["value"]
|
|
||||||
if forbiddenKeys[key] {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
items := p.cfg.GetArray(key, []string{})
|
|
||||||
items = append(items, value)
|
|
||||||
if err := p.cfg.Set(key, strings.Join(items, ";;")); err != nil {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set error: %s", err))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set %s", key))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) setKeyConfigCmd(r bot.Request) bool {
|
|
||||||
key, name, value := r.Values["key"], r.Values["name"], r.Values["value"]
|
|
||||||
if forbiddenKeys[key] {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
items := p.cfg.GetMap(key, map[string]string{})
|
|
||||||
items[name] = value
|
|
||||||
if err := p.cfg.SetMap(key, items); err != nil {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set error: %s", err))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Set %s", key))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *AdminPlugin) getConfigCmd(r bot.Request) bool {
|
|
||||||
key := r.Values["key"]
|
|
||||||
if forbiddenKeys[key] {
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, "You cannot access that key")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
v := p.cfg.Get(key, "<unknown>")
|
|
||||||
p.bot.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("%s: %s", key, v))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Help responds to help requests. Every plugin must implement a help function.
|
// Help responds to help requests. Every plugin must implement a help function.
|
||||||
func (p *AdminPlugin) help(conn bot.Connector, kind bot.Kind, m msg.Message, args ...interface{}) bool {
|
func (p *AdminPlugin) help(conn bot.Connector, kind bot.Kind, m msg.Message, args ...interface{}) bool {
|
||||||
p.bot.Send(conn, bot.Message, m.Channel, "This does super secret things that you're not allowed to know about.")
|
p.bot.Send(conn, bot.Message, m.Channel, "This does super secret things that you're not allowed to know about.")
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
@ -29,30 +28,24 @@ func setup(t *testing.T) (*AdminPlugin, *bot.MockBot) {
|
||||||
return a, mb
|
return a, mb
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeMessage(payload string, r *regexp.Regexp) bot.Request {
|
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
|
||||||
isCmd := strings.HasPrefix(payload, "!")
|
isCmd := strings.HasPrefix(payload, "!")
|
||||||
if isCmd {
|
if isCmd {
|
||||||
payload = payload[1:]
|
payload = payload[1:]
|
||||||
}
|
}
|
||||||
c := cli.CliPlugin{}
|
c := cli.CliPlugin{}
|
||||||
values := bot.ParseValues(r, payload)
|
return &c, bot.Message, msg.Message{
|
||||||
return bot.Request{
|
User: &user.User{Name: "admin"},
|
||||||
Conn: &c,
|
Channel: "test",
|
||||||
Kind: bot.Message,
|
Body: payload,
|
||||||
Values: values,
|
Command: isCmd,
|
||||||
Msg: msg.Message{
|
|
||||||
User: &user.User{Name: "admin"},
|
|
||||||
Channel: "test",
|
|
||||||
Body: payload,
|
|
||||||
Command: isCmd,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSet(t *testing.T) {
|
func TestSet(t *testing.T) {
|
||||||
a, mb := setup(t)
|
a, mb := setup(t)
|
||||||
expected := "test value"
|
expected := "test value"
|
||||||
a.setConfigCmd(makeMessage("!set test.key "+expected, setConfigRegex))
|
a.message(makeMessage("!set test.key " + expected))
|
||||||
actual := mb.Config().Get("test.key", "ERR")
|
actual := mb.Config().Get("test.key", "ERR")
|
||||||
assert.Equal(t, expected, actual)
|
assert.Equal(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
@ -61,7 +54,7 @@ func TestGetValue(t *testing.T) {
|
||||||
a, mb := setup(t)
|
a, mb := setup(t)
|
||||||
expected := "value"
|
expected := "value"
|
||||||
mb.Config().Set("test.key", "value")
|
mb.Config().Set("test.key", "value")
|
||||||
a.getConfigCmd(makeMessage("!get test.key", getConfigRegex))
|
a.message(makeMessage("!get test.key"))
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.Contains(t, mb.Messages[0], expected)
|
assert.Contains(t, mb.Messages[0], expected)
|
||||||
}
|
}
|
||||||
|
@ -69,7 +62,7 @@ func TestGetValue(t *testing.T) {
|
||||||
func TestGetEmpty(t *testing.T) {
|
func TestGetEmpty(t *testing.T) {
|
||||||
a, mb := setup(t)
|
a, mb := setup(t)
|
||||||
expected := "test.key: <unknown>"
|
expected := "test.key: <unknown>"
|
||||||
a.getConfigCmd(makeMessage("!get test.key", getConfigRegex))
|
a.message(makeMessage("!get test.key"))
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.Equal(t, expected, mb.Messages[0])
|
assert.Equal(t, expected, mb.Messages[0])
|
||||||
}
|
}
|
||||||
|
@ -77,7 +70,7 @@ func TestGetEmpty(t *testing.T) {
|
||||||
func TestGetForbidden(t *testing.T) {
|
func TestGetForbidden(t *testing.T) {
|
||||||
a, mb := setup(t)
|
a, mb := setup(t)
|
||||||
expected := "cannot access"
|
expected := "cannot access"
|
||||||
a.getConfigCmd(makeMessage("!get slack.token", getConfigRegex))
|
a.message(makeMessage("!get slack.token"))
|
||||||
assert.Len(t, mb.Messages, 1)
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.Contains(t, mb.Messages[0], expected)
|
assert.Contains(t, mb.Messages[0], expected)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package aoc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -11,6 +10,7 @@ import (
|
||||||
"code.chrissexton.org/cws/getaoc"
|
"code.chrissexton.org/cws/getaoc"
|
||||||
|
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
|
"github.com/velour/catbase/bot/msg"
|
||||||
"github.com/velour/catbase/config"
|
"github.com/velour/catbase/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,82 +24,91 @@ func New(b bot.Bot) *AOC {
|
||||||
b: b,
|
b: b,
|
||||||
c: b.Config(),
|
c: b.Config(),
|
||||||
}
|
}
|
||||||
b.RegisterRegexCmd(aoc, bot.Message, aocRegex, aoc.aocCmd)
|
b.Register(aoc, bot.Message, aoc.message)
|
||||||
return aoc
|
return aoc
|
||||||
}
|
}
|
||||||
|
|
||||||
var aocRegex = regexp.MustCompile(`(?i)^aoc\s?(?P<year>\S+)?$`)
|
func (p *AOC) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||||
|
if !message.Command {
|
||||||
func (p *AOC) aocCmd(r bot.Request) bool {
|
return false
|
||||||
year := time.Now().Year()
|
|
||||||
if time.Now().Month() < 11 {
|
|
||||||
year--
|
|
||||||
}
|
}
|
||||||
if r.Values["year"] != "" {
|
cleaned := strings.TrimSpace(strings.ToLower(message.Body))
|
||||||
year, _ = strconv.Atoi(r.Values["year"])
|
fields := strings.Split(cleaned, " ")
|
||||||
}
|
if strings.HasPrefix(cleaned, "aoc") {
|
||||||
boardId := p.c.GetInt("aoc.board", 0)
|
year := time.Now().Year()
|
||||||
var err error
|
if len(fields) > 1 {
|
||||||
|
year, _ = strconv.Atoi(fields[1])
|
||||||
|
}
|
||||||
|
boardId := p.c.GetInt("aoc.board", 0)
|
||||||
|
var err error
|
||||||
|
if len(fields) > 2 {
|
||||||
|
boardId, err = strconv.Atoi(fields[2])
|
||||||
|
if err != nil {
|
||||||
|
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Error getting leaderboard: %s", err))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
session := p.c.Get("aoc.session", "")
|
||||||
|
if session == "" {
|
||||||
|
p.b.Send(c, bot.Message, message.Channel, "Error getting leaderboard: must have a session ID.")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if boardId == 0 {
|
||||||
|
p.b.Send(c, bot.Message, message.Channel, "Error getting leaderboard: must have a board ID.")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
board, err := getaoc.GetLeaderboard(session, year, boardId)
|
||||||
|
if err != nil {
|
||||||
|
p.b.Send(c, bot.Message, message.Channel, fmt.Sprintf("Error getting leaderboard: %s", err))
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
session := p.c.Get("aoc.session", "")
|
members := []getaoc.Member{}
|
||||||
if session == "" {
|
for _, m := range board.Members {
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Error getting leaderboard: must have a session ID.")
|
members = append(members, m)
|
||||||
|
}
|
||||||
|
sort.Slice(members, func(i, j int) bool {
|
||||||
|
return members[i].LocalScore > members[j].LocalScore
|
||||||
|
})
|
||||||
|
|
||||||
|
gold, silver, bronze := -1, -1, -1
|
||||||
|
goldID, silverID, bronzeID := "", "", ""
|
||||||
|
for _, m := range members {
|
||||||
|
if m.LocalScore > gold {
|
||||||
|
gold = m.LocalScore
|
||||||
|
goldID = m.ID
|
||||||
|
}
|
||||||
|
if m.LocalScore < gold && m.LocalScore > silver {
|
||||||
|
silver = m.LocalScore
|
||||||
|
silverID = m.ID
|
||||||
|
}
|
||||||
|
if m.LocalScore < silver && m.LocalScore > bronze {
|
||||||
|
bronze = m.LocalScore
|
||||||
|
bronzeID = m.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
link := c.URLFormat("leaderboard", fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%d", year, boardId))
|
||||||
|
msg := fmt.Sprintf("AoC %s:\n", link)
|
||||||
|
for _, m := range members {
|
||||||
|
if m.Stars == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
trophy := ""
|
||||||
|
switch m.ID {
|
||||||
|
case goldID:
|
||||||
|
trophy = c.Emojy(":gold-trophy:")
|
||||||
|
case silverID:
|
||||||
|
trophy = c.Emojy(":silver-trophy:")
|
||||||
|
case bronzeID:
|
||||||
|
trophy = c.Emojy(":bronze-trophy:")
|
||||||
|
}
|
||||||
|
msg += fmt.Sprintf("%s has %d :star: for a score of %d%s\n", m.Name, m.Stars, m.LocalScore, trophy)
|
||||||
|
}
|
||||||
|
msg = strings.TrimSpace(msg)
|
||||||
|
|
||||||
|
p.b.Send(c, bot.Message, message.Channel, msg)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if boardId == 0 {
|
return false
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Error getting leaderboard: must have a board ID.")
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
board, err := getaoc.GetLeaderboard(session, year, boardId)
|
|
||||||
if err != nil {
|
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, fmt.Sprintf("Error getting leaderboard: %s", err))
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
members := []getaoc.Member{}
|
|
||||||
for _, m := range board.Members {
|
|
||||||
members = append(members, m)
|
|
||||||
}
|
|
||||||
sort.Slice(members, func(i, j int) bool {
|
|
||||||
return members[i].LocalScore > members[j].LocalScore
|
|
||||||
})
|
|
||||||
|
|
||||||
gold, silver, bronze := -1, -1, -1
|
|
||||||
goldID, silverID, bronzeID := "", "", ""
|
|
||||||
for _, m := range members {
|
|
||||||
if m.LocalScore > gold {
|
|
||||||
gold = m.LocalScore
|
|
||||||
goldID = m.ID
|
|
||||||
}
|
|
||||||
if m.LocalScore < gold && m.LocalScore > silver {
|
|
||||||
silver = m.LocalScore
|
|
||||||
silverID = m.ID
|
|
||||||
}
|
|
||||||
if m.LocalScore < silver && m.LocalScore > bronze {
|
|
||||||
bronze = m.LocalScore
|
|
||||||
bronzeID = m.ID
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
link := r.Conn.URLFormat("leaderboard", fmt.Sprintf("https://adventofcode.com/%d/leaderboard/private/view/%d", year, boardId))
|
|
||||||
msg := fmt.Sprintf("AoC %s:\n", link)
|
|
||||||
for _, m := range members {
|
|
||||||
if m.Stars == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
trophy := ""
|
|
||||||
switch m.ID {
|
|
||||||
case goldID:
|
|
||||||
trophy = r.Conn.Emojy(":gold-trophy:")
|
|
||||||
case silverID:
|
|
||||||
trophy = r.Conn.Emojy(":silver-trophy:")
|
|
||||||
case bronzeID:
|
|
||||||
trophy = r.Conn.Emojy(":bronze-trophy:")
|
|
||||||
}
|
|
||||||
msg += fmt.Sprintf("%s has %d :star: for a score of %d%s\n", m.Name, m.Stars, m.LocalScore, trophy)
|
|
||||||
}
|
|
||||||
msg = strings.TrimSpace(msg)
|
|
||||||
|
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, msg)
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
@ -27,8 +26,6 @@ type BabblerPlugin struct {
|
||||||
Bot bot.Bot
|
Bot bot.Bot
|
||||||
db *sqlx.DB
|
db *sqlx.DB
|
||||||
WithGoRoutines bool
|
WithGoRoutines bool
|
||||||
|
|
||||||
handlers bot.HandlerTable
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Babbler struct {
|
type Babbler struct {
|
||||||
|
@ -98,87 +95,57 @@ func New(b bot.Bot) *BabblerPlugin {
|
||||||
|
|
||||||
plugin.createNewWord("")
|
plugin.createNewWord("")
|
||||||
|
|
||||||
plugin.register()
|
b.Register(plugin, bot.Message, plugin.message)
|
||||||
|
b.Register(plugin, bot.Help, plugin.help)
|
||||||
|
|
||||||
return plugin
|
return plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) register() {
|
func (p *BabblerPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||||
p.handlers = bot.HandlerTable{
|
lowercase := strings.ToLower(message.Body)
|
||||||
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
|
tokens := strings.Fields(lowercase)
|
||||||
Regex: regexp.MustCompile(`(?i)^(?P<who>\S+) says-bridge (?P<start>.+)\|(?P<end>.+)$`),
|
numTokens := len(tokens)
|
||||||
Handler: func(r bot.Request) bool {
|
|
||||||
who := r.Values["who"]
|
|
||||||
start := strings.Fields(strings.ToLower(r.Values["start"]))
|
|
||||||
end := strings.Fields(strings.ToLower(r.Values["end"]))
|
|
||||||
return p.sayIt(r, p.getBabbleWithBookends(who, start, end))
|
|
||||||
}},
|
|
||||||
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
|
|
||||||
Regex: regexp.MustCompile(`(?i)^(?P<who>\S+) says-tail (?P<what>.*)$`),
|
|
||||||
Handler: func(r bot.Request) bool {
|
|
||||||
who := r.Values["who"]
|
|
||||||
what := strings.Fields(strings.ToLower(r.Values["what"]))
|
|
||||||
return p.sayIt(r, p.getBabbleWithSuffix(who, what))
|
|
||||||
}},
|
|
||||||
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
|
|
||||||
Regex: regexp.MustCompile(`(?i)^(?P<who>\S+) says-middle-out (?P<what>.*)$`),
|
|
||||||
Handler: func(r bot.Request) bool {
|
|
||||||
who := r.Values["who"]
|
|
||||||
what := strings.ToLower(r.Values["what"])
|
|
||||||
tokens := strings.Fields(what)
|
|
||||||
saidSomething := false
|
|
||||||
saidWhat := ""
|
|
||||||
|
|
||||||
saidWhatStart := p.getBabbleWithSuffix(who, tokens)
|
saidSomething := false
|
||||||
saidSomethingStart := saidWhatStart != ""
|
saidWhat := ""
|
||||||
neverSaidLooksLike := fmt.Sprintf("%s never said", who)
|
|
||||||
if !saidSomethingStart || strings.HasPrefix(saidWhatStart, neverSaidLooksLike) {
|
|
||||||
saidSomething = saidSomethingStart
|
|
||||||
saidWhat = saidWhatStart
|
|
||||||
} else {
|
|
||||||
saidWhatEnd := p.getBabble(who, tokens)
|
|
||||||
saidSomethingEnd := saidWhatEnd != ""
|
|
||||||
saidSomething = saidSomethingStart && saidSomethingEnd
|
|
||||||
if saidSomething {
|
|
||||||
saidWhat = saidWhatStart + strings.TrimPrefix(saidWhatEnd, what)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return p.sayIt(r, saidWhat)
|
|
||||||
}},
|
|
||||||
bot.HandlerSpec{Kind: bot.Message, IsCmd: false, Regex: regexp.MustCompile(`(?i)^(?P<who>\S+) (says (?P<what>.*)?|says)$`),
|
|
||||||
Handler: func(r bot.Request) bool {
|
|
||||||
who := r.Values["who"]
|
|
||||||
what := strings.Fields(strings.ToLower(r.Values["what"]))
|
|
||||||
return p.sayIt(r, p.getBabble(who, what))
|
|
||||||
}},
|
|
||||||
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
|
|
||||||
Regex: regexp.MustCompile(`(?i)^initialize babbler for (?P<who>\S+)$`),
|
|
||||||
Handler: func(r bot.Request) bool {
|
|
||||||
who := r.Values["who"]
|
|
||||||
return p.sayIt(r, p.initializeBabbler(who))
|
|
||||||
}},
|
|
||||||
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
|
|
||||||
Regex: regexp.MustCompile(`(?i)^merge babbler (?P<from>\S+) into (?P<to>\S+)$`),
|
|
||||||
Handler: func(r bot.Request) bool {
|
|
||||||
from, to := r.Values["from"], r.Values["to"]
|
|
||||||
return p.sayIt(r, p.merge(from, to))
|
|
||||||
}},
|
|
||||||
bot.HandlerSpec{Kind: bot.Message, IsCmd: false,
|
|
||||||
Regex: regexp.MustCompile(`.*`),
|
|
||||||
Handler: func(r bot.Request) bool {
|
|
||||||
p.addToBabbler(r.Msg.User.Name, strings.ToLower(r.Msg.Body))
|
|
||||||
return false
|
|
||||||
}},
|
|
||||||
}
|
|
||||||
p.Bot.RegisterTable(p, p.handlers)
|
|
||||||
p.Bot.Register(p, bot.Help, p.help)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *BabblerPlugin) sayIt(r bot.Request, what string) bool {
|
if numTokens > 2 && tokens[1] == "says-bridge" && strings.Contains(lowercase, "|") {
|
||||||
if what != "" {
|
split := strings.Split(lowercase, "|")
|
||||||
p.Bot.Send(r.Conn, bot.Message, r.Msg.Channel, what)
|
start := strings.Fields(split[0])
|
||||||
|
end := strings.Fields(split[1])
|
||||||
|
saidWhat, saidSomething = p.getBabbleWithBookends(start, end)
|
||||||
|
} else if numTokens >= 2 && tokens[1] == "says" {
|
||||||
|
saidWhat, saidSomething = p.getBabble(tokens)
|
||||||
|
} else if numTokens > 2 && tokens[1] == "says-tail" {
|
||||||
|
saidWhat, saidSomething = p.getBabbleWithSuffix(tokens)
|
||||||
|
} else if numTokens >= 2 && tokens[1] == "says-middle-out" {
|
||||||
|
saidWhatStart, saidSomethingStart := p.getBabbleWithSuffix(tokens)
|
||||||
|
neverSaidLooksLike := fmt.Sprintf("%s never said '%s'", tokens[0], strings.Join(tokens[2:], " "))
|
||||||
|
if !saidSomethingStart || saidWhatStart == neverSaidLooksLike {
|
||||||
|
saidSomething = saidSomethingStart
|
||||||
|
saidWhat = saidWhatStart
|
||||||
|
} else {
|
||||||
|
saidWhatEnd, saidSomethingEnd := p.getBabble(tokens)
|
||||||
|
saidSomething = saidSomethingStart && saidSomethingEnd
|
||||||
|
if saidSomething {
|
||||||
|
saidWhat = saidWhatStart + " " + strings.Join(strings.Fields(saidWhatEnd)[len(tokens)-2:], " ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if len(tokens) == 4 && strings.Index(lowercase, "initialize babbler for ") == 0 {
|
||||||
|
saidWhat, saidSomething = p.initializeBabbler(tokens)
|
||||||
|
} else if strings.Index(lowercase, "batch learn for ") == 0 {
|
||||||
|
saidWhat, saidSomething = p.batchLearn(tokens)
|
||||||
|
} else if len(tokens) == 5 && strings.Index(lowercase, "merge babbler") == 0 {
|
||||||
|
saidWhat, saidSomething = p.merge(tokens)
|
||||||
|
} else {
|
||||||
|
//this should always return "", false
|
||||||
|
saidWhat, saidSomething = p.addToBabbler(message.User.Name, lowercase)
|
||||||
}
|
}
|
||||||
return what != ""
|
|
||||||
|
if saidSomething {
|
||||||
|
p.Bot.Send(c, bot.Message, message.Channel, saidWhat)
|
||||||
|
}
|
||||||
|
return saidSomething
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) help(c bot.Connector, kind bot.Kind, msg msg.Message, args ...interface{}) bool {
|
func (p *BabblerPlugin) help(c bot.Connector, kind bot.Kind, msg msg.Message, args ...interface{}) bool {
|
||||||
|
|
|
@ -3,34 +3,27 @@
|
||||||
package babbler
|
package babbler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
"github.com/velour/catbase/plugins/cli"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/velour/catbase/plugins/cli"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
"github.com/velour/catbase/bot/msg"
|
"github.com/velour/catbase/bot/msg"
|
||||||
"github.com/velour/catbase/bot/user"
|
"github.com/velour/catbase/bot/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
func makeMessage(payload string, r *regexp.Regexp) bot.Request {
|
func makeMessage(payload string) (bot.Connector, bot.Kind, msg.Message) {
|
||||||
c := &cli.CliPlugin{}
|
c := &cli.CliPlugin{}
|
||||||
isCmd := strings.HasPrefix(payload, "!")
|
isCmd := strings.HasPrefix(payload, "!")
|
||||||
if isCmd {
|
if isCmd {
|
||||||
payload = payload[1:]
|
payload = payload[1:]
|
||||||
}
|
}
|
||||||
return bot.Request{
|
return c, bot.Message, msg.Message{
|
||||||
Conn: c,
|
User: &user.User{Name: "tester"},
|
||||||
Kind: bot.Message,
|
Channel: "test",
|
||||||
Values: bot.ParseValues(r, payload),
|
Body: payload,
|
||||||
Msg: msg.Message{
|
Command: isCmd,
|
||||||
User: &user.User{Name: "tester"},
|
|
||||||
Channel: "test",
|
|
||||||
Body: payload,
|
|
||||||
Command: isCmd,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,23 +39,11 @@ func newBabblerPlugin(mb *bot.MockBot) *BabblerPlugin {
|
||||||
return bp
|
return bp
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMessage(p *BabblerPlugin, msg string) bool {
|
|
||||||
for _, h := range p.handlers {
|
|
||||||
if h.Regex.MatchString(msg) {
|
|
||||||
req := makeMessage(msg, h.Regex)
|
|
||||||
if h.Handler(req) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBabblerNoBabbler(t *testing.T) {
|
func TestBabblerNoBabbler(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
testMessage(bp, "!seabass2 says")
|
bp.message(makeMessage("!seabass2 says"))
|
||||||
res := assert.Len(t, mb.Messages, 0)
|
res := assert.Len(t, mb.Messages, 0)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
// assert.Contains(t, mb.Messages[0], "seabass2 babbler not found")
|
// assert.Contains(t, mb.Messages[0], "seabass2 babbler not found")
|
||||||
|
@ -72,176 +53,229 @@ func TestBabblerNothingSaid(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
res := testMessage(bp, "initialize babbler for seabass")
|
res := bp.message(makeMessage("initialize babbler for seabass"))
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
res = testMessage(bp, "seabass says")
|
res = bp.message(makeMessage("!seabass says"))
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 2) {
|
assert.Len(t, mb.Messages, 2)
|
||||||
assert.Contains(t, mb.Messages[0], "okay.")
|
assert.Contains(t, mb.Messages[0], "okay.")
|
||||||
assert.Contains(t, mb.Messages[1], "seabass hasn't said anything yet.")
|
assert.Contains(t, mb.Messages[1], "seabass hasn't said anything yet.")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabbler(t *testing.T) {
|
func TestBabbler(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
testMessage(bp, "!initialize babbler for tester")
|
c, k, seabass := makeMessage("This is a message")
|
||||||
testMessage(bp, "This is a message")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "This is another message")
|
res := bp.message(c, k, seabass)
|
||||||
testMessage(bp, "This is a long message")
|
seabass.Body = "This is another message"
|
||||||
res := testMessage(bp, "!tester says")
|
res = bp.message(c, k, seabass)
|
||||||
|
seabass.Body = "This is a long message"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
res = bp.message(makeMessage("!seabass says"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
assert.Contains(t, mb.Messages[0], "this is")
|
||||||
assert.Contains(t, mb.Messages[0], "this is")
|
assert.Contains(t, mb.Messages[0], "message")
|
||||||
assert.Contains(t, mb.Messages[0], "message")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerSeed(t *testing.T) {
|
func TestBabblerSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("This is a message")
|
||||||
testMessage(bp, "This is a message")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "This is another message")
|
res := bp.message(c, k, seabass)
|
||||||
testMessage(bp, "This is a long message")
|
seabass.Body = "This is another message"
|
||||||
res := testMessage(bp, "tester says long")
|
res = bp.message(c, k, seabass)
|
||||||
|
seabass.Body = "This is a long message"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
res = bp.message(makeMessage("!seabass says long"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
assert.Contains(t, mb.Messages[0], "long message")
|
||||||
assert.Contains(t, mb.Messages[0], "long message")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerMultiSeed(t *testing.T) {
|
func TestBabblerMultiSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("This is a message")
|
||||||
testMessage(bp, "This is a message")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "This is another message")
|
res := bp.message(c, k, seabass)
|
||||||
testMessage(bp, "This is a long message")
|
seabass.Body = "This is another message"
|
||||||
res := testMessage(bp, "tester says is another")
|
res = bp.message(c, k, seabass)
|
||||||
|
seabass.Body = "This is a long message"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
res = bp.message(makeMessage("!seabass says This is a long"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
assert.Contains(t, mb.Messages[0], "this is a long message")
|
||||||
assert.Contains(t, mb.Messages[0], "is another")
|
}
|
||||||
}
|
|
||||||
|
func TestBabblerMultiSeed2(t *testing.T) {
|
||||||
|
mb := bot.NewMockBot()
|
||||||
|
bp := newBabblerPlugin(mb)
|
||||||
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("This is a message")
|
||||||
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
|
res := bp.message(c, k, seabass)
|
||||||
|
seabass.Body = "This is another message"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
seabass.Body = "This is a long message"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
res = bp.message(makeMessage("!seabass says is a long"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
assert.True(t, res)
|
||||||
|
assert.Contains(t, mb.Messages[0], "is a long message")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerBadSeed(t *testing.T) {
|
func TestBabblerBadSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("This is a message")
|
||||||
testMessage(bp, "This is a message")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "This is another message")
|
bp.message(c, k, seabass)
|
||||||
testMessage(bp, "This is a long message")
|
seabass.Body = "This is another message"
|
||||||
res := testMessage(bp, "tester says this is bad")
|
bp.message(c, k, seabass)
|
||||||
assert.True(t, res)
|
seabass.Body = "This is a long message"
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
bp.message(c, k, seabass)
|
||||||
assert.Contains(t, mb.Messages[0], "tester never said 'this is bad'")
|
bp.message(makeMessage("!seabass says noooo this is bad"))
|
||||||
}
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
assert.Contains(t, mb.Messages[0], "seabass never said 'noooo this is bad'")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerBadSeed2(t *testing.T) {
|
func TestBabblerBadSeed2(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("This is a message")
|
||||||
testMessage(bp, "This is a message")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "This is another message")
|
bp.message(c, k, seabass)
|
||||||
testMessage(bp, "This is a long message")
|
seabass.Body = "This is another message"
|
||||||
res := testMessage(bp, "tester says This is a really")
|
bp.message(c, k, seabass)
|
||||||
assert.True(t, res)
|
seabass.Body = "This is a long message"
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
bp.message(c, k, seabass)
|
||||||
assert.Contains(t, mb.Messages[0], "tester never said 'this is a really'")
|
bp.message(makeMessage("!seabass says This is a really"))
|
||||||
}
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
assert.Contains(t, mb.Messages[0], "seabass never said 'this is a really'")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerSuffixSeed(t *testing.T) {
|
func TestBabblerSuffixSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("This is message one")
|
||||||
testMessage(bp, "This is message one")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "It's easier to test with unique messages")
|
res := bp.message(c, k, seabass)
|
||||||
testMessage(bp, "tester says-tail message one")
|
seabass.Body = "It's easier to test with unique messages"
|
||||||
res := testMessage(bp, "tester says-tail with unique")
|
res = bp.message(c, k, seabass)
|
||||||
|
seabass.Body = "hi there"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
res = bp.message(makeMessage("!seabass says-tail message one"))
|
||||||
|
res = bp.message(makeMessage("!seabass says-tail with unique"))
|
||||||
|
assert.Len(t, mb.Messages, 2)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 2) {
|
assert.Contains(t, mb.Messages[0], "this is message one")
|
||||||
assert.Contains(t, mb.Messages[0], "this is message one")
|
assert.Contains(t, mb.Messages[1], "it's easier to test with unique")
|
||||||
assert.Contains(t, mb.Messages[1], "it's easier to test with unique")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerBadSuffixSeed(t *testing.T) {
|
func TestBabblerBadSuffixSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("This is message one")
|
||||||
testMessage(bp, "This is message one")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "It's easier to test with unique messages")
|
res := bp.message(c, k, seabass)
|
||||||
res := testMessage(bp, "tester says-tail anything true")
|
seabass.Body = "It's easier to test with unique messages"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
seabass.Body = "hi there"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
res = bp.message(makeMessage("!seabass says-tail anything true"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
assert.Contains(t, mb.Messages[0], "seabass never said 'anything true'")
|
||||||
assert.Contains(t, mb.Messages[0], "tester never said 'anything true'")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerBookendSeed(t *testing.T) {
|
func TestBabblerBookendSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("It's easier to test with unique messages")
|
||||||
testMessage(bp, "This is message one")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "It's easier to test with unique messages")
|
res := bp.message(c, k, seabass)
|
||||||
res := testMessage(bp, "tester says-bridge it's easier | unique messages")
|
res = bp.message(makeMessage("!seabass says-bridge It's easier | unique messages"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
|
||||||
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
|
}
|
||||||
}
|
|
||||||
|
func TestBabblerBookendSeedShort(t *testing.T) {
|
||||||
|
mb := bot.NewMockBot()
|
||||||
|
bp := newBabblerPlugin(mb)
|
||||||
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("It's easier to test with unique messages")
|
||||||
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
|
res := bp.message(c, k, seabass)
|
||||||
|
res = bp.message(makeMessage("!seabass says-bridge It's easier to test with | unique messages"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
assert.True(t, res)
|
||||||
|
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerBadBookendSeed(t *testing.T) {
|
func TestBabblerBadBookendSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("It's easier to test with unique messages")
|
||||||
testMessage(bp, "This is message one")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "It's easier to test with unique messages")
|
res := bp.message(c, k, seabass)
|
||||||
res := testMessage(bp, "tester says-bridge says-bridge It's easier | not unique messages")
|
res = bp.message(makeMessage("!seabass says-bridge It's easier | not unique messages"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
assert.Contains(t, mb.Messages[0], "seabass never said 'it's easier ... not unique messages'")
|
||||||
assert.Contains(t, mb.Messages[0], "tester never said 'it's easier ... not unique messages'")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerMiddleOutSeed(t *testing.T) {
|
func TestBabblerMiddleOutSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("It's easier to test with unique messages")
|
||||||
testMessage(bp, "This is message one")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "It's easier to test with unique messages")
|
res := bp.message(c, k, seabass)
|
||||||
res := testMessage(bp, "tester says-middle-out test with")
|
res = bp.message(makeMessage("!seabass says-middle-out test with"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
|
||||||
assert.Contains(t, mb.Messages[0], "it's easier to test with unique messages")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerBadMiddleOutSeed(t *testing.T) {
|
func TestBabblerBadMiddleOutSeed(t *testing.T) {
|
||||||
mb := bot.NewMockBot()
|
mb := bot.NewMockBot()
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("It's easier to test with unique messages")
|
||||||
testMessage(bp, "This is message one")
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
testMessage(bp, "It's easier to test with unique messages")
|
res := bp.message(c, k, seabass)
|
||||||
res := testMessage(bp, "tester says-middle-out anything true")
|
res = bp.message(makeMessage("!seabass says-middle-out anything true"))
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
assert.Equal(t, mb.Messages[0], "seabass never said 'anything true'")
|
||||||
assert.Contains(t, mb.Messages[0], "tester never said 'anything true'")
|
}
|
||||||
}
|
|
||||||
|
func TestBabblerBatch(t *testing.T) {
|
||||||
|
mb := bot.NewMockBot()
|
||||||
|
bp := newBabblerPlugin(mb)
|
||||||
|
assert.NotNil(t, bp)
|
||||||
|
c, k, seabass := makeMessage("batch learn for seabass This is a message! This is another message. This is not a long message? This is not a message! This is not another message. This is a long message?")
|
||||||
|
res := bp.message(c, k, seabass)
|
||||||
|
assert.Len(t, mb.Messages, 1)
|
||||||
|
res = bp.message(makeMessage("!seabass says"))
|
||||||
|
assert.Len(t, mb.Messages, 2)
|
||||||
|
assert.True(t, res)
|
||||||
|
assert.Contains(t, mb.Messages[1], "this is")
|
||||||
|
assert.Contains(t, mb.Messages[1], "message")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBabblerMerge(t *testing.T) {
|
func TestBabblerMerge(t *testing.T) {
|
||||||
|
@ -249,23 +283,28 @@ func TestBabblerMerge(t *testing.T) {
|
||||||
bp := newBabblerPlugin(mb)
|
bp := newBabblerPlugin(mb)
|
||||||
assert.NotNil(t, bp)
|
assert.NotNil(t, bp)
|
||||||
|
|
||||||
testMessage(bp, "<tester> This is a message")
|
c, k, seabass := makeMessage("<seabass> This is a message")
|
||||||
|
seabass.User = &user.User{Name: "seabass"}
|
||||||
|
res := bp.message(c, k, seabass)
|
||||||
assert.Len(t, mb.Messages, 0)
|
assert.Len(t, mb.Messages, 0)
|
||||||
|
|
||||||
testMessage(bp, "<tester> This is another message")
|
seabass.Body = "<seabass> This is another message"
|
||||||
testMessage(bp, "<tester> This is a long message")
|
res = bp.message(c, k, seabass)
|
||||||
res := testMessage(bp, "merge babbler tester into tester2")
|
|
||||||
assert.True(t, res)
|
|
||||||
if assert.Len(t, mb.Messages, 1) {
|
|
||||||
assert.Contains(t, mb.Messages[0], "mooooiggged")
|
|
||||||
}
|
|
||||||
|
|
||||||
res = testMessage(bp, "!tester2 says")
|
seabass.Body = "<seabass> This is a long message"
|
||||||
|
res = bp.message(c, k, seabass)
|
||||||
|
|
||||||
|
res = bp.message(makeMessage("!merge babbler seabass into seabass2"))
|
||||||
assert.True(t, res)
|
assert.True(t, res)
|
||||||
if assert.Len(t, mb.Messages, 2) {
|
assert.Len(t, mb.Messages, 1)
|
||||||
assert.Contains(t, mb.Messages[1], "<tester2> this is")
|
assert.Contains(t, mb.Messages[0], "mooooiggged")
|
||||||
assert.Contains(t, mb.Messages[1], "message")
|
|
||||||
}
|
res = bp.message(makeMessage("!seabass2 says"))
|
||||||
|
assert.True(t, res)
|
||||||
|
assert.Len(t, mb.Messages, 2)
|
||||||
|
|
||||||
|
assert.Contains(t, mb.Messages[1], "<seabass2> this is")
|
||||||
|
assert.Contains(t, mb.Messages[1], "message")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHelp(t *testing.T) {
|
func TestHelp(t *testing.T) {
|
||||||
|
|
|
@ -5,112 +5,117 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (p *BabblerPlugin) initializeBabbler(who string) string {
|
func (p *BabblerPlugin) initializeBabbler(tokens []string) (string, bool) {
|
||||||
|
who := tokens[3]
|
||||||
_, err := p.getOrCreateBabbler(who)
|
_, err := p.getOrCreateBabbler(who)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "babbler initialization failed."
|
return "babbler initialization failed.", true
|
||||||
}
|
}
|
||||||
return "okay."
|
return "okay.", true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) addToBabbler(babblerName, whatWasSaid string) {
|
func (p *BabblerPlugin) addToBabbler(babblerName, whatWasSaid string) (string, bool) {
|
||||||
babblerID, err := p.getOrCreateBabbler(babblerName)
|
babblerId, err := p.getOrCreateBabbler(babblerName)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if p.WithGoRoutines {
|
if p.WithGoRoutines {
|
||||||
go p.addToMarkovChain(babblerID, whatWasSaid)
|
go p.addToMarkovChain(babblerId, whatWasSaid)
|
||||||
} else {
|
} else {
|
||||||
p.addToMarkovChain(babblerID, whatWasSaid)
|
p.addToMarkovChain(babblerId, whatWasSaid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) getBabble(who string, tokens []string) string {
|
func (p *BabblerPlugin) getBabble(tokens []string) (string, bool) {
|
||||||
|
who := tokens[0]
|
||||||
_, err := p.getBabbler(who)
|
_, err := p.getBabbler(who)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == NO_BABBLER {
|
if err == NO_BABBLER {
|
||||||
// return fmt.Sprintf("%s babbler not found.", who), true
|
// return fmt.Sprintf("%s babbler not found.", who), true
|
||||||
return ""
|
return "", false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
var saying string
|
var saying string
|
||||||
if len(tokens) == 0 {
|
if len(tokens) == 2 {
|
||||||
saying, err = p.babble(who)
|
saying, err = p.babble(who)
|
||||||
} else {
|
} else {
|
||||||
saying, err = p.babbleSeed(who, tokens)
|
saying, err = p.babbleSeed(who, tokens[2:])
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == SAID_NOTHING {
|
if err == SAID_NOTHING {
|
||||||
return fmt.Sprintf("%s hasn't said anything yet.", who)
|
return fmt.Sprintf("%s hasn't said anything yet.", who), true
|
||||||
} else if err == NEVER_SAID {
|
} else if err == NEVER_SAID {
|
||||||
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens, " "))
|
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens[2:], " ")), true
|
||||||
}
|
}
|
||||||
} else if saying != "" {
|
} else if saying != "" {
|
||||||
return saying
|
return saying, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) getBabbleWithSuffix(who string, tokens []string) string {
|
func (p *BabblerPlugin) getBabbleWithSuffix(tokens []string) (string, bool) {
|
||||||
|
who := tokens[0]
|
||||||
_, err := p.getBabbler(who)
|
_, err := p.getBabbler(who)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == NO_BABBLER {
|
if err == NO_BABBLER {
|
||||||
// return fmt.Sprintf("%s babbler not found.", who), true
|
// return fmt.Sprintf("%s babbler not found.", who), true
|
||||||
return ""
|
return "", false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
saying, err := p.babbleSeedSuffix(who, tokens)
|
saying, err := p.babbleSeedSuffix(who, tokens[2:])
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == SAID_NOTHING {
|
if err == SAID_NOTHING {
|
||||||
return fmt.Sprintf("%s hasn't said anything yet.", who)
|
return fmt.Sprintf("%s hasn't said anything yet.", who), true
|
||||||
} else if err == NEVER_SAID {
|
} else if err == NEVER_SAID {
|
||||||
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens, " "))
|
return fmt.Sprintf("%s never said '%s'", who, strings.Join(tokens[2:], " ")), true
|
||||||
}
|
}
|
||||||
} else if saying != "" {
|
} else if saying != "" {
|
||||||
return saying
|
return saying, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) getBabbleWithBookends(who string, start, end []string) string {
|
func (p *BabblerPlugin) getBabbleWithBookends(start, end []string) (string, bool) {
|
||||||
|
who := start[0]
|
||||||
_, err := p.getBabbler(who)
|
_, err := p.getBabbler(who)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == NO_BABBLER {
|
if err == NO_BABBLER {
|
||||||
// return fmt.Sprintf("%s babbler not found.", who), true
|
// return fmt.Sprintf("%s babbler not found.", who), true
|
||||||
return ""
|
return "", false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
saying, err := p.babbleSeedBookends(who, start, end)
|
saying, err := p.babbleSeedBookends(who, start[2:], end)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == SAID_NOTHING {
|
if err == SAID_NOTHING {
|
||||||
return fmt.Sprintf("%s hasn't said anything yet.", who)
|
return fmt.Sprintf("%s hasn't said anything yet.", who), true
|
||||||
} else if err == NEVER_SAID {
|
} else if err == NEVER_SAID {
|
||||||
seeds := append(start[1:], "...")
|
seeds := append(start[2:], "...")
|
||||||
seeds = append(seeds, end...)
|
seeds = append(seeds, end...)
|
||||||
return fmt.Sprintf("%s never said '%s'", who, strings.Join(seeds, " "))
|
return fmt.Sprintf("%s never said '%s'", who, strings.Join(seeds, " ")), true
|
||||||
}
|
}
|
||||||
} else if saying != "" {
|
} else if saying != "" {
|
||||||
return saying
|
return saying, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ""
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) batchLearn(tokens []string) string {
|
func (p *BabblerPlugin) batchLearn(tokens []string) (string, bool) {
|
||||||
who := tokens[3]
|
who := tokens[3]
|
||||||
babblerId, err := p.getOrCreateBabbler(who)
|
babblerId, err := p.getOrCreateBabbler(who)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "batch learn failed."
|
return "batch learn failed.", true
|
||||||
}
|
}
|
||||||
|
|
||||||
body := strings.Join(tokens[4:], " ")
|
body := strings.Join(tokens[4:], " ")
|
||||||
|
@ -128,30 +133,37 @@ func (p *BabblerPlugin) batchLearn(tokens []string) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "phew that was tiring."
|
return "phew that was tiring.", true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BabblerPlugin) merge(who, into string) string {
|
func (p *BabblerPlugin) merge(tokens []string) (string, bool) {
|
||||||
|
if tokens[3] != "into" {
|
||||||
|
return "try using 'merge babbler [x] into [y]'", true
|
||||||
|
}
|
||||||
|
|
||||||
|
who := tokens[2]
|
||||||
|
into := tokens[4]
|
||||||
|
|
||||||
if who == into {
|
if who == into {
|
||||||
return "that's annoying. stop it."
|
return "that's annoying. stop it.", true
|
||||||
}
|
}
|
||||||
|
|
||||||
whoBabbler, err := p.getBabbler(who)
|
whoBabbler, err := p.getBabbler(who)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == NO_BABBLER {
|
if err == NO_BABBLER {
|
||||||
return fmt.Sprintf("%s babbler not found.", who)
|
return fmt.Sprintf("%s babbler not found.", who), true
|
||||||
}
|
}
|
||||||
return "merge failed."
|
return "merge failed.", true
|
||||||
}
|
}
|
||||||
intoBabbler, err := p.getOrCreateBabbler(into)
|
intoBabbler, err := p.getOrCreateBabbler(into)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "merge failed."
|
return "merge failed.", true
|
||||||
}
|
}
|
||||||
|
|
||||||
err = p.mergeBabblers(intoBabbler, whoBabbler, into, who)
|
err = p.mergeBabblers(intoBabbler, whoBabbler, into, who)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "merge failed."
|
return "merge failed.", true
|
||||||
}
|
}
|
||||||
|
|
||||||
return "mooooiggged"
|
return "mooooiggged", true
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/velour/catbase/bot"
|
"github.com/velour/catbase/bot"
|
||||||
|
@ -24,22 +23,27 @@ func New(b bot.Bot) *GPT2Plugin {
|
||||||
c: b.Config(),
|
c: b.Config(),
|
||||||
}
|
}
|
||||||
|
|
||||||
b.RegisterRegexCmd(p, bot.Message, gpt2Regex, p.gpt2Cmd)
|
b.Register(p, bot.Message, p.message)
|
||||||
b.Register(p, bot.Help, p.help)
|
b.Register(p, bot.Help, p.help)
|
||||||
|
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
var gpt2Regex = regexp.MustCompile(`(?i)^gpt2 (?P<input>.*)$`)
|
const prefix = "gpt2"
|
||||||
|
|
||||||
func (p *GPT2Plugin) gpt2Cmd(r bot.Request) bool {
|
func (p *GPT2Plugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||||
input := r.Values["input"]
|
ch := message.Channel
|
||||||
txt, err := p.getGPTText(input)
|
lowerBody := strings.ToLower(message.Body)
|
||||||
if err != nil {
|
if message.Command && strings.HasPrefix(lowerBody, prefix) {
|
||||||
txt = p.c.Get("gpt.error", "The GPT service is unavailable.")
|
input := message.Body[len(prefix)+1:]
|
||||||
|
txt, err := p.getGPTText(input)
|
||||||
|
if err != nil {
|
||||||
|
txt = p.c.Get("gpt.error", "The GPT service is unavailable.")
|
||||||
|
}
|
||||||
|
p.b.Send(c, bot.Message, ch, txt)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, txt)
|
return false
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *GPT2Plugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
func (p *GPT2Plugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
||||||
|
|
Loading…
Reference in New Issue