Added functionality for downtime tracking

This commit is contained in:
Chris Sexton 2013-01-22 16:14:04 -05:00
parent ce94b0b258
commit 66e2c8265d
4 changed files with 61 additions and 6 deletions

View File

@ -114,7 +114,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot {
RunNewLogger(logIn, logOut) RunNewLogger(logIn, logOut)
config.Nick = c.Me.Name config.Nick = c.Me.Nick
return &Bot{ return &Bot{
Config: config, Config: config,

View File

@ -44,6 +44,7 @@ func main() {
b := bot.NewBot(config, c) b := bot.NewBot(config, c)
// b.AddHandler(plugins.NewTestPlugin(b)) // b.AddHandler(plugins.NewTestPlugin(b))
b.AddHandler("admin", plugins.NewAdminPlugin(b)) b.AddHandler("admin", plugins.NewAdminPlugin(b))
b.AddHandler("downtime", plugins.NewDowntimePlugin(b))
b.AddHandler("talker", plugins.NewTalkerPlugin(b)) b.AddHandler("talker", plugins.NewTalkerPlugin(b))
b.AddHandler("dice", plugins.NewDicePlugin(b)) b.AddHandler("dice", plugins.NewDicePlugin(b))
b.AddHandler("beers", plugins.NewBeersPlugin(b)) b.AddHandler("beers", plugins.NewBeersPlugin(b))

View File

@ -3,7 +3,11 @@ package plugins
import "bitbucket.org/phlyingpenguin/godeepintir/bot" import "bitbucket.org/phlyingpenguin/godeepintir/bot"
import ( import (
"fmt"
"labix.org/v2/mgo" "labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"strings"
"time"
) )
// This is a downtime plugin to monitor how much our users suck // This is a downtime plugin to monitor how much our users suck
@ -13,19 +17,65 @@ type DowntimePlugin struct {
Coll *mgo.Collection Coll *mgo.Collection
} }
type idleEntry struct {
Nick string
LastSeen time.Time
}
// NewDowntimePlugin creates a new DowntimePlugin with the Plugin interface // NewDowntimePlugin creates a new DowntimePlugin with the Plugin interface
func NewDowntimePlugin(bot *bot.Bot) *DowntimePlugin { func NewDowntimePlugin(bot *bot.Bot) *DowntimePlugin {
return &DowntimePlugin{ p := DowntimePlugin{
Bot: bot, Bot: bot,
} }
p.LoadData()
return &p
} }
// Message responds to the bot hook on recieving messages. // 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. // 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. // Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *DowntimePlugin) Message(message bot.Message) bool { func (p *DowntimePlugin) Message(message bot.Message) bool {
// This bot does not reply to anything // If it's a command and the payload is idle <nick>, give it. Log everything.
return false
parts := strings.Fields(strings.ToLower(message.Body))
channel := message.Channel
ret := false
if parts[0] == "idle" && len(parts) == 2 {
nick := parts[1]
// parts[1] must be the userid, or we don't know them
var entry idleEntry
p.Coll.Find(bson.M{"nick": nick}).One(&entry)
if entry.Nick != nick {
// couldn't find em
p.Bot.SendMessage(channel, fmt.Sprintf("Sorry, I don't know %s.", nick))
} else {
p.Bot.SendMessage(channel, fmt.Sprintf("%s has been idle for: %s",
nick, time.Now().Sub(entry.LastSeen)))
}
ret = true
}
p.record(strings.ToLower(message.User.Name))
return ret
}
func (p *DowntimePlugin) record(user string) {
var entry idleEntry
p.Coll.Find(bson.M{"nick": user}).One(&entry)
if entry.Nick != user {
// insert a new entry
p.Coll.Insert(idleEntry{
Nick: user,
LastSeen: time.Now(),
})
} else {
// Update their entry, they were baaaaaad
entry.LastSeen = time.Now()
p.Coll.Upsert(bson.M{"nick": entry.Nick}, entry)
}
} }
// LoadData imports any configuration data into the plugin. This is not strictly necessary other // LoadData imports any configuration data into the plugin. This is not strictly necessary other
@ -37,10 +87,14 @@ func (p *DowntimePlugin) LoadData() {
// Help responds to help requests. Every plugin must implement a help function. // Help responds to help requests. Every plugin must implement a help function.
func (p *DowntimePlugin) Help(channel string, parts []string) { func (p *DowntimePlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Sorry, Downtime does not do a goddamn thing.") p.Bot.SendMessage(channel, "Ask me how long one of your friends has been idele with, \"idle <nick>\"")
} }
// Empty event handler because this plugin does not do anything on event recv // Empty event handler because this plugin does not do anything on event recv
func (p *DowntimePlugin) Event(kind string, message bot.Message) bool { func (p *DowntimePlugin) Event(kind string, message bot.Message) bool {
if kind == "JOIN" && message.User.Name != p.Bot.Config.Nick {
// user joined, let's nail them for it
p.record(strings.ToLower(message.User.Name))
}
return false return false
} }

View File

@ -54,7 +54,7 @@ func (p *TalkerPlugin) Help(channel string, parts []string) {
// Empty event handler because this plugin does not do anything on event recv // Empty event handler because this plugin does not do anything on event recv
func (p *TalkerPlugin) Event(kind string, message bot.Message) bool { func (p *TalkerPlugin) Event(kind string, message bot.Message) bool {
if kind == "JOIN" && message.User.Name != p.Bot.Config.Nick { if kind == "JOIN" && strings.ToLower(message.User.Name) != strings.ToLower(p.Bot.Config.Nick) {
sayings := p.Bot.Config.WelcomeMsgs sayings := p.Bot.Config.WelcomeMsgs
msg := fmt.Sprintf(sayings[rand.Intn(len(sayings))], message.User.Name) msg := fmt.Sprintf(sayings[rand.Intn(len(sayings))], message.User.Name)
p.Bot.SendMessage(message.Channel, msg) p.Bot.SendMessage(message.Channel, msg)