Massive improvements to the remember plugin, fixed some help issues, added ACTION parsing.

This commit is contained in:
Chris Sexton 2012-08-23 16:28:45 -04:00
parent 64394b90f9
commit 3953f0a831
6 changed files with 62 additions and 26 deletions

View File

@ -44,7 +44,9 @@ type User struct {
type Message struct { type Message struct {
User *User User *User
Channel, Body string Channel, Body string
Raw string
Command bool Command bool
Action bool
} }
// NewBot creates a Bot for a given connection and set of handlers. // NewBot creates a Bot for a given connection and set of handlers.

View File

@ -88,16 +88,23 @@ func (b *Bot) isCmd(message string) (bool, string) {
// Handles incomming PRIVMSG requests // Handles incomming PRIVMSG requests
func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
// Check for the user // Check for the user
user := b.checkuser(line.Nick) user := b.checkuser(line.Nick)
channel := line.Args[0] channel := line.Args[0]
if channel == conn.Me.Nick { if channel == conn.Me.Nick {
channel = line.Nick channel = line.Nick
} }
isaction := line.Cmd == "ACTION"
message := line.Args[1] message := line.Args[1]
iscmd := false iscmd := false
iscmd, message = b.isCmd(message) filteredMessage := message
parts := strings.Fields(strings.ToLower(message)) if !isaction {
iscmd, filteredMessage = b.isCmd(message)
}
parts := strings.Fields(strings.ToLower(filteredMessage))
if iscmd { if iscmd {
fmt.Println("Hey, I got a command!") fmt.Println("Hey, I got a command!")
@ -105,19 +112,21 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
user.MessageLog = append(user.MessageLog, message) user.MessageLog = append(user.MessageLog, message)
if strings.HasPrefix(message, "help") { if strings.HasPrefix(filteredMessage, "help") && iscmd{
b.checkHelp(channel, parts) b.checkHelp(channel, parts)
return return
} }
fmt.Printf("In %s, %s said: '%s'\n", channel, line.Nick, message)
for _, p := range b.Plugins {
msg := Message{ msg := Message{
User: user, User: user,
Channel: channel, Channel: channel,
Body: message, Body: filteredMessage,
Raw: message,
Command: iscmd, Command: iscmd,
Action: isaction,
} }
fmt.Printf("%#v\n", msg)
for _, p := range b.Plugins {
if p.Message(msg) { if p.Message(msg) {
break break
} }

View File

@ -9,6 +9,7 @@
"Pass": "AlePaleTest:test", "Pass": "AlePaleTest:test",
"CommandChar": "!", "CommandChar": "!",
"QuoteChance": 0.10, "QuoteChance": 0.10,
"QuoteTime": 30,
"LogLength": 50, "LogLength": 50,
"comment": "Follows is the old bot", "comment": "Follows is the old bot",

View File

@ -16,6 +16,7 @@ type Config struct {
Version string Version string
CommandChar string CommandChar string
QuoteChance float64 QuoteChance float64
QuoteTime int
LogLength int LogLength int
} }

View File

@ -48,6 +48,10 @@ func main() {
b.AddHandler("beers", plugins.NewBeersPlugin(b)) b.AddHandler("beers", plugins.NewBeersPlugin(b))
b.AddHandler("remember", plugins.NewRememberPlugin(b)) b.AddHandler("remember", plugins.NewRememberPlugin(b))
c.AddHandler("ACTION", func(conn *irc.Conn, line *irc.Line) {
b.MsgRecieved(conn, line)
})
c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) { c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) {
b.MsgRecieved(conn, line) b.MsgRecieved(conn, line)
}) })

View File

@ -46,37 +46,56 @@ func (p *RememberPlugin) Message(message bot.Message) bool {
if message.Body == "quote" && message.Command { if message.Body == "quote" && message.Command {
q := p.randQuote() q := p.randQuote()
p.Bot.SendMessage(message.Channel, q) p.Bot.SendMessage(message.Channel, q)
// is it evil not to remember that the user said quote?
return true return true
} }
parts := strings.Fields(message.Body) parts := strings.Fields(message.Body)
if len(parts) < 3 || parts[0] != "remember" { if message.Command && len(parts) >= 3 && parts[0] == "remember" {
p.Log[message.Channel] = append(p.Log[message.Channel], message)
return false
} else {
// we have a remember! // we have a remember!
// look through the logs and find parts[1] as a user, if not, fuck this hoser // look through the logs and find parts[1] as a user, if not, fuck this hoser
nick := parts[1] nick := parts[1]
snip := strings.Join(parts[2:], " ") snip := strings.Join(parts[2:], " ")
for _, entry := range p.Log[message.Channel] {
if nick == message.User.Name {
msg := fmt.Sprintf("Don't try to quote yourself, %s.", nick)
p.Bot.SendMessage(message.Channel, msg)
return true
}
for i := len(p.Log[message.Channel])-1; i >= 0; i-- {
entry := p.Log[message.Channel][i]
// find the entry we want // find the entry we want
fmt.Printf("Comparing '%s' to '%s'\n", entry.Raw, snip)
if entry.User.Name == nick && strings.Contains(entry.Body, snip) { if entry.User.Name == nick && strings.Contains(entry.Body, snip) {
// insert new remember entry // insert new remember entry
var msg string
// check if it's an action
if entry.Action {
msg = fmt.Sprintf("*%s* %s", entry.User.Name, entry.Raw)
} else {
msg = fmt.Sprintf("<%s> %s", entry.User.Name, entry.Raw)
}
u := userRemember{ u := userRemember{
Nick: entry.User.Name, Nick: entry.User.Name,
Message: fmt.Sprintf("<%s> %s", entry.User.Name, entry.Body), Message: msg,
Date: time.Now(), Date: time.Now(),
} }
p.Coll.Insert(u) p.Coll.Insert(u)
msg := fmt.Sprintf("Okay, %s, remembering '<%s> %s'.",
message.User.Name, entry.User.Name, entry.Body) // sorry, not creative with names so we're reusing msg
msg = fmt.Sprintf("Okay, %s, remembering '%s'.",
message.User.Name, msg)
p.Bot.SendMessage(message.Channel, msg) p.Bot.SendMessage(message.Channel, msg)
p.Log[message.Channel] = append(p.Log[message.Channel], message)
return true return true
} }
} }
p.Bot.SendMessage(message.Channel, "Sorry, I don't know that phrase.") p.Bot.SendMessage(message.Channel, "Sorry, I don't know that phrase.")
return true
} }
p.Log[message.Channel] = append(p.Log[message.Channel], message)
return false return false
} }
@ -85,9 +104,6 @@ func (p *RememberPlugin) Message(message bot.Message) bool {
// date. // date.
func (p *RememberPlugin) LoadData() { func (p *RememberPlugin) LoadData() {
p.Coll = p.Bot.Db.C("remember") p.Coll = p.Bot.Db.C("remember")
if p.Coll == nil {
panic("FUCK ME")
}
rand.Seed(time.Now().Unix()) rand.Seed(time.Now().Unix())
} }
@ -113,9 +129,6 @@ func (p *RememberPlugin) record(nick, msg string) {
// to have this function execute a quote for a particular channel // to have this function execute a quote for a particular channel
func (p *RememberPlugin) randQuote() string { func (p *RememberPlugin) randQuote() string {
var quotes []userRemember var quotes []userRemember
if p.Coll == nil {
panic("FUCK ME HARD")
}
iter := p.Coll.Find(bson.M{}).Iter() iter := p.Coll.Find(bson.M{}).Iter()
err := iter.All(&quotes) err := iter.All(&quotes)
if err != nil { if err != nil {
@ -124,16 +137,22 @@ func (p *RememberPlugin) randQuote() string {
// rand quote idx // rand quote idx
nquotes := len(quotes) nquotes := len(quotes)
if nquotes == 0 {
return "Sorry, I don't know any quotes."
}
quote := quotes[rand.Intn(nquotes)] quote := quotes[rand.Intn(nquotes)]
return quote.Message return quote.Message
} }
func (p *RememberPlugin) quoteTimer(channel string) { func (p *RememberPlugin) quoteTimer(channel string) {
for { for {
time.Sleep(30 * time.Minute) // this pisses me off: You can't multiply int * time.Duration so it
// has to look ugly as shit.
time.Sleep(time.Duration(p.Bot.Config.QuoteTime) * time.Minute)
chance := 1.0 / p.Bot.Config.QuoteChance chance := 1.0 / p.Bot.Config.QuoteChance
if rand.Intn(int(chance)) == 0 { if rand.Intn(int(chance)) == 0 {
msg := p.randQuote() msg := p.randQuote()
fmt.Println("Delivering quote.")
p.Bot.SendMessage(channel, msg) p.Bot.SendMessage(channel, msg)
} }
} }