Simplify remember code a bit

This commit is contained in:
Chris Sexton 2016-04-22 13:48:43 -04:00
parent 8485ed9fe3
commit 9bff7bf79a
1 changed files with 51 additions and 73 deletions

View File

@ -5,7 +5,6 @@ package fact
import ( import (
"fmt" "fmt"
"log" "log"
"math/rand"
"strings" "strings"
"time" "time"
@ -55,42 +54,27 @@ func (p *RememberPlugin) Message(message msg.Message) bool {
// we have a remember! // we have a remember!
// look through the logs and find parts[1] as a user, if not, // look through the logs and find parts[1] as a user, if not,
// fuck this hoser // fuck this hoser
snips := strings.Split(strings.Join(parts[1:], " "), "$and") nick := parts[1]
var msgs []string snip := strings.Join(parts[2:], " ")
var trigger string
for _, snip := range snips {
snip = strings.TrimSpace(snip)
snipParts := strings.Split(snip, " ")
nick := snipParts[0]
snip := strings.Join(snipParts[1:], " ")
for i := len(p.Log[message.Channel]) - 1; i >= 0; i-- { for i := len(p.Log[message.Channel]) - 1; i >= 0; i-- {
entry := p.Log[message.Channel][i] entry := p.Log[message.Channel][i]
log.Printf("Comparing %s:%s with %s:%s",
entry.User.Name, entry.Body, nick, snip)
if strings.ToLower(entry.User.Name) == strings.ToLower(nick) && if strings.ToLower(entry.User.Name) == strings.ToLower(nick) &&
strings.Contains( strings.Contains(
strings.ToLower(entry.Body), strings.ToLower(entry.Body),
strings.ToLower(snip), strings.ToLower(snip),
) { ) {
log.Printf("Found!")
// check if it's an action var msg string
if entry.Action { if entry.Action {
msgs = append(msgs, fmt.Sprintf("*%s* %s", entry.User.Name, entry.Body)) msg = fmt.Sprintf("*%s* %s", entry.User.Name, entry.Body)
} else { } else {
msgs = append(msgs, fmt.Sprintf("<%s> %s", entry.User.Name, entry.Body)) msg = fmt.Sprintf("<%s> %s", entry.User.Name, entry.Body)
} }
if trigger == "" { trigger := fmt.Sprintf("%s quotes", entry.User.Name)
trigger = fmt.Sprintf("%s quotes", entry.User.Name)
}
}
}
}
if len(msgs) == len(snips) {
msg := strings.Join(msgs, "$and")
fact := factoid{ fact := factoid{
Fact: strings.ToLower(trigger), Fact: strings.ToLower(trigger),
@ -112,15 +96,17 @@ func (p *RememberPlugin) Message(message msg.Message) bool {
msg = fmt.Sprintf("Okay, %s, remembering '%s'.", msg = fmt.Sprintf("Okay, %s, remembering '%s'.",
message.User.Name, msg) 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) p.recordMsg(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.")
p.Log[message.Channel] = append(p.Log[message.Channel], message) p.recordMsg(message)
return true return true
} }
p.Log[message.Channel] = append(p.Log[message.Channel], message) p.recordMsg(message)
return false return false
} }
@ -164,19 +150,6 @@ func (p *RememberPlugin) randQuote() string {
return f.Tidbit return f.Tidbit
} }
func (p *RememberPlugin) quoteTimer(channel string) {
for {
// 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().Factoid.QuoteTime) * time.Minute)
chance := 1.0 / p.Bot.Config().Factoid.QuoteChance
if rand.Intn(int(chance)) == 0 {
msg := p.randQuote()
p.Bot.SendMessage(channel, msg)
}
}
}
// 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 *RememberPlugin) Event(kind string, message msg.Message) bool { func (p *RememberPlugin) Event(kind string, message msg.Message) bool {
return false return false
@ -184,7 +157,7 @@ func (p *RememberPlugin) Event(kind string, message msg.Message) bool {
// Record what the bot says in the log // Record what the bot says in the log
func (p *RememberPlugin) BotMessage(message msg.Message) bool { func (p *RememberPlugin) BotMessage(message msg.Message) bool {
p.Log[message.Channel] = append(p.Log[message.Channel], message) p.recordMsg(message)
return false return false
} }
@ -192,3 +165,8 @@ func (p *RememberPlugin) BotMessage(message msg.Message) bool {
func (p *RememberPlugin) RegisterWeb() *string { func (p *RememberPlugin) RegisterWeb() *string {
return nil return nil
} }
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)
}