From 132fdd29bef583840a2d10e63b2ebcd52cfe3865 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Tue, 19 Dec 2017 13:37:47 -0500 Subject: [PATCH] picker: add new plugin --- main.go | 2 ++ plugins/picker/picker.go | 78 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 plugins/picker/picker.go diff --git a/main.go b/main.go index 027a60d..4ab7e1b 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "github.com/velour/catbase/plugins/first" "github.com/velour/catbase/plugins/inventory" "github.com/velour/catbase/plugins/leftpad" + "github.com/velour/catbase/plugins/picker" "github.com/velour/catbase/plugins/reaction" "github.com/velour/catbase/plugins/reminder" "github.com/velour/catbase/plugins/rpgORdie" @@ -60,6 +61,7 @@ func main() { // b.AddHandler("downtime", downtime.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)) diff --git a/plugins/picker/picker.go b/plugins/picker/picker.go new file mode 100644 index 0000000..7b0c3a7 --- /dev/null +++ b/plugins/picker/picker.go @@ -0,0 +1,78 @@ +// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors. + +package picker + +import ( + "strings" + "time" + + "fmt" + "math/rand" + + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" +) + +type PickerPlugin struct { + Bot bot.Bot +} + +// NewPickerPlugin creates a new PickerPlugin with the Plugin interface +func New(bot bot.Bot) *PickerPlugin { + rand.Seed(time.Now().Unix()) + + return &PickerPlugin{ + Bot: bot, + } +} + +func rollDie(sides int) int { + return rand.Intn(sides) + 1 +} + +// 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 { + if !message.Command { + return false + } + + body := message.Body + pfx, sfx := "pick {", "}" + + if strings.HasPrefix(body, pfx) && strings.HasSuffix(body, sfx) { + body = strings.TrimSuffix(strings.TrimPrefix(body, pfx), sfx) + items := strings.Split(body, ",") + item := items[rand.Intn(len(items))] + + out := fmt.Sprintf("I've chosen \"%s\" for you.", item) + + p.Bot.SendMessage(message.Channel, out) + + return true + } + return false +} + +// 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}\".") +} + +// 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 +} + +// Register any web URLs desired +func (p *PickerPlugin) RegisterWeb() *string { + return nil +} + +func (p *PickerPlugin) ReplyMessage(message msg.Message, identifier string) bool { return false }