Added dynamic variables (I'm sure this is going to backfire)

This commit is contained in:
Chris Sexton 2012-08-26 19:23:51 -04:00
parent 24e8010efc
commit b418096bf6
5 changed files with 45 additions and 0 deletions

View File

@ -24,6 +24,8 @@ type Bot struct {
DbSession *mgo.Session
Db *mgo.Database
varColl *mgo.Collection
Version string
}
@ -40,6 +42,8 @@ type User struct {
// Last N messages sent to the user
MessageLog []string
Admin bool
}
type Message struct {
@ -50,6 +54,10 @@ type Message struct {
Action bool
}
type Variable struct {
Variable, Value string
}
// NewBot creates a Bot for a given connection and set of handlers.
func NewBot(config *config.Config, c *irc.Conn) *Bot {
session, err := mgo.Dial(config.DbServer)
@ -67,6 +75,7 @@ func NewBot(config *config.Config, c *irc.Conn) *Bot {
Conn: c,
DbSession: session,
Db: db,
varColl: db.C("variables"),
Version: config.Version,
}
}

View File

@ -6,6 +6,9 @@ import (
"strconv"
"strings"
"time"
// "labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"regexp"
)
import irc "github.com/fluffle/goirc/client"
@ -27,10 +30,17 @@ func (b *Bot) checkuser(nick string) *User {
}
}
if user == nil {
isadmin := false
for _, u := range b.Config.Admins {
if nick == u {
isadmin = true
}
}
user = &User{
Name: nick,
Alts: make([]string, 1),
MessageLog: make([]string, 50),
Admin: isadmin,
}
b.Users = append(b.Users, *user)
}
@ -150,6 +160,8 @@ func (b *Bot) Filter(message Message, input string) string {
input = strings.Replace(input, "$NICK", nick, -1)
}
// Let's be bucket compatible for this var
strings.Replace(input, "$who", "$nick", -1)
if strings.Contains(input, "$nick") {
nick := message.User.Name
input = strings.Replace(input, "$nick", nick, -1)
@ -170,6 +182,27 @@ func (b *Bot) Filter(message Message, input string) string {
input = strings.Replace(input, "$nonzero", num, 1)
}
r, err := regexp.Compile("\\$[A-z]+")
if err != nil {
panic(err)
}
varname := r.FindString(input)
blacklist := make(map[string]bool)
blacklist["$and"] = true
for len(varname) > 0 && !blacklist[varname] {
fmt.Printf("Trying to match '%s'\n", varname)
var result []Variable
b.varColl.Find(bson.M{"variable": varname}).All(&result)
if len(result) == 0 {
blacklist[varname] = true
continue
}
variable := result[rand.Intn(len(result))]
input = strings.Replace(input, varname, variable.Value, 1)
varname = r.FindString(input)
}
return input
}

View File

@ -11,6 +11,7 @@
"QuoteChance": 0.10,
"QuoteTime": 30,
"LogLength": 50,
"Admins": ["flyngpngn"],
"comment": "Follows is the old bot",

View File

@ -18,6 +18,7 @@ type Config struct {
QuoteChance float64
QuoteTime int
LogLength int
Admins []string
}
// Readconfig loads the config data out of a JSON file located in cfile

View File

@ -43,6 +43,7 @@ func main() {
b := bot.NewBot(config, c)
// b.AddHandler(plugins.NewTestPlugin(b))
b.AddHandler("admin", plugins.NewAdminPlugin(b))
b.AddHandler("talker", plugins.NewTalkerPlugin(b))
b.AddHandler("beers", plugins.NewBeersPlugin(b))
b.AddHandler("remember", plugins.NewRememberPlugin(b))