mirror of https://github.com/velour/catbase.git
Massive improvements to the remember plugin, fixed some help issues, added ACTION parsing.
This commit is contained in:
parent
64394b90f9
commit
3953f0a831
|
@ -44,7 +44,9 @@ type User struct {
|
|||
type Message struct {
|
||||
User *User
|
||||
Channel, Body string
|
||||
Raw string
|
||||
Command bool
|
||||
Action bool
|
||||
}
|
||||
|
||||
// NewBot creates a Bot for a given connection and set of handlers.
|
||||
|
|
|
@ -88,16 +88,23 @@ func (b *Bot) isCmd(message string) (bool, string) {
|
|||
// Handles incomming PRIVMSG requests
|
||||
func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) {
|
||||
// Check for the user
|
||||
|
||||
user := b.checkuser(line.Nick)
|
||||
|
||||
channel := line.Args[0]
|
||||
if channel == conn.Me.Nick {
|
||||
channel = line.Nick
|
||||
}
|
||||
|
||||
isaction := line.Cmd == "ACTION"
|
||||
|
||||
message := line.Args[1]
|
||||
iscmd := false
|
||||
iscmd, message = b.isCmd(message)
|
||||
parts := strings.Fields(strings.ToLower(message))
|
||||
filteredMessage := message
|
||||
if !isaction {
|
||||
iscmd, filteredMessage = b.isCmd(message)
|
||||
}
|
||||
parts := strings.Fields(strings.ToLower(filteredMessage))
|
||||
|
||||
if iscmd {
|
||||
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)
|
||||
|
||||
if strings.HasPrefix(message, "help") {
|
||||
if strings.HasPrefix(filteredMessage, "help") && iscmd{
|
||||
b.checkHelp(channel, parts)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("In %s, %s said: '%s'\n", channel, line.Nick, message)
|
||||
for _, p := range b.Plugins {
|
||||
msg := Message{
|
||||
User: user,
|
||||
Channel: channel,
|
||||
Body: message,
|
||||
Body: filteredMessage,
|
||||
Raw: message,
|
||||
Command: iscmd,
|
||||
Action: isaction,
|
||||
}
|
||||
fmt.Printf("%#v\n", msg)
|
||||
for _, p := range b.Plugins {
|
||||
if p.Message(msg) {
|
||||
break
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
"Pass": "AlePaleTest:test",
|
||||
"CommandChar": "!",
|
||||
"QuoteChance": 0.10,
|
||||
"QuoteTime": 30,
|
||||
"LogLength": 50,
|
||||
|
||||
"comment": "Follows is the old bot",
|
||||
|
|
|
@ -16,6 +16,7 @@ type Config struct {
|
|||
Version string
|
||||
CommandChar string
|
||||
QuoteChance float64
|
||||
QuoteTime int
|
||||
LogLength int
|
||||
}
|
||||
|
||||
|
|
4
main.go
4
main.go
|
@ -48,6 +48,10 @@ func main() {
|
|||
b.AddHandler("beers", plugins.NewBeersPlugin(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) {
|
||||
b.MsgRecieved(conn, line)
|
||||
})
|
||||
|
|
|
@ -46,37 +46,56 @@ func (p *RememberPlugin) Message(message bot.Message) bool {
|
|||
if message.Body == "quote" && message.Command {
|
||||
q := p.randQuote()
|
||||
p.Bot.SendMessage(message.Channel, q)
|
||||
|
||||
// is it evil not to remember that the user said quote?
|
||||
return true
|
||||
}
|
||||
|
||||
parts := strings.Fields(message.Body)
|
||||
if len(parts) < 3 || parts[0] != "remember" {
|
||||
p.Log[message.Channel] = append(p.Log[message.Channel], message)
|
||||
return false
|
||||
} else {
|
||||
if message.Command && len(parts) >= 3 && parts[0] == "remember" {
|
||||
// we have a remember!
|
||||
// look through the logs and find parts[1] as a user, if not, fuck this hoser
|
||||
nick := parts[1]
|
||||
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
|
||||
fmt.Printf("Comparing '%s' to '%s'\n", entry.Raw, snip)
|
||||
if entry.User.Name == nick && strings.Contains(entry.Body, snip) {
|
||||
// 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{
|
||||
Nick: entry.User.Name,
|
||||
Message: fmt.Sprintf("<%s> %s", entry.User.Name, entry.Body),
|
||||
Message: msg,
|
||||
Date: time.Now(),
|
||||
}
|
||||
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.Log[message.Channel] = append(p.Log[message.Channel], message)
|
||||
return true
|
||||
}
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -85,9 +104,6 @@ func (p *RememberPlugin) Message(message bot.Message) bool {
|
|||
// date.
|
||||
func (p *RememberPlugin) LoadData() {
|
||||
p.Coll = p.Bot.Db.C("remember")
|
||||
if p.Coll == nil {
|
||||
panic("FUCK ME")
|
||||
}
|
||||
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
|
||||
func (p *RememberPlugin) randQuote() string {
|
||||
var quotes []userRemember
|
||||
if p.Coll == nil {
|
||||
panic("FUCK ME HARD")
|
||||
}
|
||||
iter := p.Coll.Find(bson.M{}).Iter()
|
||||
err := iter.All("es)
|
||||
if err != nil {
|
||||
|
@ -124,16 +137,22 @@ func (p *RememberPlugin) randQuote() string {
|
|||
|
||||
// rand quote idx
|
||||
nquotes := len(quotes)
|
||||
if nquotes == 0 {
|
||||
return "Sorry, I don't know any quotes."
|
||||
}
|
||||
quote := quotes[rand.Intn(nquotes)]
|
||||
return quote.Message
|
||||
}
|
||||
|
||||
func (p *RememberPlugin) quoteTimer(channel string) {
|
||||
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
|
||||
if rand.Intn(int(chance)) == 0 {
|
||||
msg := p.randQuote()
|
||||
fmt.Println("Delivering quote.")
|
||||
p.Bot.SendMessage(channel, msg)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue