From e7c88c0c9c028722bdda6a708b38861cb82d8bc6 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 5 Feb 2019 10:54:13 -0500 Subject: [PATCH] events: refactor Combining all of the various send/recv functions into one --- bot/bot.go | 22 ++++++++--- bot/handlers.go | 58 +++++++++++++++++------------ bot/interfaces.go | 60 +++++++++++++++++++----------- bot/mock.go | 11 +++--- main.go | 50 ++++++++++++------------- plugins/admin/admin.go | 24 ++++++------ plugins/babbler/babbler.go | 4 +- plugins/beers/beers.go | 26 ++++++------- plugins/couldashouldawoulda/csw.go | 2 +- plugins/counter/counter.go | 36 +++++++++--------- plugins/dice/dice.go | 6 +-- plugins/downtime/downtime.go | 8 ++-- plugins/emojifyme/emojifyme.go | 2 +- plugins/fact/factoid.go | 48 ++++++++++++------------ plugins/fact/remember.go | 10 ++--- plugins/first/first.go | 4 +- plugins/inventory/inventory.go | 8 ++-- plugins/leftpad/leftpad.go | 6 +-- plugins/nerdepedia/nerdepedia.go | 4 +- plugins/picker/picker.go | 8 ++-- plugins/reaction/reaction.go | 2 +- plugins/reminder/reminder.go | 26 ++++++------- plugins/rpgORdie/rpgORdie.go | 14 +++---- plugins/rss/rss.go | 8 ++-- plugins/sisyphus/sisyphus.go | 28 +++++++------- plugins/talker/talker.go | 6 +-- plugins/tell/tell.go | 4 +- plugins/twitch/twitch.go | 10 ++--- plugins/your/your.go | 4 +- plugins/zork/zork.go | 8 ++-- 30 files changed, 271 insertions(+), 236 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 6a69bb2..e3fffa3 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -19,7 +19,7 @@ import ( type bot struct { // Each plugin must be registered in our plugins handler. To come: a map so that this // will allow plugins to respond to specific kinds of events - plugins map[string]Handler + plugins map[string]Plugin pluginOrdering []string // Users holds information about all of our friends @@ -41,13 +41,16 @@ type bot struct { // filters registered by plugins filters map[string]func(string) string + + callbacks map[string][]Callback } +// Variable represents a $var replacement type Variable struct { Variable, Value string } -// Newbot creates a bot for a given connection and set of handlers. +// New creates a bot for a given connection and set of handlers. func New(config *config.Config, connector Connector) Bot { logIn := make(chan msg.Message) logOut := make(chan msg.Messages) @@ -62,7 +65,7 @@ func New(config *config.Config, connector Connector) Bot { bot := &bot{ config: config, - plugins: make(map[string]Handler), + plugins: make(map[string]Plugin), pluginOrdering: make([]string, 0), conn: connector, users: users, @@ -71,6 +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), } bot.migrateDB() @@ -109,7 +113,7 @@ func (b *bot) migrateDB() { } // Adds a constructed handler to the bots handlers list -func (b *bot) AddHandler(name string, h Handler) { +func (b *bot) AddPlugin(name string, h Plugin) { b.plugins[name] = h b.pluginOrdering = append(b.pluginOrdering, name) if entry := h.RegisterWeb(); entry != nil { @@ -126,7 +130,7 @@ func (b *bot) Who(channel string) []user.User { return users } -var rootIndex string = ` +var rootIndex = ` @@ -166,7 +170,7 @@ func (b *bot) serveRoot(w http.ResponseWriter, r *http.Request) { t.Execute(w, context) } -// Checks if message is a command and returns its curtailed version +// IsCmd checks if message is a command and returns its curtailed version func IsCmd(c *config.Config, message string) (bool, string) { cmdcs := c.GetArray("CommandChar", []string{"!"}) botnick := strings.ToLower(c.Get("Nick", "bot")) @@ -244,3 +248,9 @@ func (b *bot) checkAdmin(nick string) bool { func (b *bot) RegisterFilter(name string, f func(string) string) { b.filters[name] = f } + +// Send a message to the connection +func (b *bot) Send(int, ...interface{}) (error, string) { return nil, "" } + +// Register a callback +func (b *bot) Register(int, Callback) {} diff --git a/bot/handlers.go b/bot/handlers.go index 39b5d72..072d36e 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -16,6 +16,10 @@ import ( "github.com/velour/catbase/bot/msg" ) +func (b *bot) Receive(kind int, msg msg.Message, args ...interface{}) { + panic("I don't know what to do here yet") +} + // Handles incomming PRIVMSG requests func (b *bot) MsgReceived(msg msg.Message) { log.Println("Received message: ", msg) @@ -29,9 +33,8 @@ func (b *bot) MsgReceived(msg msg.Message) { } for _, name := range b.pluginOrdering { - p := b.plugins[name] - if p.Message(msg) { - break + if b.runCallback(name, Message, msg) { + goto RET } } @@ -45,47 +48,54 @@ func (b *bot) EventReceived(msg msg.Message) { log.Println("Received event: ", msg) //msg := b.buildMessage(conn, inMsg) for _, name := range b.pluginOrdering { - p := b.plugins[name] - if p.Event(msg.Body, msg) { // TODO: could get rid of msg.Body - break + if b.runCallback(name, Event, msg) { + return } } } +func (b *bot) runCallback(plugin string, evt int, message msg.Message, args ...interface{}) bool { + for _, cb := range b.callbacks[plugin] { + if cb(evt, message) { + return true + } + } + return false +} + // Handle incoming replys func (b *bot) ReplyMsgReceived(msg msg.Message, identifier string) { log.Println("Received message: ", msg) for _, name := range b.pluginOrdering { - p := b.plugins[name] - if p.ReplyMessage(msg, identifier) { + if b.runCallback(name, Reply, msg, identifier) { break } } } -func (b *bot) SendMessage(channel, message string) string { - return b.conn.SendMessage(channel, message) +func (b *bot) SendMessage(channel, message string) (error, string) { + return b.conn.Send(Message, channel, message) } -func (b *bot) SendAction(channel, message string) string { - return b.conn.SendAction(channel, message) +func (b *bot) SendAction(channel, message string) (error, string) { + return b.conn.Send(Action, channel, message) } -func (b *bot) ReplyToMessageIdentifier(channel, message, identifier string) (string, bool) { - return b.conn.ReplyToMessageIdentifier(channel, message, identifier) +func (b *bot) ReplyToMessageIdentifier(channel, message, identifier string) (error, string) { + return b.conn.Send(Reply, channel, message, identifier) } -func (b *bot) ReplyToMessage(channel, message string, replyTo msg.Message) (string, bool) { - return b.conn.ReplyToMessage(channel, message, replyTo) +func (b *bot) ReplyToMessage(channel, message string, replyTo msg.Message) (error, string) { + return b.conn.Send(Reply, channel, message, replyTo) } -func (b *bot) React(channel, reaction string, message msg.Message) bool { - return b.conn.React(channel, reaction, message) +func (b *bot) React(channel, reaction string, message msg.Message) (error, string) { + return b.conn.Send(Reaction, channel, reaction, message) } -func (b *bot) Edit(channel, newMessage, identifier string) bool { - return b.conn.Edit(channel, newMessage, identifier) +func (b *bot) Edit(channel, newMessage, identifier string) (error, string) { + return b.conn.Send(Edit, channel, newMessage, identifier) } func (b *bot) GetEmojiList() map[string]string { @@ -113,7 +123,8 @@ func (b *bot) checkHelp(channel string, parts []string) { } plugin := b.plugins[parts[1]] if plugin != nil { - plugin.Help(channel, parts) + // TODO: Maybe broke + b.runCallback(parts[1], Help, msg.Message{Channel: channel}, channel, parts) } else { msg := fmt.Sprintf("I'm sorry, I don't know what %s is!", parts[1]) b.SendMessage(channel, msg) @@ -236,9 +247,8 @@ func (b *bot) selfSaid(channel, message string, action bool) { } for _, name := range b.pluginOrdering { - p := b.plugins[name] - if p.BotMessage(msg) { - break + if b.runCallback(name, SelfMessage, msg) { + return } } } diff --git a/bot/interfaces.go b/bot/interfaces.go index 42b0980..ec2aa6e 100644 --- a/bot/interfaces.go +++ b/bot/interfaces.go @@ -9,22 +9,47 @@ import ( "github.com/velour/catbase/config" ) +const ( + _ = iota + + // Message any standard chat + Message + // Reply something containing a message reference + Reply + // Action any /me action + Action + // Reaction Icon reaction if service supports it + Reaction + // Edit message ref'd new message to replace + Edit + // Not sure what event is + Event + // Help is used when the bot help system is triggered + Help + // SelfMessage triggers when the bot is sending a message + SelfMessage +) + +type kind int + +type Callback func(int, msg.Message, ...interface{}) bool + type Bot interface { Config() *config.Config DB() *sqlx.DB Who(string) []user.User - AddHandler(string, Handler) - SendMessage(string, string) string - SendAction(string, string) string - ReplyToMessageIdentifier(string, string, string) (string, bool) - ReplyToMessage(string, string, msg.Message) (string, bool) - React(string, string, msg.Message) bool - Edit(string, string, string) bool - MsgReceived(msg.Message) - ReplyMsgReceived(msg.Message, string) - EventReceived(msg.Message) + AddPlugin(string, Plugin) + + // First arg should be one of bot.Message/Reply/Action/etc + Send(int, ...interface{}) (error, string) + // First arg should be one of bot.Message/Reply/Action/etc + Receive(int, msg.Message, ...interface{}) + // Register a callback + Register(int, Callback) + Filter(msg.Message, string) string LastMessage(string) (msg.Message, error) + CheckAdmin(string) bool GetEmojiList() map[string]string RegisterFilter(string, func(string) string) @@ -35,12 +60,8 @@ type Connector interface { RegisterMessageReceived(func(message msg.Message)) RegisterReplyMessageReceived(func(msg.Message, string)) - SendMessage(channel, message string) string - SendAction(channel, message string) string - ReplyToMessageIdentifier(string, string, string) (string, bool) - ReplyToMessage(string, string, msg.Message) (string, bool) - React(string, string, msg.Message) bool - Edit(string, string, string) bool + Send(int, ...interface{}) (error, string) + GetEmojiList() map[string]string Serve() error @@ -48,11 +69,6 @@ type Connector interface { } // Interface used for compatibility with the Plugin interface -type Handler interface { - Message(message msg.Message) bool - Event(kind string, message msg.Message) bool - ReplyMessage(msg.Message, string) bool - BotMessage(message msg.Message) bool - Help(channel string, parts []string) +type Plugin interface { RegisterWeb() *string } diff --git a/bot/mock.go b/bot/mock.go index f3170e4..710b7f0 100644 --- a/bot/mock.go +++ b/bot/mock.go @@ -26,12 +26,11 @@ type MockBot struct { Reactions []string } -func (mb *MockBot) Config() *config.Config { return mb.Cfg } -func (mb *MockBot) DBVersion() int64 { return 1 } -func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB } -func (mb *MockBot) Conn() Connector { return nil } -func (mb *MockBot) Who(string) []user.User { return []user.User{} } -func (mb *MockBot) AddHandler(name string, f Handler) {} +func (mb *MockBot) Config() *config.Config { return mb.Cfg } +func (mb *MockBot) DBVersion() int64 { return 1 } +func (mb *MockBot) DB() *sqlx.DB { return mb.Cfg.DB } +func (mb *MockBot) Conn() Connector { return nil } +func (mb *MockBot) Who(string) []user.User { return []user.User{} } func (mb *MockBot) SendMessage(ch string, msg string) string { mb.Messages = append(mb.Messages, msg) return fmt.Sprintf("m-%d", len(mb.Actions)-1) diff --git a/main.go b/main.go index c3d7ebe..18ff59f 100644 --- a/main.go +++ b/main.go @@ -78,32 +78,32 @@ func main() { b := bot.New(c, client) - b.AddHandler("admin", admin.New(b)) - b.AddHandler("first", first.New(b)) - b.AddHandler("leftpad", leftpad.New(b)) - b.AddHandler("talker", talker.New(b)) - b.AddHandler("dice", dice.New(b)) - b.AddHandler("picker", picker.New(b)) - b.AddHandler("beers", beers.New(b)) - b.AddHandler("remember", fact.NewRemember(b)) - b.AddHandler("your", your.New(b)) - b.AddHandler("counter", counter.New(b)) - b.AddHandler("reminder", reminder.New(b)) - b.AddHandler("babbler", babbler.New(b)) - b.AddHandler("zork", zork.New(b)) - b.AddHandler("rss", rss.New(b)) - b.AddHandler("reaction", reaction.New(b)) - b.AddHandler("emojifyme", emojifyme.New(b)) - b.AddHandler("twitch", twitch.New(b)) - b.AddHandler("inventory", inventory.New(b)) - b.AddHandler("rpgORdie", rpgORdie.New(b)) - b.AddHandler("sisyphus", sisyphus.New(b)) - b.AddHandler("tell", tell.New(b)) - b.AddHandler("couldashouldawoulda", couldashouldawoulda.New(b)) - b.AddHandler("nedepedia", nerdepedia.New(b)) + b.AddPlugin("admin", admin.New(b)) + b.AddPlugin("first", first.New(b)) + b.AddPlugin("leftpad", leftpad.New(b)) + b.AddPlugin("talker", talker.New(b)) + b.AddPlugin("dice", dice.New(b)) + b.AddPlugin("picker", picker.New(b)) + b.AddPlugin("beers", beers.New(b)) + b.AddPlugin("remember", fact.NewRemember(b)) + b.AddPlugin("your", your.New(b)) + b.AddPlugin("counter", counter.New(b)) + b.AddPlugin("reminder", reminder.New(b)) + b.AddPlugin("babbler", babbler.New(b)) + b.AddPlugin("zork", zork.New(b)) + b.AddPlugin("rss", rss.New(b)) + b.AddPlugin("reaction", reaction.New(b)) + b.AddPlugin("emojifyme", emojifyme.New(b)) + b.AddPlugin("twitch", twitch.New(b)) + b.AddPlugin("inventory", inventory.New(b)) + b.AddPlugin("rpgORdie", rpgORdie.New(b)) + b.AddPlugin("sisyphus", sisyphus.New(b)) + b.AddPlugin("tell", tell.New(b)) + b.AddPlugin("couldashouldawoulda", couldashouldawoulda.New(b)) + b.AddPlugin("nedepedia", nerdepedia.New(b)) // catches anything left, will always return true - b.AddHandler("factoid", fact.New(b)) - b.AddHandler("db", db.New(b)) + b.AddPlugin("factoid", fact.New(b)) + b.AddPlugin("db", db.New(b)) for { err := client.Serve() diff --git a/plugins/admin/admin.go b/plugins/admin/admin.go index a7e45c8..628125e 100644 --- a/plugins/admin/admin.go +++ b/plugins/admin/admin.go @@ -62,7 +62,7 @@ func (p *AdminPlugin) Message(message msg.Message) bool { if strings.ToLower(body) == "shut up" { dur := time.Duration(p.cfg.GetInt("quietDuration", 5)) * time.Minute log.Printf("Going to sleep for %v, %v", dur, time.Now().Add(dur)) - p.Bot.SendMessage(message.Channel, "Okay. I'll be back later.") + p.Bot.Send(bot.Message, message.Channel, "Okay. I'll be back later.") p.quiet = true go func() { select { @@ -76,19 +76,19 @@ func (p *AdminPlugin) Message(message msg.Message) bool { parts := strings.Split(body, " ") if parts[0] == "set" && len(parts) > 2 && forbiddenKeys[parts[1]] { - p.Bot.SendMessage(message.Channel, "You cannot access that key") + p.Bot.Send(bot.Message, message.Channel, "You cannot access that key") return true } else if parts[0] == "set" && len(parts) > 2 { p.cfg.Set(parts[1], strings.Join(parts[2:], " ")) - p.Bot.SendMessage(message.Channel, fmt.Sprintf("Set %s", parts[1])) + p.Bot.Send(bot.Message, message.Channel, fmt.Sprintf("Set %s", parts[1])) return true } if parts[0] == "get" && len(parts) == 2 && forbiddenKeys[parts[1]] { - p.Bot.SendMessage(message.Channel, "You cannot access that key") + p.Bot.Send(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], "") - p.Bot.SendMessage(message.Channel, fmt.Sprintf("%s: %s", parts[1], v)) + p.Bot.Send(bot.Message, message.Channel, fmt.Sprintf("%s: %s", parts[1], v)) return true } @@ -102,10 +102,10 @@ func (p *AdminPlugin) handleVariables(message msg.Message) bool { _, err := p.db.Exec(`delete from variables where name=? and value=?`, variable, value) if err != nil { - p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.") + p.Bot.Send(bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.") log.Println("[admin]: ", err) } else { - p.Bot.SendMessage(message.Channel, "Removed.") + p.Bot.Send(bot.Message, message.Channel, "Removed.") } return true @@ -123,28 +123,28 @@ func (p *AdminPlugin) handleVariables(message msg.Message) bool { row := p.db.QueryRow(`select count(*) from variables where value = ?`, variable, value) err := row.Scan(&count) if err != nil { - p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.") + p.Bot.Send(bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.") log.Println("[admin]: ", err) return true } if count > 0 { - p.Bot.SendMessage(message.Channel, "I've already got that one.") + p.Bot.Send(bot.Message, message.Channel, "I've already got that one.") } else { _, err := p.db.Exec(`INSERT INTO variables (name, value) VALUES (?, ?)`, variable, value) if err != nil { - p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.") + p.Bot.Send(bot.Message, message.Channel, "I'm broke and need attention in my variable creation code.") log.Println("[admin]: ", err) return true } - p.Bot.SendMessage(message.Channel, "Added.") + p.Bot.Send(bot.Message, message.Channel, "Added.") } return true } // Help responds to help requests. Every plugin must implement a help function. func (p *AdminPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "This does super secret things that you're not allowed to know about.") + p.Bot.Send(bot.Message, channel, "This does super secret things that you're not allowed to know about.") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/babbler/babbler.go b/plugins/babbler/babbler.go index 78ba647..38997ec 100644 --- a/plugins/babbler/babbler.go +++ b/plugins/babbler/babbler.go @@ -141,7 +141,7 @@ func (p *BabblerPlugin) Message(message msg.Message) bool { } if saidSomething { - p.Bot.SendMessage(message.Channel, saidWhat) + p.Bot.Send(bot.Message, message.Channel, saidWhat) } return saidSomething } @@ -155,7 +155,7 @@ func (p *BabblerPlugin) Help(channel string, parts []string) { "seabass says-middle-out ...", "seabass says-bridge ... | ...", } - p.Bot.SendMessage(channel, strings.Join(commands, "\n\n")) + p.Bot.Send(bot.Message, channel, strings.Join(commands, "\n\n")) } func (p *BabblerPlugin) Event(kind string, message msg.Message) bool { diff --git a/plugins/beers/beers.go b/plugins/beers/beers.go index d4da158..d81cfa0 100644 --- a/plugins/beers/beers.go +++ b/plugins/beers/beers.go @@ -81,13 +81,13 @@ func (p *BeersPlugin) Message(message msg.Message) bool { count, err := strconv.Atoi(parts[2]) if err != nil { // if it's not a number, maybe it's a nick! - p.Bot.SendMessage(channel, "Sorry, that didn't make any sense.") + p.Bot.Send(bot.Message, channel, "Sorry, that didn't make any sense.") } if count < 0 { // you can't be negative msg := fmt.Sprintf("Sorry %s, you can't have negative beers!", nick) - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) return true } if parts[1] == "+=" { @@ -101,14 +101,14 @@ func (p *BeersPlugin) Message(message msg.Message) bool { p.randomReply(channel) } } else { - p.Bot.SendMessage(channel, "I don't know your math.") + p.Bot.Send(bot.Message, channel, "I don't know your math.") } } else if len(parts) == 2 { if p.doIKnow(parts[1]) { p.reportCount(parts[1], channel, false) } else { msg := fmt.Sprintf("Sorry, I don't know %s.", parts[1]) - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) } } else if len(parts) == 1 { p.reportCount(nick, channel, true) @@ -132,7 +132,7 @@ func (p *BeersPlugin) Message(message msg.Message) bool { channel := message.Channel if len(parts) < 2 { - p.Bot.SendMessage(channel, "You must also provide a user name.") + p.Bot.Send(bot.Message, channel, "You must also provide a user name.") } else if len(parts) == 3 { chanNick = parts[2] } else if len(parts) == 4 { @@ -154,7 +154,7 @@ func (p *BeersPlugin) Message(message msg.Message) bool { log.Println("Error registering untappd: ", err) } if count > 0 { - p.Bot.SendMessage(channel, "I'm already watching you.") + p.Bot.Send(bot.Message, channel, "I'm already watching you.") return true } _, err = p.db.Exec(`insert into untappd ( @@ -170,11 +170,11 @@ func (p *BeersPlugin) Message(message msg.Message) bool { ) if err != nil { log.Println("Error registering untappd: ", err) - p.Bot.SendMessage(channel, "I can't see.") + p.Bot.Send(bot.Message, channel, "I can't see.") return true } - p.Bot.SendMessage(channel, "I'll be watching you.") + p.Bot.Send(bot.Message, channel, "I'll be watching you.") p.checkUntappd(channel) @@ -200,7 +200,7 @@ func (p *BeersPlugin) Help(channel string, parts []string) { msg := "Beers: imbibe by using either beers +=,=,++ or with the !imbibe/drink " + "commands. I'll keep a count of how many beers you've had and then if you want " + "to reset, just !puke it all up!" - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) } func getUserBeers(db *sqlx.DB, user string) counter.Item { @@ -239,13 +239,13 @@ func (p *BeersPlugin) reportCount(nick, channel string, himself bool) { msg = fmt.Sprintf("You've had %d beers so far, %s.", beers, nick) } } - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) } func (p *BeersPlugin) puke(user string, channel string) { p.setBeers(user, 0) msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user) - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) } func (p *BeersPlugin) doIKnow(nick string) bool { @@ -260,7 +260,7 @@ func (p *BeersPlugin) doIKnow(nick string) bool { // Sends random affirmation to the channel. This could be better (with a datastore for sayings) func (p *BeersPlugin) randomReply(channel string) { replies := []string{"ZIGGY! ZAGGY!", "HIC!", "Stay thirsty, my friend!"} - p.Bot.SendMessage(channel, replies[rand.Intn(len(replies))]) + p.Bot.Send(bot.Message, channel, replies[rand.Intn(len(replies))]) } type checkin struct { @@ -421,7 +421,7 @@ func (p *BeersPlugin) checkUntappd(channel string) { } log.Println("checkin id:", checkin.Checkin_id, "Message:", msg) - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) } } diff --git a/plugins/couldashouldawoulda/csw.go b/plugins/couldashouldawoulda/csw.go index 455c45d..1a99317 100644 --- a/plugins/couldashouldawoulda/csw.go +++ b/plugins/couldashouldawoulda/csw.go @@ -63,7 +63,7 @@ func (p *CSWPlugin) Message(message msg.Message) bool { } } - p.Bot.SendMessage(message.Channel, responses[rand.Intn(len(responses))]) + p.Bot.Send(bot.Message, message.Channel, responses[rand.Intn(len(responses))]) return true } diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 2c7668a..0100d79 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -211,7 +211,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { log.Println(err) return false } - p.Bot.SendMessage(channel, fmt.Sprintf("Created alias %s -> %s", + p.Bot.Send(bot.Message, channel, fmt.Sprintf("Created alias %s -> %s", parts[1], parts[2])) return true } else if strings.ToLower(parts[0]) == "leaderboard" { @@ -241,7 +241,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { it.Item, ) } - p.Bot.SendMessage(channel, out) + p.Bot.Send(bot.Message, channel, out) return true } else if match := teaMatcher.MatchString(message.Body); match { // check for tea match TTT @@ -250,14 +250,14 @@ func (p *CounterPlugin) Message(message msg.Message) bool { items, err := GetItems(p.DB, strings.ToLower(nick)) if err != nil { log.Printf("Error getting items to reset %s: %s", nick, err) - p.Bot.SendMessage(channel, "Something is technically wrong with your counters.") + p.Bot.Send(bot.Message, channel, "Something is technically wrong with your counters.") return true } log.Printf("Items: %+v", items) for _, item := range items { item.Delete() } - p.Bot.SendMessage(channel, fmt.Sprintf("%s, you are as new, my son.", nick)) + p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s, you are as new, my son.", nick)) return true } else if message.Command && parts[0] == "inspect" && len(parts) == 2 { var subject string @@ -273,7 +273,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { items, err := GetItems(p.DB, subject) if err != nil { log.Fatalf("Error retrieving items for %s: %s", subject, err) - p.Bot.SendMessage(channel, "Something went wrong finding that counter;") + p.Bot.Send(bot.Message, channel, "Something went wrong finding that counter;") return true } @@ -293,11 +293,11 @@ func (p *CounterPlugin) Message(message msg.Message) bool { resp += "." if count == 0 { - p.Bot.SendMessage(channel, fmt.Sprintf("%s has no counters.", subject)) + p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has no counters.", subject)) return true } - p.Bot.SendMessage(channel, resp) + p.Bot.Send(bot.Message, channel, resp) return true } else if message.Command && len(parts) == 2 && parts[0] == "clear" { subject := strings.ToLower(nick) @@ -306,17 +306,17 @@ func (p *CounterPlugin) Message(message msg.Message) bool { it, err := GetItem(p.DB, subject, itemName) if err != nil { log.Printf("Error getting item to remove %s.%s: %s", subject, itemName, err) - p.Bot.SendMessage(channel, "Something went wrong removing that counter;") + p.Bot.Send(bot.Message, channel, "Something went wrong removing that counter;") return true } err = it.Delete() if err != nil { log.Printf("Error removing item %s.%s: %s", subject, itemName, err) - p.Bot.SendMessage(channel, "Something went wrong removing that counter;") + p.Bot.Send(bot.Message, channel, "Something went wrong removing that counter;") return true } - p.Bot.SendAction(channel, fmt.Sprintf("chops a few %s out of his brain", + p.Bot.Send(bot.Action, channel, fmt.Sprintf("chops a few %s out of his brain", itemName)) return true @@ -339,7 +339,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { item, err := GetItem(p.DB, subject, itemName) switch { case err == sql.ErrNoRows: - p.Bot.SendMessage(channel, fmt.Sprintf("I don't think %s has any %s.", + p.Bot.Send(bot.Message, channel, fmt.Sprintf("I don't think %s has any %s.", subject, itemName)) return true case err != nil: @@ -348,7 +348,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { return true } - p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, item.Count, + p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, item.Count, itemName)) return true @@ -379,7 +379,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { } log.Printf("About to update item: %#v", item) item.UpdateDelta(1) - p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, + p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, item.Count, item.Item)) return true } else if strings.HasSuffix(parts[0], "--") { @@ -391,7 +391,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { return false } item.UpdateDelta(-1) - p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, + p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, item.Count, item.Item)) return true } @@ -420,7 +420,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { n, _ := strconv.Atoi(parts[2]) log.Printf("About to update item by %d: %#v", n, item) item.UpdateDelta(n) - p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, + p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, item.Count, item.Item)) return true } else if parts[1] == "-=" { @@ -434,7 +434,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { n, _ := strconv.Atoi(parts[2]) log.Printf("About to update item by -%d: %#v", n, item) item.UpdateDelta(-n) - p.Bot.SendMessage(channel, fmt.Sprintf("%s has %d %s.", subject, + p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has %d %s.", subject, item.Count, item.Item)) return true } @@ -445,7 +445,7 @@ func (p *CounterPlugin) Message(message msg.Message) bool { // Help responds to help requests. Every plugin must implement a help function. func (p *CounterPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "You can set counters incrementally by using "+ + p.Bot.Send(bot.Message, channel, "You can set counters incrementally by using "+ "++ and --. You can see all of your counters using "+ "\"inspect\", erase them with \"clear\", and view single counters with "+ "\"count\".") @@ -487,7 +487,7 @@ func (p *CounterPlugin) checkMatch(message msg.Message) bool { } log.Printf("About to update item: %#v", item) item.UpdateDelta(1) - p.Bot.SendMessage(channel, fmt.Sprintf("bleep-bloop-blop... %s has %d %s", + p.Bot.Send(bot.Message, channel, fmt.Sprintf("bleep-bloop-blop... %s has %d %s", nick, item.Count, itemName)) return true } diff --git a/plugins/dice/dice.go b/plugins/dice/dice.go index 104e016..51bbd72 100644 --- a/plugins/dice/dice.go +++ b/plugins/dice/dice.go @@ -46,7 +46,7 @@ func (p *DicePlugin) Message(message msg.Message) bool { } if sides < 2 || nDice < 1 || nDice > 20 { - p.Bot.SendMessage(channel, "You're a dick.") + p.Bot.Send(bot.Message, channel, "You're a dick.") return true } @@ -61,14 +61,14 @@ func (p *DicePlugin) Message(message msg.Message) bool { } } - p.Bot.SendMessage(channel, rolls) + p.Bot.Send(bot.Message, channel, rolls) return true } // Help responds to help requests. Every plugin must implement a help function. func (p *DicePlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "Roll dice using notation XdY. Try \"3d20\".") + p.Bot.Send(bot.Message, channel, "Roll dice using notation XdY. Try \"3d20\".") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/downtime/downtime.go b/plugins/downtime/downtime.go index e50df61..335886e 100644 --- a/plugins/downtime/downtime.go +++ b/plugins/downtime/downtime.go @@ -142,9 +142,9 @@ func (p *DowntimePlugin) Message(message msg.Message) bool { } if !entry.id.Valid { // couldn't find em - p.Bot.SendMessage(channel, fmt.Sprintf("Sorry, I don't know %s.", nick)) + p.Bot.Send(bot.Message, channel, fmt.Sprintf("Sorry, I don't know %s.", nick)) } else { - p.Bot.SendMessage(channel, fmt.Sprintf("%s has been idle for: %s", + p.Bot.Send(bot.Message, channel, fmt.Sprintf("%s has been idle for: %s", nick, time.Now().Sub(entry.lastSeen))) } ret = true @@ -165,7 +165,7 @@ func (p *DowntimePlugin) Message(message msg.Message) bool { tops = fmt.Sprintf("%s%s: %s ", tops, e.nick, time.Now().Sub(e.lastSeen)) } } - p.Bot.SendMessage(channel, tops) + p.Bot.Send(bot.Message, channel, tops) ret = true } @@ -197,7 +197,7 @@ func (p *DowntimePlugin) remove(user string) error { // Help responds to help requests. Every plugin must implement a help function. func (p *DowntimePlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "Ask me how long one of your friends has been idele with, \"idle \"") + p.Bot.Send(bot.Message, channel, "Ask me how long one of your friends has been idele with, \"idle \"") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/emojifyme/emojifyme.go b/plugins/emojifyme/emojifyme.go index 26b5b72..b3614fd 100644 --- a/plugins/emojifyme/emojifyme.go +++ b/plugins/emojifyme/emojifyme.go @@ -90,7 +90,7 @@ func (p *EmojifyMePlugin) Message(message msg.Message) bool { if emojied > 0 && rand.Float64() <= p.Bot.Config().GetFloat64("Emojify.Chance", 0.02)*emojied { for _, e := range emojys { - p.Bot.React(message.Channel, e, message) + p.Bot.Send(bot.Reaction, message.Channel, e, message) } return true } diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index c53c934..fc2dfad 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -405,13 +405,13 @@ func (p *Factoid) sayFact(message msg.Message, fact factoid) { } if fact.Verb == "action" { - p.Bot.SendAction(message.Channel, msg) + p.Bot.Send(bot.Action, message.Channel, msg) } else if fact.Verb == "react" { - p.Bot.React(message.Channel, msg, message) + p.Bot.Send(bot.Reaction, message.Channel, msg, message) } else if fact.Verb == "reply" { - p.Bot.SendMessage(message.Channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) } else { - p.Bot.SendMessage(message.Channel, full) + p.Bot.Send(bot.Message, message.Channel, full) } } @@ -457,7 +457,7 @@ func (p *Factoid) tellThemWhatThatWas(message msg.Message) bool { msg = fmt.Sprintf("That was (#%d) '%s <%s> %s'", fact.id.Int64, fact.Fact, fact.Verb, fact.Tidbit) } - p.Bot.SendMessage(message.Channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) return true } @@ -475,21 +475,21 @@ func (p *Factoid) learnAction(message msg.Message, action string) bool { action = strings.TrimSpace(action) if len(trigger) == 0 || len(fact) == 0 || len(action) == 0 { - p.Bot.SendMessage(message.Channel, "I don't want to learn that.") + p.Bot.Send(bot.Message, message.Channel, "I don't want to learn that.") return true } if len(strings.Split(fact, "$and")) > 4 { - p.Bot.SendMessage(message.Channel, "You can't use more than 4 $and operators.") + p.Bot.Send(bot.Message, message.Channel, "You can't use more than 4 $and operators.") return true } strippedaction := strings.Replace(strings.Replace(action, "<", "", 1), ">", "", 1) if err := p.learnFact(message, trigger, strippedaction, fact); err != nil { - p.Bot.SendMessage(message.Channel, err.Error()) + p.Bot.Send(bot.Message, message.Channel, err.Error()) } else { - p.Bot.SendMessage(message.Channel, fmt.Sprintf("Okay, %s.", message.User.Name)) + p.Bot.Send(bot.Message, message.Channel, fmt.Sprintf("Okay, %s.", message.User.Name)) } return true @@ -509,7 +509,7 @@ func changeOperator(body string) string { // an admin, it may be deleted func (p *Factoid) forgetLastFact(message msg.Message) bool { if p.LastFact == nil { - p.Bot.SendMessage(message.Channel, "I refuse.") + p.Bot.Send(bot.Message, message.Channel, "I refuse.") return true } @@ -519,7 +519,7 @@ func (p *Factoid) forgetLastFact(message msg.Message) bool { } fmt.Printf("Forgot #%d: %s %s %s\n", p.LastFact.id.Int64, p.LastFact.Fact, p.LastFact.Verb, p.LastFact.Tidbit) - p.Bot.SendAction(message.Channel, "hits himself over the head with a skillet") + p.Bot.Send(bot.Action, message.Channel, "hits himself over the head with a skillet") p.LastFact = nil return true @@ -539,7 +539,7 @@ func (p *Factoid) changeFact(message msg.Message) bool { if len(parts) == 4 { // replacement if parts[0] != "s" { - p.Bot.SendMessage(message.Channel, "Nah.") + p.Bot.Send(bot.Message, message.Channel, "Nah.") } find := parts[1] replace := parts[2] @@ -554,10 +554,10 @@ func (p *Factoid) changeFact(message msg.Message) bool { } // make the changes msg := fmt.Sprintf("Changing %d facts.", len(result)) - p.Bot.SendMessage(message.Channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) reg, err := regexp.Compile(find) if err != nil { - p.Bot.SendMessage(message.Channel, "I don't really want to.") + p.Bot.Send(bot.Message, message.Channel, "I don't really want to.") return false } for _, fact := range result { @@ -574,12 +574,12 @@ func (p *Factoid) changeFact(message msg.Message) bool { result, err := getFacts(p.db, trigger, parts[1]) if err != nil { log.Println("Error getting facts: ", trigger, err) - p.Bot.SendMessage(message.Channel, "bzzzt") + p.Bot.Send(bot.Message, message.Channel, "bzzzt") return true } count := len(result) if count == 0 { - p.Bot.SendMessage(message.Channel, "I didn't find any facts like that.") + p.Bot.Send(bot.Message, message.Channel, "I didn't find any facts like that.") return true } if parts[2] == "g" && len(result) > 4 { @@ -599,9 +599,9 @@ func (p *Factoid) changeFact(message msg.Message) bool { if count > 4 { msg = fmt.Sprintf("%s | ...and %d others", msg, count) } - p.Bot.SendMessage(message.Channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) } else { - p.Bot.SendMessage(message.Channel, "I don't know what you mean.") + p.Bot.Send(bot.Message, message.Channel, "I don't know what you mean.") } return true } @@ -625,14 +625,14 @@ func (p *Factoid) Message(message msg.Message) bool { m := strings.TrimPrefix(message.Body, "alias ") parts := strings.SplitN(m, "->", 2) if len(parts) != 2 { - p.Bot.SendMessage(message.Channel, "If you want to alias something, use: `alias this -> that`") + p.Bot.Send(bot.Message, message.Channel, "If you want to alias something, use: `alias this -> that`") return true } a := aliasFromStrings(strings.TrimSpace(parts[1]), strings.TrimSpace(parts[0])) if err := a.save(p.db); err != nil { - p.Bot.SendMessage(message.Channel, err.Error()) + p.Bot.Send(bot.Message, message.Channel, err.Error()) } else { - p.Bot.SendAction(message.Channel, "learns a new synonym") + p.Bot.Send(bot.Action, message.Channel, "learns a new synonym") } return true } @@ -664,14 +664,14 @@ func (p *Factoid) Message(message msg.Message) bool { } // We didn't find anything, panic! - p.Bot.SendMessage(message.Channel, p.NotFound[rand.Intn(len(p.NotFound))]) + p.Bot.Send(bot.Message, message.Channel, p.NotFound[rand.Intn(len(p.NotFound))]) return true } // Help responds to help requests. Every plugin must implement a help function. func (p *Factoid) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "I can learn facts and spit them back out. You can say \"this is that\" or \"he $5\". Later, trigger the factoid by just saying the trigger word, \"this\" or \"he\" in these examples.") - p.Bot.SendMessage(channel, "I can also figure out some variables including: $nonzero, $digit, $nick, and $someone.") + p.Bot.Send(bot.Message, channel, "I can learn facts and spit them back out. You can say \"this is that\" or \"he $5\". Later, trigger the factoid by just saying the trigger word, \"this\" or \"he\" in these examples.") + p.Bot.Send(bot.Message, channel, "I can also figure out some variables including: $nonzero, $digit, $nick, and $someone.") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/fact/remember.go b/plugins/fact/remember.go index 1038eb7..09ecf7b 100644 --- a/plugins/fact/remember.go +++ b/plugins/fact/remember.go @@ -40,7 +40,7 @@ func (p *RememberPlugin) Message(message msg.Message) bool { if strings.ToLower(message.Body) == "quote" && message.Command { q := p.randQuote() - p.Bot.SendMessage(message.Channel, q) + p.Bot.Send(bot.Message, message.Channel, q) // is it evil not to remember that the user said quote? return true @@ -87,7 +87,7 @@ func (p *RememberPlugin) Message(message msg.Message) bool { } if err := fact.save(p.db); err != nil { log.Println("ERROR!!!!:", err) - p.Bot.SendMessage(message.Channel, "Tell somebody I'm broke.") + p.Bot.Send(bot.Message, message.Channel, "Tell somebody I'm broke.") } log.Println("Remembering factoid:", msg) @@ -95,14 +95,14 @@ func (p *RememberPlugin) Message(message msg.Message) bool { // sorry, not creative with names so we're reusing msg msg = fmt.Sprintf("Okay, %s, remembering '%s'.", message.User.Name, msg) - p.Bot.SendMessage(message.Channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) p.recordMsg(message) return true } } - p.Bot.SendMessage(message.Channel, "Sorry, I don't know that phrase.") + p.Bot.Send(bot.Message, message.Channel, "Sorry, I don't know that phrase.") p.recordMsg(message) return true } @@ -118,7 +118,7 @@ func (p *RememberPlugin) Help(channel string, parts []string) { "be any part of their message. Later on, you can ask for a random " + "!quote." - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) } // deliver a random quote out of the db. diff --git a/plugins/first/first.go b/plugins/first/first.go index a70f6e7..af3ffcc 100644 --- a/plugins/first/first.go +++ b/plugins/first/first.go @@ -195,7 +195,7 @@ func (p *FirstPlugin) recordFirst(message msg.Message) { func (p *FirstPlugin) announceFirst(message msg.Message) { c := message.Channel if p.First != nil { - p.Bot.SendMessage(c, fmt.Sprintf("%s had first at %s with the message: \"%s\"", + p.Bot.Send(bot.Message, c, fmt.Sprintf("%s had first at %s with the message: \"%s\"", p.First.nick, p.First.time.Format("15:04"), p.First.body)) } } @@ -209,7 +209,7 @@ func (p *FirstPlugin) LoadData() { // Help responds to help requests. Every plugin must implement a help function. func (p *FirstPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "Sorry, First does not do a goddamn thing.") + p.Bot.Send(bot.Message, channel, "Sorry, First does not do a goddamn thing.") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/inventory/inventory.go b/plugins/inventory/inventory.go index 708639f..072a22c 100644 --- a/plugins/inventory/inventory.go +++ b/plugins/inventory/inventory.go @@ -86,7 +86,7 @@ func (p *InventoryPlugin) Message(message msg.Message) bool { log.Printf("I think I have more than 0 items: %+v, len(items)=%d", items, len(items)) say = fmt.Sprintf("I'm currently holding %s", strings.Join(items, ", ")) } - p.bot.SendMessage(message.Channel, say) + p.bot.Send(bot.Message, message.Channel, say) return true } @@ -197,7 +197,7 @@ func (p *InventoryPlugin) remove(i string) { func (p *InventoryPlugin) addItem(m msg.Message, i string) bool { if p.exists(i) { - p.bot.SendMessage(m.Channel, fmt.Sprintf("I already have %s.", i)) + p.bot.Send(bot.Message, m.Channel, fmt.Sprintf("I already have %s.", i)) return true } var removed string @@ -210,9 +210,9 @@ func (p *InventoryPlugin) addItem(m msg.Message, i string) bool { log.Printf("Error inserting new inventory item: %s", err) } if removed != "" { - p.bot.SendAction(m.Channel, fmt.Sprintf("dropped %s and took %s from %s", removed, i, m.User.Name)) + p.bot.Send(bot.Action, m.Channel, fmt.Sprintf("dropped %s and took %s from %s", removed, i, m.User.Name)) } else { - p.bot.SendAction(m.Channel, fmt.Sprintf("takes %s from %s", i, m.User.Name)) + p.bot.Send(bot.Action, m.Channel, fmt.Sprintf("takes %s from %s", i, m.User.Name)) } return true } diff --git a/plugins/leftpad/leftpad.go b/plugins/leftpad/leftpad.go index cf5327e..42a4493 100644 --- a/plugins/leftpad/leftpad.go +++ b/plugins/leftpad/leftpad.go @@ -42,20 +42,20 @@ func (p *LeftpadPlugin) Message(message msg.Message) bool { padchar := parts[1] length, err := strconv.Atoi(parts[2]) if err != nil { - p.bot.SendMessage(message.Channel, "Invalid padding number") + p.bot.Send(bot.Message, message.Channel, "Invalid padding number") return true } maxLen, who := p.config.GetInt("LeftPad.MaxLen", 50), p.config.Get("LeftPad.Who", "Putin") if length > maxLen && maxLen > 0 { msg := fmt.Sprintf("%s would kill me if I did that.", who) - p.bot.SendMessage(message.Channel, msg) + p.bot.Send(bot.Message, message.Channel, msg) return true } text := strings.Join(parts[3:], " ") res := leftpad.LeftPad(text, length, padchar) - p.bot.SendMessage(message.Channel, res) + p.bot.Send(bot.Message, message.Channel, res) return true } diff --git a/plugins/nerdepedia/nerdepedia.go b/plugins/nerdepedia/nerdepedia.go index 1da2514..f0e0096 100644 --- a/plugins/nerdepedia/nerdepedia.go +++ b/plugins/nerdepedia/nerdepedia.go @@ -78,7 +78,7 @@ func (p *NerdepediaPlugin) Message(message msg.Message) bool { } if description != "" && link != "" { - p.bot.SendMessage(message.Channel, fmt.Sprintf("%s (%s)", description, link)) + p.bot.Send(bot.Message, message.Channel, fmt.Sprintf("%s (%s)", description, link)) return true } } @@ -88,7 +88,7 @@ func (p *NerdepediaPlugin) Message(message msg.Message) bool { // Help responds to help requests. Every plugin must implement a help function. func (p *NerdepediaPlugin) Help(channel string, parts []string) { - p.bot.SendMessage(channel, "nerd stuff") + p.bot.Send(bot.Message, channel, "nerd stuff") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/picker/picker.go b/plugins/picker/picker.go index 53b5f4b..290a490 100644 --- a/plugins/picker/picker.go +++ b/plugins/picker/picker.go @@ -36,14 +36,14 @@ func (p *PickerPlugin) Message(message msg.Message) bool { n, items, err := p.parse(message.Body) if err != nil { - p.Bot.SendMessage(message.Channel, err.Error()) + p.Bot.Send(bot.Message, message.Channel, err.Error()) return true } if n == 1 { item := items[rand.Intn(len(items))] out := fmt.Sprintf("I've chosen %q for you.", strings.TrimSpace(item)) - p.Bot.SendMessage(message.Channel, out) + p.Bot.Send(bot.Message, message.Channel, out) return true } @@ -59,7 +59,7 @@ func (p *PickerPlugin) Message(message msg.Message) bool { fmt.Fprintf(&b, ", %q", item) } b.WriteString(" }") - p.Bot.SendMessage(message.Channel, b.String()) + p.Bot.Send(bot.Message, message.Channel, b.String()) return true } @@ -109,7 +109,7 @@ func (p *PickerPlugin) parse(body string) (int, []string, error) { // Help responds to help requests. Every plugin must implement a help function. func (p *PickerPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "Choose from a list of options. Try \"pick {a,b,c}\".") + p.Bot.Send(bot.Message, channel, "Choose from a list of options. Try \"pick {a,b,c}\".") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/reaction/reaction.go b/plugins/reaction/reaction.go index 6f98d55..6c13201 100644 --- a/plugins/reaction/reaction.go +++ b/plugins/reaction/reaction.go @@ -56,7 +56,7 @@ func (p *ReactionPlugin) Message(message msg.Message) bool { reaction = p.Config.GetArray("Reaction.NegativeReactions", []string{})[index] } - p.Bot.React(message.Channel, reaction, message) + p.Bot.Send(bot.Reaction, message.Channel, reaction, message) } return false diff --git a/plugins/reminder/reminder.go b/plugins/reminder/reminder.go index 4e00e9c..df38f04 100644 --- a/plugins/reminder/reminder.go +++ b/plugins/reminder/reminder.go @@ -85,7 +85,7 @@ func (p *ReminderPlugin) Message(message msg.Message) bool { dur, err := time.ParseDuration(parts[3]) if err != nil { - p.Bot.SendMessage(channel, "Easy cowboy, not sure I can parse that duration.") + p.Bot.Send(bot.Message, channel, "Easy cowboy, not sure I can parse that duration.") return true } @@ -113,7 +113,7 @@ func (p *ReminderPlugin) Message(message msg.Message) bool { //remind who every dur for dur2 blah dur2, err := time.ParseDuration(parts[5]) if err != nil { - p.Bot.SendMessage(channel, "Easy cowboy, not sure I can parse that duration.") + p.Bot.Send(bot.Message, channel, "Easy cowboy, not sure I can parse that duration.") return true } @@ -124,7 +124,7 @@ func (p *ReminderPlugin) Message(message msg.Message) bool { max := p.config.GetInt("Reminder.MaxBatchAdd", 10) for i := 0; when.Before(endTime); i++ { if i >= max { - p.Bot.SendMessage(channel, "Easy cowboy, that's a lot of reminders. I'll add some of them.") + p.Bot.Send(bot.Message, channel, "Easy cowboy, that's a lot of reminders. I'll add some of them.") doConfirm = false break } @@ -141,14 +141,14 @@ func (p *ReminderPlugin) Message(message msg.Message) bool { when = when.Add(dur) } } else { - p.Bot.SendMessage(channel, "Easy cowboy, not sure I comprehend what you're asking.") + p.Bot.Send(bot.Message, channel, "Easy cowboy, not sure I comprehend what you're asking.") return true } if doConfirm && from == who { - p.Bot.SendMessage(channel, fmt.Sprintf("Okay. I'll remind you.")) + p.Bot.Send(bot.Message, channel, fmt.Sprintf("Okay. I'll remind you.")) } else if doConfirm { - p.Bot.SendMessage(channel, fmt.Sprintf("Sure %s, I'll remind %s.", from, who)) + p.Bot.Send(bot.Message, channel, fmt.Sprintf("Sure %s, I'll remind %s.", from, who)) } p.queueUpNextReminder() @@ -168,22 +168,22 @@ func (p *ReminderPlugin) Message(message msg.Message) bool { } } if err != nil { - p.Bot.SendMessage(channel, "listing failed.") + p.Bot.Send(bot.Message, channel, "listing failed.") } else { - p.Bot.SendMessage(channel, response) + p.Bot.Send(bot.Message, channel, response) } return true } else if len(parts) == 3 && strings.ToLower(parts[0]) == "cancel" && strings.ToLower(parts[1]) == "reminder" { id, err := strconv.ParseInt(parts[2], 10, 64) if err != nil { - p.Bot.SendMessage(channel, fmt.Sprintf("couldn't parse id: %s", parts[2])) + p.Bot.Send(bot.Message, channel, fmt.Sprintf("couldn't parse id: %s", parts[2])) } else { err := p.deleteReminder(id) if err == nil { - p.Bot.SendMessage(channel, fmt.Sprintf("successfully canceled reminder: %s", parts[2])) + p.Bot.Send(bot.Message, channel, fmt.Sprintf("successfully canceled reminder: %s", parts[2])) } else { - p.Bot.SendMessage(channel, fmt.Sprintf("failed to find and cancel reminder: %s", parts[2])) + p.Bot.Send(bot.Message, channel, fmt.Sprintf("failed to find and cancel reminder: %s", parts[2])) } } return true @@ -193,7 +193,7 @@ func (p *ReminderPlugin) Message(message msg.Message) bool { } func (p *ReminderPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "Pester someone with a reminder. Try \"remind in message\".\n\nUnsure about duration syntax? Check https://golang.org/pkg/time/#ParseDuration") + p.Bot.Send(bot.Message, channel, "Pester someone with a reminder. Try \"remind in message\".\n\nUnsure about duration syntax? Check https://golang.org/pkg/time/#ParseDuration") } func (p *ReminderPlugin) Event(kind string, message msg.Message) bool { @@ -353,7 +353,7 @@ func reminderer(p *ReminderPlugin) { message = fmt.Sprintf("Hey %s, %s wanted you to be reminded: %s", reminder.who, reminder.from, reminder.what) } - p.Bot.SendMessage(reminder.channel, message) + p.Bot.Send(bot.Message, reminder.channel, message) if err := p.deleteReminder(reminder.id); err != nil { log.Print(reminder.id) diff --git a/plugins/rpgORdie/rpgORdie.go b/plugins/rpgORdie/rpgORdie.go index 48394d4..8c69174 100644 --- a/plugins/rpgORdie/rpgORdie.go +++ b/plugins/rpgORdie/rpgORdie.go @@ -107,9 +107,9 @@ func New(b bot.Bot) *RPGPlugin { func (p *RPGPlugin) Message(message msg.Message) bool { if strings.ToLower(message.Body) == "start rpg" { b := NewRandomBoard() - ts := p.Bot.SendMessage(message.Channel, b.toMessageString()) + _, ts := p.Bot.Send(bot.Message, message.Channel, b.toMessageString()) p.listenFor[ts] = b - p.Bot.ReplyToMessageIdentifier(message.Channel, "Over here.", ts) + p.Bot.Send(bot.Reply, message.Channel, "Over here.", ts) return true } return false @@ -120,7 +120,7 @@ func (p *RPGPlugin) LoadData() { } func (p *RPGPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "Go find a walkthrough or something.") + p.Bot.Send(bot.Message, channel, "Go find a walkthrough or something.") } func (p *RPGPlugin) Event(kind string, message msg.Message) bool { @@ -155,12 +155,12 @@ func (p *RPGPlugin) ReplyMessage(message msg.Message, identifier string) bool { switch res { case OK: - p.Bot.Edit(message.Channel, b.toMessageString(), identifier) + p.Bot.Send(bot.Edit, message.Channel, b.toMessageString(), identifier) case WIN: - p.Bot.Edit(message.Channel, b.toMessageString(), identifier) - p.Bot.ReplyToMessageIdentifier(message.Channel, "congratulations, you beat the easiest level imaginable.", identifier) + p.Bot.Send(bot.Edit, message.Channel, b.toMessageString(), identifier) + p.Bot.Send(bot.Reply, message.Channel, "congratulations, you beat the easiest level imaginable.", identifier) case INVALID: - p.Bot.ReplyToMessageIdentifier(message.Channel, fmt.Sprintf("you can't move %s", message.Body), identifier) + p.Bot.Send(bot.Reply, message.Channel, fmt.Sprintf("you can't move %s", message.Body), identifier) } return true } diff --git a/plugins/rss/rss.go b/plugins/rss/rss.go index 37a5b1d..c7bed49 100644 --- a/plugins/rss/rss.go +++ b/plugins/rss/rss.go @@ -64,13 +64,13 @@ func (p *RSSPlugin) Message(message msg.Message) bool { if numTokens == 2 && strings.ToLower(tokens[0]) == "rss" { if item, ok := p.cache[strings.ToLower(tokens[1])]; ok && time.Now().Before(item.expiration) { - p.Bot.SendMessage(message.Channel, item.getCurrentPage(p.maxLines)) + p.Bot.Send(bot.Message, message.Channel, item.getCurrentPage(p.maxLines)) return true } else { fp := gofeed.NewParser() feed, err := fp.ParseURL(tokens[1]) if err != nil { - p.Bot.SendMessage(message.Channel, fmt.Sprintf("RSS error: %s", err.Error())) + p.Bot.Send(bot.Message, message.Channel, fmt.Sprintf("RSS error: %s", err.Error())) return true } item := &cacheItem{ @@ -86,7 +86,7 @@ func (p *RSSPlugin) Message(message msg.Message) bool { p.cache[strings.ToLower(tokens[1])] = item - p.Bot.SendMessage(message.Channel, item.getCurrentPage(p.maxLines)) + p.Bot.Send(bot.Message, message.Channel, item.getCurrentPage(p.maxLines)) return true } } @@ -100,7 +100,7 @@ func (p *RSSPlugin) LoadData() { // Help responds to help requests. Every plugin must implement a help function. func (p *RSSPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "try '!rss http://rss.cnn.com/rss/edition.rss'") + p.Bot.Send(bot.Message, channel, "try '!rss http://rss.cnn.com/rss/edition.rss'") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/sisyphus/sisyphus.go b/plugins/sisyphus/sisyphus.go index 91a9fbc..a96ce74 100644 --- a/plugins/sisyphus/sisyphus.go +++ b/plugins/sisyphus/sisyphus.go @@ -37,17 +37,17 @@ type game struct { nextAns int } -func NewRandomGame(bot bot.Bot, channel, who string) *game { +func NewRandomGame(b bot.Bot, channel, who string) *game { size := rand.Intn(9) + 2 g := game{ channel: channel, - bot: bot, + bot: b, who: who, start: time.Now(), size: size, current: size / 2, } - g.id = bot.SendMessage(channel, g.toMessageString()) + _, g.id = b.Send(bot.Message, channel, g.toMessageString()) g.schedulePush() g.scheduleDecrement() @@ -98,11 +98,11 @@ func (g *game) endGame() { func (g *game) handleDecrement() { g.current++ - g.bot.Edit(g.channel, g.toMessageString(), g.id) + g.bot.Send(bot.Edit, g.channel, g.toMessageString(), g.id) if g.current > g.size-2 { - g.bot.ReplyToMessageIdentifier(g.channel, "you lose", g.id) + g.bot.Send(bot.Reply, g.channel, "you lose", g.id) msg := fmt.Sprintf("%s just lost the game after %s", g.who, time.Now().Sub(g.start)) - g.bot.SendMessage(g.channel, msg) + g.bot.Send(bot.Message, g.channel, msg) g.endGame() } else { g.scheduleDecrement() @@ -110,7 +110,7 @@ func (g *game) handleDecrement() { } func (g *game) handleNotify() { - g.bot.ReplyToMessageIdentifier(g.channel, "You can push now.\n"+g.generateQuestion(), g.id) + g.bot.Send(bot.Reply, g.channel, "You can push now.\n"+g.generateQuestion(), g.id) } func (g *game) generateQuestion() string { @@ -172,14 +172,14 @@ func (p *SisyphusPlugin) Message(message msg.Message) bool { if strings.ToLower(message.Body) == "start sisyphus" { b := NewRandomGame(p.Bot, message.Channel, message.User.Name) p.listenFor[b.id] = b - p.Bot.ReplyToMessageIdentifier(message.Channel, "Over here.", b.id) + p.Bot.Send(bot.Reply, message.Channel, "Over here.", b.id) return true } return false } func (p *SisyphusPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "https://en.wikipedia.org/wiki/Sisyphus") + p.Bot.Send(bot.Message, channel, "https://en.wikipedia.org/wiki/Sisyphus") } func (p *SisyphusPlugin) Event(kind string, message msg.Message) bool { @@ -211,18 +211,18 @@ func (p *SisyphusPlugin) ReplyMessage(message msg.Message, identifier string) bo if time.Now().After(g.nextPush) { if g.checkAnswer(message.Body) { - p.Bot.Edit(message.Channel, g.toMessageString(), identifier) + p.Bot.Send(bot.Edit, message.Channel, g.toMessageString(), identifier) g.schedulePush() msg := fmt.Sprintf("Ok. You can push again in %s", g.nextPush.Sub(time.Now())) - p.Bot.ReplyToMessageIdentifier(message.Channel, msg, identifier) + p.Bot.Send(bot.Reply, message.Channel, msg, identifier) } else { - p.Bot.ReplyToMessageIdentifier(message.Channel, "you lose", identifier) + p.Bot.Send(bot.Reply, message.Channel, "you lose", identifier) msg := fmt.Sprintf("%s just lost the sisyphus game after %s", g.who, time.Now().Sub(g.start)) - p.Bot.SendMessage(message.Channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) g.endGame() } } else { - p.Bot.ReplyToMessageIdentifier(message.Channel, "you cannot push yet", identifier) + p.Bot.Send(bot.Reply, message.Channel, "you cannot push yet", identifier) } return true } diff --git a/plugins/talker/talker.go b/plugins/talker/talker.go index b96866f..f4abdd7 100644 --- a/plugins/talker/talker.go +++ b/plugins/talker/talker.go @@ -57,7 +57,7 @@ func (p *TalkerPlugin) Message(message msg.Message) bool { // TODO: This ought to be space split afterwards to remove any punctuation if message.Command && strings.HasPrefix(lowermessage, "say") { msg := strings.TrimSpace(body[3:]) - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) return true } @@ -73,7 +73,7 @@ func (p *TalkerPlugin) Message(message msg.Message) bool { line = strings.Replace(line, "{nick}", nick, 1) output += line + "\n" } - p.Bot.SendMessage(channel, output) + p.Bot.Send(bot.Message, channel, output) return true } @@ -81,7 +81,7 @@ func (p *TalkerPlugin) Message(message msg.Message) bool { } func (p *TalkerPlugin) Help(channel string, parts []string) { - p.Bot.SendMessage(channel, "Hi, this is talker. I like to talk about FredFelps!") + p.Bot.Send(bot.Message, channel, "Hi, this is talker. I like to talk about FredFelps!") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/tell/tell.go b/plugins/tell/tell.go index 05bab8a..99ea601 100644 --- a/plugins/tell/tell.go +++ b/plugins/tell/tell.go @@ -26,13 +26,13 @@ func (t *TellPlugin) Message(message msg.Message) bool { newMessage := strings.Join(parts[2:], " ") newMessage = fmt.Sprintf("Hey, %s. %s said: %s", target, message.User.Name, newMessage) t.users[target] = append(t.users[target], newMessage) - t.b.SendMessage(message.Channel, fmt.Sprintf("Okay. I'll tell %s.", target)) + t.b.Send(bot.Message, message.Channel, fmt.Sprintf("Okay. I'll tell %s.", target)) return true } uname := strings.ToLower(message.User.Name) if msg, ok := t.users[uname]; ok && len(msg) > 0 { for _, m := range msg { - t.b.SendMessage(message.Channel, string(m)) + t.b.Send(bot.Message, message.Channel, string(m)) } t.users[uname] = []string{} return true diff --git a/plugins/twitch/twitch.go b/plugins/twitch/twitch.go index 6e22f14..b927f14 100644 --- a/plugins/twitch/twitch.go +++ b/plugins/twitch/twitch.go @@ -140,7 +140,7 @@ func (p *TwitchPlugin) LoadData() { func (p *TwitchPlugin) Help(channel string, parts []string) { msg := "There's no help for you here." - p.Bot.SendMessage(channel, msg) + p.Bot.Send(bot.Message, channel, msg) } func (p *TwitchPlugin) twitchLoop(channel string) { @@ -223,18 +223,18 @@ func (p *TwitchPlugin) checkTwitch(channel string, twitcher *Twitcher, alwaysPri } if alwaysPrintStatus { if game == "" { - p.Bot.SendMessage(channel, twitcher.name+" is not streaming.") + p.Bot.Send(bot.Message, channel, twitcher.name+" is not streaming.") } else { - p.Bot.SendMessage(channel, twitcher.name+" is streaming "+game+" at "+twitcher.URL()) + p.Bot.Send(bot.Message, channel, twitcher.name+" is streaming "+game+" at "+twitcher.URL()) } } else if game == "" { if twitcher.game != "" { - p.Bot.SendMessage(channel, twitcher.name+" just stopped streaming.") + p.Bot.Send(bot.Message, channel, twitcher.name+" just stopped streaming.") } twitcher.game = "" } else { if twitcher.game != game { - p.Bot.SendMessage(channel, twitcher.name+" just started streaming "+game+" at "+twitcher.URL()) + p.Bot.Send(bot.Message, channel, twitcher.name+" just started streaming "+game+" at "+twitcher.URL()) } twitcher.game = game } diff --git a/plugins/your/your.go b/plugins/your/your.go index c7c8b45..3f40fea 100644 --- a/plugins/your/your.go +++ b/plugins/your/your.go @@ -43,7 +43,7 @@ func (p *YourPlugin) Message(message msg.Message) bool { } } if msg != message.Body { - p.bot.SendMessage(message.Channel, msg) + p.bot.Send(bot.Message, message.Channel, msg) return true } return false @@ -51,7 +51,7 @@ func (p *YourPlugin) Message(message msg.Message) bool { // Help responds to help requests. Every plugin must implement a help function. func (p *YourPlugin) Help(channel string, parts []string) { - p.bot.SendMessage(channel, "Your corrects people's grammar.") + p.bot.Send(bot.Message, channel, "Your corrects people's grammar.") } // Empty event handler because this plugin does not do anything on event recv diff --git a/plugins/zork/zork.go b/plugins/zork/zork.go index 7e5d0af..3942203 100644 --- a/plugins/zork/zork.go +++ b/plugins/zork/zork.go @@ -26,7 +26,7 @@ type ZorkPlugin struct { zorks map[string]io.WriteCloser } -func New(b bot.Bot) bot.Handler { +func New(b bot.Bot) bot.Plugin { return &ZorkPlugin{ bot: b, zorks: make(map[string]io.WriteCloser), @@ -75,7 +75,7 @@ func (p *ZorkPlugin) runZork(ch string) error { m := strings.Replace(s.Text(), ">", "", -1) m = strings.Replace(m, "\n", "\n>", -1) m = ">" + m + "\n" - p.bot.SendMessage(ch, m) + p.bot.Send(bot.Message, ch, m) } }() go func() { @@ -104,7 +104,7 @@ func (p *ZorkPlugin) Message(message msg.Message) bool { defer p.Unlock() if p.zorks[ch] == nil { if err := p.runZork(ch); err != nil { - p.bot.SendMessage(ch, "failed to run zork: "+err.Error()) + p.bot.Send(bot.Message, ch, "failed to run zork: "+err.Error()) return true } } @@ -118,7 +118,7 @@ func (p *ZorkPlugin) Event(_ string, _ msg.Message) bool { return false } func (p *ZorkPlugin) BotMessage(_ msg.Message) bool { return false } func (p *ZorkPlugin) Help(ch string, _ []string) { - p.bot.SendMessage(ch, "Play zork using 'zork '.") + p.bot.Send(bot.Message, ch, "Play zork using 'zork '.") } func (p *ZorkPlugin) RegisterWeb() *string { return nil }