bot: rename a few things

This commit is contained in:
Chris Sexton 2019-02-05 12:25:31 -05:00
parent 2e20971dd1
commit d85c855d47
5 changed files with 24 additions and 22 deletions

View File

@ -42,7 +42,7 @@ type bot struct {
// filters registered by plugins // filters registered by plugins
filters map[string]func(string) string filters map[string]func(string) string
callbacks map[string][]Callback callbacks CallbackMap
} }
// Variable represents a $var replacement // Variable represents a $var replacement
@ -74,7 +74,7 @@ func New(config *config.Config, connector Connector) Bot {
logOut: logOut, logOut: logOut,
httpEndPoints: make(map[string]string), httpEndPoints: make(map[string]string),
filters: make(map[string]func(string) string), filters: make(map[string]func(string) string),
callbacks: make(map[string][]Callback), callbacks: make(CallbackMap),
} }
bot.migrateDB() bot.migrateDB()
@ -250,7 +250,9 @@ func (b *bot) RegisterFilter(name string, f func(string) string) {
} }
// Send a message to the connection // 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 // 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)
}

View File

@ -16,7 +16,7 @@ import (
"github.com/velour/catbase/bot/msg" "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") 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 { func (b *bot) runCallback(plugin string, evt Kind, message msg.Message, args ...interface{}) bool {
for _, cb := range b.callbacks[plugin] { for _, cb := range b.callbacks[plugin][evt] {
if cb(evt, message) { if cb(evt, message) {
return true return true
} }

View File

@ -30,9 +30,9 @@ const (
SelfMessage SelfMessage
) )
type kind int type Kind int
type Callback func(Kind, msg.Message, ...interface{}) bool
type Callback func(int, msg.Message, ...interface{}) bool type CallbackMap map[string]map[Kind][]Callback
// Bot interface serves to allow mocking of the actual bot // Bot interface serves to allow mocking of the actual bot
type Bot interface { type Bot interface {
@ -45,11 +45,11 @@ type Bot interface {
// AddPlugin registers a new plugin handler // AddPlugin registers a new plugin handler
AddPlugin(string, Plugin) AddPlugin(string, Plugin)
// First arg should be one of bot.Message/Reply/Action/etc // 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 // 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 a callback
Register(int, Callback) Register(string, Kind, Callback)
Filter(msg.Message, string) string Filter(msg.Message, string) string
LastMessage(string) (msg.Message, error) LastMessage(string) (msg.Message, error)
@ -65,7 +65,7 @@ type Connector interface {
RegisterMessageReceived(func(message msg.Message)) RegisterMessageReceived(func(message msg.Message))
RegisterReplyMessageReceived(func(msg.Message, string)) RegisterReplyMessageReceived(func(msg.Message, string))
Send(int, ...interface{}) (error, string) Send(Kind, ...interface{}) (error, string)
GetEmojiList() map[string]string GetEmojiList() map[string]string
Serve() error Serve() error

View File

@ -29,7 +29,7 @@ type MockBot struct {
func (mb *MockBot) Config() *config.Config { return mb.Cfg } func (mb *MockBot) Config() *config.Config { return mb.Cfg }
func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB } func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB }
func (mb *MockBot) Who(string) []user.User { return []user.User{} } 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 { switch kind {
case Message: case Message:
mb.Messages = append(mb.Messages, args[1].(string)) 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" return fmt.Errorf("Mesasge type unhandled"), "ERROR"
} }
func (mb *MockBot) AddPlugin(name string, f Plugin) {} func (mb *MockBot) AddPlugin(name string, f Plugin) {}
func (mb *MockBot) Register(kind int, cb Callback) {} func (mb *MockBot) Register(name string, kind Kind, cb Callback) {}
func (mb *MockBot) Receive(kind int, msg msg.Message, args ...interface{}) {} 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) 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) LastMessage(ch string) (msg.Message, error) { return msg.Message{}, nil }
func (mb *MockBot) CheckAdmin(nick string) bool { return false } func (mb *MockBot) CheckAdmin(nick string) bool { return false }
func (mb *MockBot) react(channel, reaction string, message msg.Message) (error, string) { func (mb *MockBot) react(channel, reaction string, message msg.Message) (error, string) {
mb.Reactions = append(mb.Reactions, reaction) mb.Reactions = append(mb.Reactions, reaction)

View File

@ -66,7 +66,7 @@ func (i *Irc) RegisterReplyMessageReceived(f func(msg.Message, string)) {
i.replyMessageReceived = f 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 { switch kind {
case bot.Reply: case bot.Reply:
case bot.Message: case bot.Message: