From d85c855d474c03a876500b06aff1ac7d52b2bc32 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 5 Feb 2019 12:25:31 -0500 Subject: [PATCH] bot: rename a few things --- bot/bot.go | 10 ++++++---- bot/handlers.go | 6 +++--- bot/interfaces.go | 14 +++++++------- bot/mock.go | 14 +++++++------- irc/irc.go | 2 +- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index e3fffa3..f0a1dac 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -42,7 +42,7 @@ type bot struct { // filters registered by plugins filters map[string]func(string) string - callbacks map[string][]Callback + callbacks CallbackMap } // Variable represents a $var replacement @@ -74,7 +74,7 @@ func New(config *config.Config, connector Connector) Bot { logOut: logOut, httpEndPoints: make(map[string]string), filters: make(map[string]func(string) string), - callbacks: make(map[string][]Callback), + callbacks: make(CallbackMap), } bot.migrateDB() @@ -250,7 +250,9 @@ func (b *bot) RegisterFilter(name string, f func(string) string) { } // Send a message to the connection -func (b *bot) Send(int, ...interface{}) (error, string) { return nil, "" } +func (b *bot) Send(kind Kind, args ...interface{}) (error, string) { return nil, "" } // Register a callback -func (b *bot) Register(int, Callback) {} +func (b *bot) Register(name string, kind Kind, cb Callback) { + b.callbacks[name][kind] = append(b.callbacks[name][kind], cb) +} diff --git a/bot/handlers.go b/bot/handlers.go index 072d36e..06814c7 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -16,7 +16,7 @@ import ( "github.com/velour/catbase/bot/msg" ) -func (b *bot) Receive(kind int, msg msg.Message, args ...interface{}) { +func (b *bot) Receive(kind Kind, msg msg.Message, args ...interface{}) { panic("I don't know what to do here yet") } @@ -54,8 +54,8 @@ func (b *bot) EventReceived(msg msg.Message) { } } -func (b *bot) runCallback(plugin string, evt int, message msg.Message, args ...interface{}) bool { - for _, cb := range b.callbacks[plugin] { +func (b *bot) runCallback(plugin string, evt Kind, message msg.Message, args ...interface{}) bool { + for _, cb := range b.callbacks[plugin][evt] { if cb(evt, message) { return true } diff --git a/bot/interfaces.go b/bot/interfaces.go index 7320ead..950a8b2 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -30,9 +30,9 @@ const ( SelfMessage ) -type kind int - -type Callback func(int, msg.Message, ...interface{}) bool +type Kind int +type Callback func(Kind, msg.Message, ...interface{}) bool +type CallbackMap map[string]map[Kind][]Callback // Bot interface serves to allow mocking of the actual bot type Bot interface { @@ -45,11 +45,11 @@ type Bot interface { // AddPlugin registers a new plugin handler AddPlugin(string, Plugin) // First arg should be one of bot.Message/Reply/Action/etc - Send(int, ...interface{}) (error, string) + Send(Kind, ...interface{}) (error, string) // First arg should be one of bot.Message/Reply/Action/etc - Receive(int, msg.Message, ...interface{}) + Receive(Kind, msg.Message, ...interface{}) // Register a callback - Register(int, Callback) + Register(string, Kind, Callback) Filter(msg.Message, string) string LastMessage(string) (msg.Message, error) @@ -65,7 +65,7 @@ type Connector interface { RegisterMessageReceived(func(message msg.Message)) RegisterReplyMessageReceived(func(msg.Message, string)) - Send(int, ...interface{}) (error, string) + Send(Kind, ...interface{}) (error, string) GetEmojiList() map[string]string Serve() error diff --git a/bot/mock.go b/bot/mock.go index 60afc2c..3e300af 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -29,7 +29,7 @@ type MockBot struct { func (mb *MockBot) Config() *config.Config { return mb.Cfg } func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB } func (mb *MockBot) Who(string) []user.User { return []user.User{} } -func (mb *MockBot) Send(kind int, args ...interface{}) (error, string) { +func (mb *MockBot) Send(kind Kind, args ...interface{}) (error, string) { switch kind { case Message: mb.Messages = append(mb.Messages, args[1].(string)) @@ -46,12 +46,12 @@ func (mb *MockBot) Send(kind int, args ...interface{}) (error, string) { } return fmt.Errorf("Mesasge type unhandled"), "ERROR" } -func (mb *MockBot) AddPlugin(name string, f Plugin) {} -func (mb *MockBot) Register(kind int, cb Callback) {} -func (mb *MockBot) Receive(kind int, msg msg.Message, args ...interface{}) {} -func (mb *MockBot) Filter(msg msg.Message, s string) string { return s } -func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil } -func (mb *MockBot) CheckAdmin(nick string) bool { return false } +func (mb *MockBot) AddPlugin(name string, f Plugin) {} +func (mb *MockBot) Register(name string, kind Kind, cb Callback) {} +func (mb *MockBot) Receive(kind Kind, msg msg.Message, args ...interface{}) {} +func (mb *MockBot) Filter(msg msg.Message, s string) string { return s } +func (mb *MockBot) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil } +func (mb *MockBot) CheckAdmin(nick string) bool { return false } func (mb *MockBot) react(channel, reaction string, message msg.Message) (error, string) { mb.Reactions = append(mb.Reactions, reaction) diff --git a/irc/irc.go b/irc/irc.go index 086c0c8..de6c992 100644 --- a/irc/irc.go +++ b/irc/irc.go @@ -66,7 +66,7 @@ func (i *Irc) RegisterReplyMessageReceived(f func(msg.Message, string)) { i.replyMessageReceived = f } -func (i *Irc) Send(kind int, args ...interface{}) (error, string) { +func (i *Irc) Send(kind bot.Kind, args ...interface{}) (error, string) { switch kind { case bot.Reply: case bot.Message: