plugins: add callback handler registrations

This commit is contained in:
Chris Sexton 2019-02-05 14:41:38 -05:00
parent 933e514ddd
commit 90e7b11308
22 changed files with 205 additions and 385 deletions
plugins
beers
couldashouldawoulda
counter
dice
emojifyme
fact
first
inventory
leftpad
nerdepedia
picker
reaction
reminder
rpgORdie
rss
sisyphus
talker
tell
twitch
your
zork

View File

@ -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 }

View File

@ -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
}

View File

@ -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 "+
"<noun>++ and <noun>--. 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

View File

@ -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 }

View File

@ -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 {

View File

@ -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 <has> $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 <has> $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 }

View File

@ -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 <nick> <snippet> 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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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

View File

@ -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 }

View File

@ -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 <user> in <duration> 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 <user> in <duration> 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 }

View File

@ -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 {

View File

@ -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 }

View File

@ -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 {

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 }

View File

@ -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 <zork command>'.")
func (p *ZorkPlugin) help(kind bot.Kind, message msg.Message, args ...interface{}) bool {
p.bot.Send(bot.Message, message.Channel, "Play zork using 'zork <zork command>'.")
return true
}
func (p *ZorkPlugin) RegisterWeb() *string { return nil }
func (p *ZorkPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }