From 933e514dddbfc9c014a94b8e1db9115431f3a913 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 5 Feb 2019 13:58:12 -0500 Subject: [PATCH] bot: switch plugins to matching instance instead of string --- bot/bot.go | 13 ++++++------- bot/handlers.go | 13 ++++++------- bot/interfaces.go | 4 ++-- plugins/admin/admin.go | 4 ++-- plugins/babbler/babbler.go | 34 ++++++++++++++-------------------- 5 files changed, 30 insertions(+), 38 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 9d8c55f..d7eca51 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -248,13 +248,12 @@ func (b *bot) RegisterFilter(name string, f func(string) string) { } // Register a callback -func (b *bot) Register(name string, kind Kind, cb Callback) { - name = strings.ToLower(name) - if _, ok := b.callbacks[name]; !ok { - b.callbacks[name] = make(map[Kind][]Callback) +func (b *bot) Register(p Plugin, kind Kind, cb Callback) { + if _, ok := b.callbacks[p]; !ok { + b.callbacks[p] = make(map[Kind][]Callback) } - if _, ok := b.callbacks[name][kind]; !ok { - b.callbacks[name][kind] = []Callback{} + if _, ok := b.callbacks[p][kind]; !ok { + b.callbacks[p][kind] = []Callback{} } - b.callbacks[name][kind] = append(b.callbacks[name][kind], cb) + b.callbacks[p][kind] = append(b.callbacks[p][kind], cb) } diff --git a/bot/handlers.go b/bot/handlers.go index 70f0754..be789cd 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -28,7 +28,7 @@ func (b *bot) Receive(kind Kind, msg msg.Message, args ...interface{}) { } for _, name := range b.pluginOrdering { - if b.runCallback(name, kind, msg, args) { + if b.runCallback(b.plugins[name], kind, msg, args) { goto RET } } @@ -38,7 +38,7 @@ RET: return } -func (b *bot) runCallback(plugin string, evt Kind, message msg.Message, args ...interface{}) bool { +func (b *bot) runCallback(plugin Plugin, evt Kind, message msg.Message, args ...interface{}) bool { for _, cb := range b.callbacks[plugin][evt] { if cb(evt, message, args) { return true @@ -75,10 +75,9 @@ func (b *bot) checkHelp(channel string, parts []string) { b.listVars(channel, parts) return } - plugin := b.plugins[parts[1]] - if plugin != nil { - // TODO: Maybe broke - b.runCallback(parts[1], Help, msg.Message{Channel: channel}, channel, parts) + plugin, ok := b.plugins[parts[1]] + if ok { + b.runCallback(plugin, Help, msg.Message{Channel: channel}, channel, parts) } else { msg := fmt.Sprintf("I'm sorry, I don't know what %s is!", parts[1]) b.Send(Message, channel, msg) @@ -201,7 +200,7 @@ func (b *bot) selfSaid(channel, message string, action bool) { } for _, name := range b.pluginOrdering { - if b.runCallback(name, SelfMessage, msg) { + if b.runCallback(b.plugins[name], SelfMessage, msg) { return } } diff --git a/bot/interfaces.go b/bot/interfaces.go index b3dc6d7..1a348dc 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -32,7 +32,7 @@ const ( type Kind int type Callback func(Kind, msg.Message, ...interface{}) bool -type CallbackMap map[string]map[Kind][]Callback +type CallbackMap map[Plugin]map[Kind][]Callback // Bot interface serves to allow mocking of the actual bot type Bot interface { @@ -49,7 +49,7 @@ type Bot interface { // First arg should be one of bot.Message/Reply/Action/etc Receive(Kind, msg.Message, ...interface{}) // Register a callback - Register(string, Kind, Callback) + Register(Plugin, Kind, Callback) Filter(msg.Message, string) string LastMessage(string) (msg.Message, error) diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index cb2eba2..5de34f0 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -31,8 +31,8 @@ func New(b bot.Bot) *AdminPlugin { db: b.DB(), cfg: b.Config(), } - b.Register("admin", bot.Message, p.message) - b.Register("admin", bot.Help, p.help) + b.Register(p, bot.Message, p.message) + b.Register(p, bot.Help, p.help) return p } diff --git a/plugins/babbler/babbler.go b/plugins/babbler/babbler.go index 38997ec..efc0c63 100644 --- a/plugins/babbler/babbler.go +++ b/plugins/babbler/babbler.go @@ -52,24 +52,24 @@ type BabblerArc struct { Frequency int64 `db:"frequency"` } -func New(bot bot.Bot) *BabblerPlugin { +func New(b bot.Bot) *BabblerPlugin { log.SetFlags(log.LstdFlags | log.Lshortfile) - if _, err := bot.DB().Exec(`create table if not exists babblers ( + if _, err := b.DB().Exec(`create table if not exists babblers ( id integer primary key, babbler string );`); err != nil { log.Fatal(err) } - if _, err := bot.DB().Exec(`create table if not exists babblerWords ( + if _, err := b.DB().Exec(`create table if not exists babblerWords ( id integer primary key, word string );`); err != nil { log.Fatal(err) } - if _, err := bot.DB().Exec(`create table if not exists babblerNodes ( + if _, err := b.DB().Exec(`create table if not exists babblerNodes ( id integer primary key, babblerId integer, wordId integer, @@ -79,7 +79,7 @@ func New(bot bot.Bot) *BabblerPlugin { log.Fatal(err) } - if _, err := bot.DB().Exec(`create table if not exists babblerArcs ( + if _, err := b.DB().Exec(`create table if not exists babblerArcs ( id integer primary key, fromNodeId integer, toNodeId interger, @@ -89,17 +89,20 @@ func New(bot bot.Bot) *BabblerPlugin { } plugin := &BabblerPlugin{ - Bot: bot, - db: bot.DB(), + Bot: b, + db: b.DB(), WithGoRoutines: true, } plugin.createNewWord("") + b.Register(plugin, bot.Message, plugin.message) + b.Register(plugin, bot.Help, plugin.help) + return plugin } -func (p *BabblerPlugin) Message(message msg.Message) bool { +func (p *BabblerPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { lowercase := strings.ToLower(message.Body) tokens := strings.Fields(lowercase) numTokens := len(tokens) @@ -146,7 +149,7 @@ func (p *BabblerPlugin) Message(message msg.Message) bool { return saidSomething } -func (p *BabblerPlugin) Help(channel string, parts []string) { +func (p *BabblerPlugin) help(kind bot.Kind, msg msg.Message, args ...interface{}) bool { commands := []string{ "initialize babbler for seabass", "merge babbler drseabass into seabass", @@ -155,15 +158,8 @@ func (p *BabblerPlugin) Help(channel string, parts []string) { "seabass says-middle-out ...", "seabass says-bridge ... | ...", } - p.Bot.Send(bot.Message, channel, strings.Join(commands, "\n\n")) -} - -func (p *BabblerPlugin) Event(kind string, message msg.Message) bool { - return false -} - -func (p *BabblerPlugin) BotMessage(message msg.Message) bool { - return false + p.Bot.Send(bot.Message, msg.Channel, strings.Join(commands, "\n\n")) + return true } func (p *BabblerPlugin) RegisterWeb() *string { @@ -934,5 +930,3 @@ func (p *BabblerPlugin) babbleSeedBookends(babblerName string, start, end []stri return strings.Join(words, " "), nil } - -func (p *BabblerPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }