From 30077d1b6c114115123de6b8f4c03ee3ed1d3f87 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Mon, 20 Apr 2020 05:56:49 -0400 Subject: [PATCH] achievements: sketch --- main.go | 2 + plugins/achievements/achievements.go | 81 +++++++++++++++++++++++ plugins/achievements/achievements_test.go | 1 + 3 files changed, 84 insertions(+) create mode 100644 plugins/achievements/achievements.go create mode 100644 plugins/achievements/achievements_test.go diff --git a/main.go b/main.go index 7c0e539..6ade223 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "os" "time" + "github.com/velour/catbase/plugins/achievements" "github.com/velour/catbase/plugins/aoc" "github.com/velour/catbase/plugins/twitter" @@ -138,6 +139,7 @@ func main() { b.AddPlugin(impossible.New(b)) b.AddPlugin(cli.New(b)) b.AddPlugin(aoc.New(b)) + b.AddPlugin(achievements.New(b)) // catches anything left, will always return true b.AddPlugin(fact.New(b)) diff --git a/plugins/achievements/achievements.go b/plugins/achievements/achievements.go new file mode 100644 index 0000000..af3a1d3 --- /dev/null +++ b/plugins/achievements/achievements.go @@ -0,0 +1,81 @@ +package achievements + +import ( + "fmt" + + "github.com/jmoiron/sqlx" + + "github.com/velour/catbase/bot" + "github.com/velour/catbase/bot/msg" + "github.com/velour/catbase/config" +) + +// I feel dirty about this, but ;shrug +var instance *AchievementsPlugin + +type Category int + +const ( + Daily = iota + Monthly + Yearly + Forever +) + +// A plugin to track our misdeeds +type AchievementsPlugin struct { + bot bot.Bot + cfg *config.Config + db *sqlx.DB +} + +func New(b bot.Bot) *AchievementsPlugin { + if instance == nil { + ap := &AchievementsPlugin{ + bot: b, + cfg: b.Config(), + db: b.DB(), + } + instance = ap + ap.mkDB() + b.Register(ap, bot.Message, ap.message) + b.Register(ap, bot.Help, ap.help) + } + return instance +} + +func (p *AchievementsPlugin) mkDB() error { + q := ` + create table if not exists achievements + ` + tx, err := p.db.Beginx() + if err != nil { + return err + } + _, err = tx.Exec(q) + if err != nil { + return err + } + err = tx.Commit() + if err != nil { + return err + } + return nil +} + +func (p *AchievementsPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { + return false +} + +func (p *AchievementsPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool { + ch := message.Channel + me := p.bot.WhoAmI() + msg := fmt.Sprintf("%s helps those who help themselves.", me) + p.bot.Send(c, bot.Message, ch, msg) + return true +} + +// Award is used by other plugins to register a particular award for a user +func Award(nick, thing string, category Category) error { + return nil +} diff --git a/plugins/achievements/achievements_test.go b/plugins/achievements/achievements_test.go new file mode 100644 index 0000000..1ae9553 --- /dev/null +++ b/plugins/achievements/achievements_test.go @@ -0,0 +1 @@ +package achievements