From 6f3c817e7f5610cfcdc629dc85314294eb7d994e Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 26 Aug 2012 15:14:09 -0400 Subject: [PATCH] Initial version of factoids seems to work. Probably some bugs to work out. Added some $vars. --- bot/handlers.go | 25 ++++++++++++++++++++++++- main.go | 3 ++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/bot/handlers.go b/bot/handlers.go index 32d17f8..fcaf0d5 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -2,7 +2,10 @@ package bot import ( "fmt" + "math/rand" + "strconv" "strings" + "time" ) import irc "github.com/fluffle/goirc/client" @@ -140,13 +143,33 @@ func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { // Take an input string and mutate it based on $vars in the string func (b *Bot) Filter(message Message, input string) string { + rand.Seed(time.Now().Unix()) + if strings.Contains(input, "$NICK") { nick := strings.ToUpper(message.User.Name) input = strings.Replace(input, "$NICK", nick, -1) - } else if strings.Contains(input, "$nick") { + } + + if strings.Contains(input, "$nick") { nick := message.User.Name input = strings.Replace(input, "$nick", nick, -1) } + + if strings.Contains(input, "$someone") { + someone := b.Users[rand.Intn(len(b.Users))].Name + input = strings.Replace(input, "$someone", someone, -1) + } + + for strings.Contains(input, "$digit") { + num := strconv.Itoa(rand.Intn(9)) + input = strings.Replace(input, "$digit", num, 1) + } + + for strings.Contains(input, "$nonzero") { + num := strconv.Itoa(rand.Intn(8) + 1) + input = strings.Replace(input, "$nonzero", num, 1) + } + return input } diff --git a/main.go b/main.go index 133ba7f..2efcd9c 100644 --- a/main.go +++ b/main.go @@ -46,8 +46,9 @@ func main() { b.AddHandler("talker", plugins.NewTalkerPlugin(b)) b.AddHandler("beers", plugins.NewBeersPlugin(b)) b.AddHandler("remember", plugins.NewRememberPlugin(b)) - b.AddHandler("factoid", plugins.NewFactoidPlugin(b)) b.AddHandler("skeleton", plugins.NewSkeletonPlugin(b)) + // catches anything left, will always return true + b.AddHandler("factoid", plugins.NewFactoidPlugin(b)) c.AddHandler("ACTION", func(conn *irc.Conn, line *irc.Line) { b.MsgRecieved(conn, line)