From 90e7b1130867cb443efec66862d0cef31736e952 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 5 Feb 2019 14:41:38 -0500 Subject: [PATCH] plugins: add callback handler registrations --- plugins/beers/beers.go | 33 ++++++++++----------------- plugins/couldashouldawoulda/csw.go | 22 +++++------------- plugins/counter/counter.go | 36 ++++++++++++------------------ plugins/dice/dice.go | 28 +++++++++-------------- plugins/emojifyme/emojifyme.go | 24 +++++--------------- plugins/fact/factoid.go | 24 +++++++------------- plugins/fact/remember.go | 24 ++++++-------------- plugins/first/first.go | 24 +++++++------------- plugins/inventory/inventory.go | 33 +++++++++------------------ plugins/leftpad/leftpad.go | 26 ++++++--------------- plugins/nerdepedia/nerdepedia.go | 30 +++++++++---------------- plugins/picker/picker.go | 26 +++++++++------------ plugins/reaction/reaction.go | 26 ++++++--------------- plugins/reminder/reminder.go | 30 ++++++++++--------------- plugins/rpgORdie/rpgORdie.go | 28 +++++++++-------------- plugins/rss/rss.go | 36 ++++++++++-------------------- plugins/sisyphus/sisyphus.go | 24 +++++++++----------- plugins/talker/talker.go | 28 +++++++++-------------- plugins/tell/tell.go | 12 +++++----- plugins/twitch/twitch.go | 28 +++++++---------------- plugins/your/your.go | 30 +++++++++---------------- plugins/zork/zork.go | 18 +++++++-------- 22 files changed, 205 insertions(+), 385 deletions(-) diff --git a/plugins/beers/beers.go b/plugins/beers/beers.go index d81cfa0..7736936 100644 --- a/plugins/beers/beers.go +++ b/plugins/beers/beers.go @@ -38,8 +38,8 @@ type untappdUser struct { } // New BeersPlugin creates a new BeersPlugin with the Plugin interface -func New(bot bot.Bot) *BeersPlugin { - if _, err := bot.DB().Exec(`create table if not exists untappd ( +func New(b bot.Bot) *BeersPlugin { + if _, err := b.DB().Exec(`create table if not exists untappd ( id integer primary key, untappdUser string, channel string, @@ -49,19 +49,21 @@ func New(bot bot.Bot) *BeersPlugin { log.Fatal(err) } p := BeersPlugin{ - Bot: bot, - db: bot.DB(), + Bot: b, + db: b.DB(), } - for _, channel := range bot.Config().GetArray("Untappd.Channels", []string{}) { + for _, channel := range b.Config().GetArray("Untappd.Channels", []string{}) { go p.untappdLoop(channel) } + b.Register(p, bot.Message, p.message) + b.Register(p, bot.Help, p.help) return &p } // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. -func (p *BeersPlugin) Message(message msg.Message) bool { +func (p *BeersPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { parts := strings.Fields(message.Body) if len(parts) == 0 { @@ -190,17 +192,13 @@ func (p *BeersPlugin) Message(message msg.Message) bool { return false } -// Empty event handler because this plugin does not do anything on event recv -func (p *BeersPlugin) Event(kind string, message msg.Message) bool { - return false -} - // Help responds to help requests. Every plugin must implement a help function. -func (p *BeersPlugin) Help(channel string, parts []string) { +func (p *BeersPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { 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.Send(bot.Message, channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) + return true } func getUserBeers(db *sqlx.DB, user string) counter.Item { @@ -439,14 +437,7 @@ func (p *BeersPlugin) untappdLoop(channel string) { } } -// Handler for bot's own messages -func (p *BeersPlugin) BotMessage(message msg.Message) bool { - return false -} - // Register any web URLs desired -func (p *BeersPlugin) RegisterWeb() *string { +func (p BeersPlugin) RegisterWeb() *string { return nil } - -func (p *BeersPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/couldashouldawoulda/csw.go b/plugins/couldashouldawoulda/csw.go index 1a99317..9d2d7a3 100644 --- a/plugins/couldashouldawoulda/csw.go +++ b/plugins/couldashouldawoulda/csw.go @@ -17,13 +17,15 @@ type CSWPlugin struct { Config *config.Config } -func New(bot bot.Bot) *CSWPlugin { - return &CSWPlugin{ - Bot: bot, +func New(b bot.Bot) *CSWPlugin { + csw := &CSWPlugin{ + Bot: b, } + b.Register(csw, bot.Message, csw.message) + return csw } -func (p *CSWPlugin) Message(message msg.Message) bool { +func (p *CSWPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if !message.Command { return false } @@ -70,18 +72,6 @@ func (p *CSWPlugin) Message(message msg.Message) bool { return false } -func (p *CSWPlugin) Help(channel string, parts []string) {} - -func (p *CSWPlugin) Event(kind string, message msg.Message) bool { - return false -} - -func (p *CSWPlugin) BotMessage(message msg.Message) bool { - return false -} - -func (p *CSWPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } - func (p *CSWPlugin) RegisterWeb() *string { return nil } diff --git a/plugins/counter/counter.go b/plugins/counter/counter.go index 0100d79..6d0e0ed 100644 --- a/plugins/counter/counter.go +++ b/plugins/counter/counter.go @@ -172,31 +172,34 @@ func (i *Item) Delete() error { } // NewCounterPlugin creates a new CounterPlugin with the Plugin interface -func New(bot bot.Bot) *CounterPlugin { - tx := bot.DB().MustBegin() - bot.DB().MustExec(`create table if not exists counter ( +func New(b bot.Bot) *CounterPlugin { + tx := b.DB().MustBegin() + b.DB().MustExec(`create table if not exists counter ( id integer primary key, nick string, item string, count integer );`) - bot.DB().MustExec(`create table if not exists counter_alias ( + b.DB().MustExec(`create table if not exists counter_alias ( id integer PRIMARY KEY AUTOINCREMENT, item string NOT NULL UNIQUE, points_to string NOT NULL );`) tx.Commit() - return &CounterPlugin{ - Bot: bot, - DB: bot.DB(), + cp := &CounterPlugin{ + Bot: b, + DB: b.DB(), } + b.Register(cp, bot.Message, cp.message) + b.Register(cp, bot.Help, cp.help) + return cp } // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the // users message. Otherwise, the function returns false and the bot continues // execution of other plugins. -func (p *CounterPlugin) Message(message msg.Message) bool { +func (p *CounterPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { // This bot does not reply to anything nick := message.User.Name channel := message.Channel @@ -444,21 +447,12 @@ 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.Send(bot.Message, channel, "You can set counters incrementally by using "+ +func (p *CounterPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, 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\".") -} - -// Empty event handler because this plugin does not do anything on event recv -func (p *CounterPlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Handler for bot's own messages -func (p *CounterPlugin) BotMessage(message msg.Message) bool { - return false + return true } // Register any web URLs desired @@ -466,8 +460,6 @@ func (p *CounterPlugin) RegisterWeb() *string { return nil } -func (p *CounterPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } - func (p *CounterPlugin) checkMatch(message msg.Message) bool { nick := message.User.Name channel := message.Channel diff --git a/plugins/dice/dice.go b/plugins/dice/dice.go index 51bbd72..721a652 100644 --- a/plugins/dice/dice.go +++ b/plugins/dice/dice.go @@ -19,10 +19,13 @@ type DicePlugin struct { } // NewDicePlugin creates a new DicePlugin with the Plugin interface -func New(bot bot.Bot) *DicePlugin { - return &DicePlugin{ - Bot: bot, +func New(b bot.Bot) *DicePlugin { + dp := &DicePlugin{ + Bot: b, } + b.Register(dp, bot.Message, dp.message) + b.Register(dp, bot.Help, dp.help) + return dp } func rollDie(sides int) int { @@ -32,7 +35,7 @@ func rollDie(sides int) int { // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. -func (p *DicePlugin) Message(message msg.Message) bool { +func (p *DicePlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if !message.Command { return false } @@ -67,23 +70,12 @@ func (p *DicePlugin) Message(message msg.Message) bool { } // Help responds to help requests. Every plugin must implement a help function. -func (p *DicePlugin) Help(channel string, parts []string) { - 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 -func (p *DicePlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Handler for bot's own messages -func (p *DicePlugin) BotMessage(message msg.Message) bool { - return false +func (p *DicePlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, message.Channel, "Roll dice using notation XdY. Try \"3d20\".") + return true } // Register any web URLs desired func (p *DicePlugin) RegisterWeb() *string { return nil } - -func (p *DicePlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/emojifyme/emojifyme.go b/plugins/emojifyme/emojifyme.go index b3614fd..76610d8 100644 --- a/plugins/emojifyme/emojifyme.go +++ b/plugins/emojifyme/emojifyme.go @@ -20,7 +20,7 @@ type EmojifyMePlugin struct { Emoji map[string]string } -func New(bot bot.Bot) *EmojifyMePlugin { +func New(b bot.Bot) *EmojifyMePlugin { resp, err := http.Get("https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json") if err != nil { log.Fatalf("Error generic emoji list: %s", err) @@ -48,14 +48,16 @@ func New(bot bot.Bot) *EmojifyMePlugin { } } - return &EmojifyMePlugin{ - Bot: bot, + ep := &EmojifyMePlugin{ + Bot: b, GotBotEmoji: false, Emoji: emojiMap, } + b.Register(ep, bot.Message, ep.message) + return ep } -func (p *EmojifyMePlugin) Message(message msg.Message) bool { +func (p *EmojifyMePlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if !p.GotBotEmoji { p.GotBotEmoji = true emojiMap := p.Bot.GetEmojiList() @@ -97,24 +99,10 @@ func (p *EmojifyMePlugin) Message(message msg.Message) bool { return false } -func (p *EmojifyMePlugin) Help(channel string, parts []string) { - -} - -func (p *EmojifyMePlugin) Event(kind string, message msg.Message) bool { - return false -} - -func (p *EmojifyMePlugin) BotMessage(message msg.Message) bool { - return false -} - func (p *EmojifyMePlugin) RegisterWeb() *string { return nil } -func (p *EmojifyMePlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } - func stringsContain(haystack []string, needle string) bool { for _, s := range haystack { if s == needle { diff --git a/plugins/fact/factoid.go b/plugins/fact/factoid.go index fc2dfad..b8b40c2 100644 --- a/plugins/fact/factoid.go +++ b/plugins/fact/factoid.go @@ -314,6 +314,9 @@ func New(botInst bot.Bot) *Factoid { }(channel) } + botInst.Register(p, bot.Message, p.message) + botInst.Register(p, bot.Help, p.help) + return p } @@ -609,7 +612,7 @@ func (p *Factoid) changeFact(message msg.Message) bool { // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. -func (p *Factoid) Message(message msg.Message) bool { +func (p *Factoid) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if strings.ToLower(message.Body) == "what was that?" { return p.tellThemWhatThatWas(message) } @@ -669,14 +672,10 @@ func (p *Factoid) Message(message msg.Message) bool { } // Help responds to help requests. Every plugin must implement a help function. -func (p *Factoid) Help(channel string, parts []string) { - 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 -func (p *Factoid) Event(kind string, message msg.Message) bool { - return false +func (p *Factoid) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, 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, message.Channel, "I can also figure out some variables including: $nonzero, $digit, $nick, and $someone.") + return true } // Pull a fact at random from the database @@ -737,11 +736,6 @@ func (p *Factoid) factTimer(channel string) { } } -// Handler for bot's own messages -func (p *Factoid) BotMessage(message msg.Message) bool { - return false -} - // Register any web URLs desired func (p *Factoid) RegisterWeb() *string { http.HandleFunc("/factoid/req", p.serveQuery) @@ -784,5 +778,3 @@ func (p *Factoid) serveQuery(w http.ResponseWriter, r *http.Request) { log.Println(err) } } - -func (p *Factoid) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/fact/remember.go b/plugins/fact/remember.go index 09ecf7b..87485f1 100644 --- a/plugins/fact/remember.go +++ b/plugins/fact/remember.go @@ -29,6 +29,8 @@ func NewRemember(b bot.Bot) *RememberPlugin { Log: make(map[string][]msg.Message), db: b.DB(), } + b.Register(p, bot.Message, p.message) + b.Register(p, bot.Message, p.help) return &p } @@ -36,7 +38,7 @@ func NewRemember(b bot.Bot) *RememberPlugin { // This function returns true if the plugin responds in a meaningful way to the // users message. Otherwise, the function returns false and the bot continues // execution of other plugins. -func (p *RememberPlugin) Message(message msg.Message) bool { +func (p *RememberPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if strings.ToLower(message.Body) == "quote" && message.Command { q := p.randQuote() @@ -111,14 +113,15 @@ func (p *RememberPlugin) Message(message msg.Message) bool { } // Help responds to help requests. Every plugin must implement a help function. -func (p *RememberPlugin) Help(channel string, parts []string) { +func (p *RememberPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { msg := "!remember will let you quote your idiot friends. Just type " + "!remember to remember what they said. Snippet can " + "be any part of their message. Later on, you can ask for a random " + "!quote." - p.Bot.Send(bot.Message, channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) + return true } // deliver a random quote out of the db. @@ -150,19 +153,8 @@ func (p *RememberPlugin) randQuote() string { return f.Tidbit } -// Empty event handler because this plugin does not do anything on event recv -func (p *RememberPlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Record what the bot says in the log -func (p *RememberPlugin) BotMessage(message msg.Message) bool { - p.recordMsg(message) - return false -} - // Register any web URLs desired -func (p *RememberPlugin) RegisterWeb() *string { +func (p RememberPlugin) RegisterWeb() *string { return nil } @@ -170,5 +162,3 @@ func (p *RememberPlugin) recordMsg(message msg.Message) { log.Printf("Logging message: %s: %s", message.User.Name, message.Body) p.Log[message.Channel] = append(p.Log[message.Channel], message) } - -func (p *RememberPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/first/first.go b/plugins/first/first.go index af3ffcc..9771dbf 100644 --- a/plugins/first/first.go +++ b/plugins/first/first.go @@ -66,11 +66,14 @@ func New(b bot.Bot) *FirstPlugin { log.Fatal("Could not initialize first plugin: ", err) } - return &FirstPlugin{ + fp := &FirstPlugin{ Bot: b, db: b.DB(), First: first, } + b.Register(fp, bot.Message, fp.message) + b.Register(fp, bot.Help, fp.help) + return fp } func getLastFirst(db *sqlx.DB) (*FirstEntry, error) { @@ -123,7 +126,7 @@ func isToday(t time.Time) bool { // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. -func (p *FirstPlugin) Message(message msg.Message) bool { +func (p *FirstPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { // This bot does not reply to anything if p.First == nil && p.allowed(message) { @@ -208,23 +211,12 @@ 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.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 -func (p *FirstPlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Handler for bot's own messages -func (p *FirstPlugin) BotMessage(message msg.Message) bool { - return false +func (p *FirstPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, message.Channel, "Sorry, First does not do a goddamn thing.") + return true } // Register any web URLs desired func (p *FirstPlugin) RegisterWeb() *string { return nil } - -func (p *FirstPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/inventory/inventory.go b/plugins/inventory/inventory.go index 072a22c..50fb6f3 100644 --- a/plugins/inventory/inventory.go +++ b/plugins/inventory/inventory.go @@ -24,8 +24,8 @@ type InventoryPlugin struct { } // New creates a new InventoryPlugin with the Plugin interface -func New(bot bot.Bot) *InventoryPlugin { - config := bot.Config() +func New(b bot.Bot) *InventoryPlugin { + config := b.Config() nick := config.Get("nick", "bot") r1, err := regexp.Compile("take this (.+)") checkerr(err) @@ -38,15 +38,15 @@ func New(bot bot.Bot) *InventoryPlugin { r5, err := regexp.Compile(fmt.Sprintf("gives (.+) to %s([^a-zA-Z].*)?", nick)) checkerr(err) - p := InventoryPlugin{ - DB: bot.DB(), - bot: bot, + p := &InventoryPlugin{ + DB: b.DB(), + bot: b, config: config, r1: r1, r2: r2, r3: r3, r4: r4, r5: r5, } - bot.RegisterFilter("$item", p.itemFilter) - bot.RegisterFilter("$giveitem", p.giveItemFilter) + b.RegisterFilter("$item", p.itemFilter) + b.RegisterFilter("$giveitem", p.giveItemFilter) _, err = p.DB.Exec(`create table if not exists inventory ( item string primary key @@ -56,7 +56,9 @@ func New(bot bot.Bot) *InventoryPlugin { log.Fatal(err) } - return &p + b.Register(p, bot.Message, p.message) + + return p } func (p *InventoryPlugin) giveItemFilter(input string) string { @@ -75,7 +77,7 @@ func (p *InventoryPlugin) itemFilter(input string) string { return input } -func (p *InventoryPlugin) Message(message msg.Message) bool { +func (p *InventoryPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { m := message.Body log.Printf("inventory trying to read %+v", message) if message.Command { @@ -223,20 +225,7 @@ func checkerr(e error) { } } -func (p *InventoryPlugin) Event(e string, message msg.Message) bool { - return false -} - -func (p *InventoryPlugin) BotMessage(message msg.Message) bool { - return false -} - -func (p *InventoryPlugin) Help(e string, m []string) { -} - func (p *InventoryPlugin) RegisterWeb() *string { // nothing to register return nil } - -func (p *InventoryPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/leftpad/leftpad.go b/plugins/leftpad/leftpad.go index 42a4493..e005b46 100644 --- a/plugins/leftpad/leftpad.go +++ b/plugins/leftpad/leftpad.go @@ -20,19 +20,20 @@ type LeftpadPlugin struct { } // New creates a new LeftpadPlugin with the Plugin interface -func New(bot bot.Bot) *LeftpadPlugin { - p := LeftpadPlugin{ - bot: bot, - config: bot.Config(), +func New(b bot.Bot) *LeftpadPlugin { + p := &LeftpadPlugin{ + bot: b, + config: b.Config(), } - return &p + b.Register(p, bot.Message, p.message) + return p } type leftpadResp struct { Str string } -func (p *LeftpadPlugin) Message(message msg.Message) bool { +func (p *LeftpadPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if !message.Command { return false } @@ -62,20 +63,7 @@ func (p *LeftpadPlugin) Message(message msg.Message) bool { return false } -func (p *LeftpadPlugin) Event(e string, message msg.Message) bool { - return false -} - -func (p *LeftpadPlugin) BotMessage(message msg.Message) bool { - return false -} - -func (p *LeftpadPlugin) Help(e string, m []string) { -} - func (p *LeftpadPlugin) RegisterWeb() *string { // nothing to register return nil } - -func (p *LeftpadPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/nerdepedia/nerdepedia.go b/plugins/nerdepedia/nerdepedia.go index f0e0096..eb040a7 100644 --- a/plugins/nerdepedia/nerdepedia.go +++ b/plugins/nerdepedia/nerdepedia.go @@ -27,17 +27,20 @@ type NerdepediaPlugin struct { } // NewNerdepediaPlugin creates a new NerdepediaPlugin with the Plugin interface -func New(bot bot.Bot) *NerdepediaPlugin { - return &NerdepediaPlugin{ - bot: bot, - config: bot.Config(), +func New(b bot.Bot) *NerdepediaPlugin { + np := &NerdepediaPlugin{ + bot: b, + config: b.Config(), } + b.Register(np, bot.Message, np.message) + b.Register(np, bot.Help, np.help) + return np } // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. -func (p *NerdepediaPlugin) Message(message msg.Message) bool { +func (p *NerdepediaPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { lowerCase := strings.ToLower(message.Body) query := "" if lowerCase == "may the force be with you" || lowerCase == "help me obi-wan" { @@ -87,23 +90,12 @@ 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.Send(bot.Message, channel, "nerd stuff") -} - -// Empty event handler because this plugin does not do anything on event recv -func (p *NerdepediaPlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Handler for bot's own messages -func (p *NerdepediaPlugin) BotMessage(message msg.Message) bool { - return false +func (p *NerdepediaPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.bot.Send(bot.Message, message.Channel, "nerd stuff") + return true } // Register any web URLs desired func (p *NerdepediaPlugin) RegisterWeb() *string { return nil } - -func (p *NerdepediaPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/picker/picker.go b/plugins/picker/picker.go index 290a490..3a1c320 100644 --- a/plugins/picker/picker.go +++ b/plugins/picker/picker.go @@ -20,16 +20,19 @@ type PickerPlugin struct { } // NewPickerPlugin creates a new PickerPlugin with the Plugin interface -func New(bot bot.Bot) *PickerPlugin { - return &PickerPlugin{ - Bot: bot, +func New(b bot.Bot) *PickerPlugin { + pp := &PickerPlugin{ + Bot: b, } + b.Register(pp, bot.Message, pp.message) + b.Register(pp, bot.Help, pp.help) + return pp } // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. -func (p *PickerPlugin) Message(message msg.Message) bool { +func (p *PickerPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if !strings.HasPrefix(message.Body, "pick") { return false } @@ -108,18 +111,9 @@ 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.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 -func (p *PickerPlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Handler for bot's own messages -func (p *PickerPlugin) BotMessage(message msg.Message) bool { - return false +func (p *PickerPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, message.Channel, "Choose from a list of options. Try \"pick {a,b,c}\".") + return true } // Register any web URLs desired diff --git a/plugins/reaction/reaction.go b/plugins/reaction/reaction.go index 6c13201..c964022 100644 --- a/plugins/reaction/reaction.go +++ b/plugins/reaction/reaction.go @@ -15,14 +15,16 @@ type ReactionPlugin struct { Config *config.Config } -func New(bot bot.Bot) *ReactionPlugin { - return &ReactionPlugin{ - Bot: bot, - Config: bot.Config(), +func New(b bot.Bot) *ReactionPlugin { + rp := &ReactionPlugin{ + Bot: b, + Config: b.Config(), } + b.Register(rp, bot.Message, rp.message) + return rp } -func (p *ReactionPlugin) Message(message msg.Message) bool { +func (p *ReactionPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { harrass := false for _, nick := range p.Config.GetArray("Reaction.HarrassList", []string{}) { if message.User.Name == nick { @@ -62,20 +64,6 @@ func (p *ReactionPlugin) Message(message msg.Message) bool { return false } -func (p *ReactionPlugin) Help(channel string, parts []string) { - -} - -func (p *ReactionPlugin) Event(kind string, message msg.Message) bool { - return false -} - -func (p *ReactionPlugin) BotMessage(message msg.Message) bool { - return false -} - func (p *ReactionPlugin) RegisterWeb() *string { return nil } - -func (p *ReactionPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/reminder/reminder.go b/plugins/reminder/reminder.go index df38f04..93d4dec 100644 --- a/plugins/reminder/reminder.go +++ b/plugins/reminder/reminder.go @@ -38,9 +38,9 @@ type Reminder struct { channel string } -func New(bot bot.Bot) *ReminderPlugin { +func New(b bot.Bot) *ReminderPlugin { log.SetFlags(log.LstdFlags | log.Lshortfile) - if _, err := bot.DB().Exec(`create table if not exists reminders ( + if _, err := b.DB().Exec(`create table if not exists reminders ( id integer primary key, fromWho string, toWho string, @@ -56,21 +56,24 @@ func New(bot bot.Bot) *ReminderPlugin { timer.Stop() plugin := &ReminderPlugin{ - Bot: bot, - db: bot.DB(), + Bot: b, + db: b.DB(), mutex: &sync.Mutex{}, timer: timer, - config: bot.Config(), + config: b.Config(), } plugin.queueUpNextReminder() go reminderer(plugin) + b.Register(plugin, bot.Message, plugin.message) + b.Register(plugin, bot.Help, plugin.help) + return plugin } -func (p *ReminderPlugin) Message(message msg.Message) bool { +func (p *ReminderPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { channel := message.Channel from := message.User.Name @@ -192,16 +195,9 @@ func (p *ReminderPlugin) Message(message msg.Message) bool { return false } -func (p *ReminderPlugin) Help(channel string, parts []string) { - 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 { - return false -} - -func (p *ReminderPlugin) BotMessage(message msg.Message) bool { - return false +func (p *ReminderPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, message.Channel, "Pester someone with a reminder. Try \"remind in message\".\n\nUnsure about duration syntax? Check https://golang.org/pkg/time/#ParseDuration") + return true } func (p *ReminderPlugin) RegisterWeb() *string { @@ -365,5 +361,3 @@ func reminderer(p *ReminderPlugin) { p.queueUpNextReminder() } } - -func (p *ReminderPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/rpgORdie/rpgORdie.go b/plugins/rpgORdie/rpgORdie.go index 1763e73..8b5a40e 100644 --- a/plugins/rpgORdie/rpgORdie.go +++ b/plugins/rpgORdie/rpgORdie.go @@ -98,13 +98,17 @@ func (b *board) checkAndMove(dx, dy int) int { } func New(b bot.Bot) *RPGPlugin { - return &RPGPlugin{ + rpg := &RPGPlugin{ Bot: b, listenFor: map[string]*board{}, } + b.Register(rpg, bot.Message, rpg.message) + b.Register(rpg, bot.Reply, rpg.replyMessage) + b.Register(rpg, bot.Help, rpg.help) + return rpg } -func (p *RPGPlugin) Message(message msg.Message) bool { +func (p *RPGPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if strings.ToLower(message.Body) == "start rpg" { b := NewRandomBoard() ts, _ := p.Bot.Send(bot.Message, message.Channel, b.toMessageString()) @@ -115,27 +119,17 @@ func (p *RPGPlugin) Message(message msg.Message) bool { return false } -func (p *RPGPlugin) LoadData() { - -} - -func (p *RPGPlugin) Help(channel string, parts []string) { - p.Bot.Send(bot.Message, channel, "Go find a walkthrough or something.") -} - -func (p *RPGPlugin) Event(kind string, message msg.Message) bool { - return false -} - -func (p *RPGPlugin) BotMessage(message msg.Message) bool { - return false +func (p *RPGPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, message.Channel, "Go find a walkthrough or something.") + return true } func (p *RPGPlugin) RegisterWeb() *string { return nil } -func (p *RPGPlugin) ReplyMessage(message msg.Message, identifier string) bool { +func (p *RPGPlugin) replyMessage(kind bot.Kind, message msg.Message, args ...interface{}) bool { + identifier := args[0].(string) if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Get("Nick", "bot")) { if b, ok := p.listenFor[identifier]; ok { diff --git a/plugins/rss/rss.go b/plugins/rss/rss.go index c7bed49..b25d749 100644 --- a/plugins/rss/rss.go +++ b/plugins/rss/rss.go @@ -49,16 +49,19 @@ func (c *cacheItem) getCurrentPage(maxLines int) string { return page } -func New(bot bot.Bot) *RSSPlugin { - return &RSSPlugin{ - Bot: bot, +func New(b bot.Bot) *RSSPlugin { + rss := &RSSPlugin{ + Bot: b, cache: map[string]*cacheItem{}, - shelfLife: time.Minute * 20, - maxLines: 5, + shelfLife: time.Minute * time.Duration(b.Config().GetInt("rss.shelfLife", 20)), + maxLines: b.Config().GetInt("rss.maxLines", 5), } + b.Register(rss, bot.Message, rss.message) + b.Register(rss, bot.Help, rss.help) + return rss } -func (p *RSSPlugin) Message(message msg.Message) bool { +func (p *RSSPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { tokens := strings.Fields(message.Body) numTokens := len(tokens) @@ -94,28 +97,13 @@ func (p *RSSPlugin) Message(message msg.Message) bool { return false } -func (p *RSSPlugin) LoadData() { - // This bot has no data to load -} - // Help responds to help requests. Every plugin must implement a help function. -func (p *RSSPlugin) Help(channel string, parts []string) { - 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 -func (p *RSSPlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Handler for bot's own messages -func (p *RSSPlugin) BotMessage(message msg.Message) bool { - return false +func (p *RSSPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, message.Channel, "try '!rss http://rss.cnn.com/rss/edition.rss'") + return true } // Register any web URLs desired func (p *RSSPlugin) RegisterWeb() *string { return nil } - -func (p *RSSPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/sisyphus/sisyphus.go b/plugins/sisyphus/sisyphus.go index 0eac02d..5a0a8b8 100644 --- a/plugins/sisyphus/sisyphus.go +++ b/plugins/sisyphus/sisyphus.go @@ -162,13 +162,17 @@ func (g *game) toMessageString() string { } func New(b bot.Bot) *SisyphusPlugin { - return &SisyphusPlugin{ + sp := &SisyphusPlugin{ Bot: b, listenFor: map[string]*game{}, } + b.Register(sp, bot.Message, sp.message) + b.Register(sp, bot.Reply, sp.replyMessage) + b.Register(sp, bot.Help, sp.help) + return sp } -func (p *SisyphusPlugin) Message(message msg.Message) bool { +func (p *SisyphusPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if strings.ToLower(message.Body) == "start sisyphus" { b := NewRandomGame(p.Bot, message.Channel, message.User.Name) p.listenFor[b.id] = b @@ -178,23 +182,17 @@ func (p *SisyphusPlugin) Message(message msg.Message) bool { return false } -func (p *SisyphusPlugin) Help(channel string, parts []string) { - p.Bot.Send(bot.Message, channel, "https://en.wikipedia.org/wiki/Sisyphus") -} - -func (p *SisyphusPlugin) Event(kind string, message msg.Message) bool { - return false -} - -func (p *SisyphusPlugin) BotMessage(message msg.Message) bool { - return false +func (p *SisyphusPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, message.Channel, "https://en.wikipedia.org/wiki/Sisyphus") + return true } func (p *SisyphusPlugin) RegisterWeb() *string { return nil } -func (p *SisyphusPlugin) ReplyMessage(message msg.Message, identifier string) bool { +func (p *SisyphusPlugin) replyMessage(kind bot.Kind, message msg.Message, args ...interface{}) bool { + identifier := args[0].(string) if strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config().Get("Nick", "bot")) { if g, ok := p.listenFor[identifier]; ok { diff --git a/plugins/talker/talker.go b/plugins/talker/talker.go index f4abdd7..46ba61a 100644 --- a/plugins/talker/talker.go +++ b/plugins/talker/talker.go @@ -43,13 +43,16 @@ type TalkerPlugin struct { sayings []string } -func New(bot bot.Bot) *TalkerPlugin { - return &TalkerPlugin{ - Bot: bot, +func New(b bot.Bot) *TalkerPlugin { + tp := &TalkerPlugin{ + Bot: b, } + b.Register(tp, bot.Message, tp.message) + b.Register(tp, bot.Help, tp.help) + return tp } -func (p *TalkerPlugin) Message(message msg.Message) bool { +func (p *TalkerPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { channel := message.Channel body := message.Body lowermessage := strings.ToLower(body) @@ -80,23 +83,12 @@ func (p *TalkerPlugin) Message(message msg.Message) bool { return false } -func (p *TalkerPlugin) Help(channel string, parts []string) { - 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 -func (p *TalkerPlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Handler for bot's own messages -func (p *TalkerPlugin) BotMessage(message msg.Message) bool { - return false +func (p *TalkerPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.Bot.Send(bot.Message, message.Channel, "Hi, this is talker. I like to talk about FredFelps!") + return true } // Register any web URLs desired func (p *TalkerPlugin) RegisterWeb() *string { return nil } - -func (p *TalkerPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/tell/tell.go b/plugins/tell/tell.go index 99ea601..c852141 100644 --- a/plugins/tell/tell.go +++ b/plugins/tell/tell.go @@ -16,10 +16,12 @@ type TellPlugin struct { } func New(b bot.Bot) *TellPlugin { - return &TellPlugin{b, make(map[string][]string)} + tp := &TellPlugin{b, make(map[string][]string)} + b.Register(tp, bot.Message, tp.message) + return tp } -func (t *TellPlugin) Message(message msg.Message) bool { +func (t *TellPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if strings.HasPrefix(strings.ToLower(message.Body), "tell") { parts := strings.Split(message.Body, " ") target := strings.ToLower(parts[1]) @@ -40,8 +42,4 @@ func (t *TellPlugin) Message(message msg.Message) bool { return false } -func (t *TellPlugin) Event(kind string, message msg.Message) bool { return false } -func (t *TellPlugin) ReplyMessage(msg.Message, string) bool { return false } -func (t *TellPlugin) BotMessage(message msg.Message) bool { return false } -func (t *TellPlugin) Help(channel string, parts []string) {} -func (t *TellPlugin) RegisterWeb() *string { return nil } +func (t *TellPlugin) RegisterWeb() *string { return nil } diff --git a/plugins/twitch/twitch.go b/plugins/twitch/twitch.go index d712292..0c17a24 100644 --- a/plugins/twitch/twitch.go +++ b/plugins/twitch/twitch.go @@ -51,10 +51,10 @@ type stream struct { } `json:"pagination"` } -func New(bot bot.Bot) *TwitchPlugin { +func New(b bot.Bot) *TwitchPlugin { p := &TwitchPlugin{ - Bot: bot, - config: bot.Config(), + Bot: b, + config: b.Config(), twitchList: map[string]*Twitcher{}, } @@ -70,13 +70,10 @@ func New(bot bot.Bot) *TwitchPlugin { go p.twitchLoop(ch) } + b.Register(p, bot.Message, p.message) return p } -func (p *TwitchPlugin) BotMessage(message msg.Message) bool { - return false -} - func (p *TwitchPlugin) RegisterWeb() *string { http.HandleFunc("/isstreaming/", p.serveStreaming) tmp := "/isstreaming" @@ -114,7 +111,7 @@ func (p *TwitchPlugin) serveStreaming(w http.ResponseWriter, r *http.Request) { } } -func (p *TwitchPlugin) Message(message msg.Message) bool { +func (p *TwitchPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { if strings.ToLower(message.Body) == "twitch status" { channel := message.Channel if users := p.config.GetArray("Twitch."+channel+".Users", []string{}); len(users) > 0 { @@ -130,17 +127,10 @@ func (p *TwitchPlugin) Message(message msg.Message) bool { return false } -func (p *TwitchPlugin) Event(kind string, message msg.Message) bool { - return false -} - -func (p *TwitchPlugin) LoadData() { - -} - -func (p *TwitchPlugin) Help(channel string, parts []string) { +func (p *TwitchPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { msg := "There's no help for you here." - p.Bot.Send(bot.Message, channel, msg) + p.Bot.Send(bot.Message, message.Channel, msg) + return true } func (p *TwitchPlugin) twitchLoop(channel string) { @@ -240,5 +230,3 @@ func (p *TwitchPlugin) checkTwitch(channel string, twitcher *Twitcher, alwaysPri twitcher.game = game } } - -func (p *TwitchPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/your/your.go b/plugins/your/your.go index 3f40fea..a319039 100644 --- a/plugins/your/your.go +++ b/plugins/your/your.go @@ -17,17 +17,20 @@ type YourPlugin struct { } // NewYourPlugin creates a new YourPlugin with the Plugin interface -func New(bot bot.Bot) *YourPlugin { - return &YourPlugin{ - bot: bot, - config: bot.Config(), +func New(b bot.Bot) *YourPlugin { + yp := &YourPlugin{ + bot: b, + config: b.Config(), } + b.Register(yp, bot.Message, yp.message) + b.Register(yp, bot.Help, yp.help) + return yp } // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. -func (p *YourPlugin) Message(message msg.Message) bool { +func (p *YourPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { maxLen := p.config.GetInt("your.maxlength", 140) if len(message.Body) > maxLen { return false @@ -50,23 +53,12 @@ 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.Send(bot.Message, channel, "Your corrects people's grammar.") -} - -// Empty event handler because this plugin does not do anything on event recv -func (p *YourPlugin) Event(kind string, message msg.Message) bool { - return false -} - -// Handler for bot's own messages -func (p *YourPlugin) BotMessage(message msg.Message) bool { - return false +func (p *YourPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.bot.Send(bot.Message, message.Channel, "Your corrects people's grammar.") + return true } // Register any web URLs desired func (p *YourPlugin) RegisterWeb() *string { return nil } - -func (p *YourPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false } diff --git a/plugins/zork/zork.go b/plugins/zork/zork.go index 3942203..b5ac682 100644 --- a/plugins/zork/zork.go +++ b/plugins/zork/zork.go @@ -27,10 +27,13 @@ type ZorkPlugin struct { } func New(b bot.Bot) bot.Plugin { - return &ZorkPlugin{ + z := &ZorkPlugin{ bot: b, zorks: make(map[string]io.WriteCloser), } + b.Register(z, bot.Message, z.message) + b.Register(z, bot.Help, z.help) + return z } func (p *ZorkPlugin) runZork(ch string) error { @@ -91,7 +94,7 @@ func (p *ZorkPlugin) runZork(ch string) error { return nil } -func (p *ZorkPlugin) Message(message msg.Message) bool { +func (p *ZorkPlugin) message(kind bot.Kind, message msg.Message, args ...interface{}) bool { m := strings.ToLower(message.Body) log.Printf("got message [%s]\n", m) if ts := strings.Fields(m); len(ts) < 1 || ts[0] != "zork" { @@ -113,14 +116,9 @@ func (p *ZorkPlugin) Message(message msg.Message) bool { return true } -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.Send(bot.Message, ch, "Play zork using 'zork '.") +func (p *ZorkPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool { + p.bot.Send(bot.Message, message.Channel, "Play zork using 'zork '.") + return true } func (p *ZorkPlugin) RegisterWeb() *string { return nil } - -func (p *ZorkPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }