diff --git a/bot/bot.go b/bot/bot.go index a49bd46..057ab37 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -44,6 +44,7 @@ type User struct { type Message struct { User *User Channel, Body string + Command bool } // NewBot creates a Bot for a given connection and set of handlers. diff --git a/bot/handlers.go b/bot/handlers.go index b76fd27..f03a83f 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -34,39 +34,76 @@ func (b *Bot) checkuser(nick string) *User { return user } +// Checks to see if the user is asking for help, returns true if so and handles the situation. +func (b *Bot) checkHelp(channel string, parts []string) { + if len(parts) > 1 { + // just print out a list of help topics + topics := "Help topics: about" + for name, _ := range b.Plugins { + topics = fmt.Sprintf("%s, %s", topics, name) + } + b.SendMessage(channel, topics) + } else { + // trigger the proper plugin's help response + if parts[1] == "about" { + b.Help(channel, parts) + return + } + plugin := b.Plugins[parts[1]] + if plugin != nil { + plugin.Help(channel, parts) + } else { + msg := fmt.Sprintf("I'm sorry, I don't know what %s is!", parts[1]) + b.SendMessage(channel, msg) + } + } +} + +// Checks if message is a command and returns its curtailed version +func (b *Bot) isCmd(message string) (bool, string) { + cmdc := b.Config.CommandChar + botnick := b.Conn.Me.Nick + iscmd := false + + if strings.HasPrefix(message, cmdc) && len(cmdc) > 0 { + fmt.Println("Got a commandchar prefix") + iscmd = true + message = message[len(cmdc):] + } else if strings.HasPrefix(message, botnick) { + iscmd = true + message = message[len(botnick):] + + // trim off the customary addressing punctuation + if message[0] == ':' || message[0] == ',' { + message = message[1:] + } + } + + // trim off any whitespace left on the message + message = strings.TrimSpace(message) + + return iscmd, message +} + // Handles incomming PRIVMSG requests -func (b *Bot) Msg_recieved(conn *irc.Conn, line *irc.Line) { +func (b *Bot) MsgRecieved(conn *irc.Conn, line *irc.Line) { // Check for the user user := b.checkuser(line.Nick) channel := line.Args[0] message := line.Args[1] + iscmd := false + iscmd, message = b.isCmd(message) parts := strings.Fields(strings.ToLower(message)) + if iscmd { + fmt.Println("Hey, I got a command!") + } + user.MessageLog = append(user.MessageLog, message) - if len(parts) > 0 && parts[0] == "help" { - if len(parts) == 1 { - // just print out a list of help topics - topics := "Help topics: about" - for name, _ := range b.Plugins { - topics = fmt.Sprintf("%s, %s", topics, name) - } - b.SendMessage(channel, topics) - } else { - // trigger the proper plugin's help response - if parts[1] == "about" { - b.Help(channel, parts) - return - } - plugin := b.Plugins[parts[1]] - if plugin != nil { - plugin.Help(channel, parts) - } else { - msg := fmt.Sprintf("I'm sorry, I don't know what %s is!", parts[1]) - b.SendMessage(channel, msg) - } - } + if strings.HasPrefix(message, "help") { + b.checkHelp(channel, parts) return } @@ -76,6 +113,7 @@ func (b *Bot) Msg_recieved(conn *irc.Conn, line *irc.Line) { User: user, Channel: channel, Body: message, + Command: iscmd, } if p.Message(msg) { break diff --git a/config.json b/config.json index 29c8190..c988cb4 100644 --- a/config.json +++ b/config.json @@ -7,6 +7,7 @@ "Server": "127.0.0.1:6666", "Nick": "AlePaleTest", "Pass": "AlePaleTest:test", + "CommandChar": "!", "comment": "Follows is the old bot", diff --git a/config/config.go b/config/config.go index b4ecdf0..8c31d57 100644 --- a/config/config.go +++ b/config/config.go @@ -14,6 +14,7 @@ type Config struct { Plugins []string Nick, Server, Pass string Version string + CommandChar string } // Readconfig loads the config data out of a JSON file located in cfile diff --git a/main.go b/main.go index be576b5..c423417 100644 --- a/main.go +++ b/main.go @@ -43,12 +43,12 @@ func main() { b := bot.NewBot(config, c) // b.AddHandler(plugins.NewTestPlugin(b)) + b.AddHandler("skeleton", plugins.NewSkeletonPlugin(b)) b.AddHandler("talker", plugins.NewTalkerPlugin(b)) b.AddHandler("beers", plugins.NewBeersPlugin(b)) - b.AddHandler("skeleton", plugins.NewSkeletonPlugin(b)) c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) { - b.Msg_recieved(conn, line) + b.MsgRecieved(conn, line) }) // Tell client to connect diff --git a/plugins/beers.go b/plugins/beers.go index aee3017..b706852 100644 --- a/plugins/beers.go +++ b/plugins/beers.go @@ -1,6 +1,12 @@ package plugins -import "bitbucket.org/phlyingpenguin/godeepintir/bot" +import ( + "bitbucket.org/phlyingpenguin/godeepintir/bot" + "fmt" + "strconv" + "strings" + "time" +) // This is a skeleton plugin to serve as an example and quick copy/paste for new plugins. @@ -15,11 +21,72 @@ func NewBeersPlugin(bot *bot.Bot) *BeersPlugin { } } +type UserBeers struct { + Nick string + berrcount int + lastdrunk time.Time + momentum float64 +} + // Message responds to the bot hook on recieving messages. // This function returns true if the plugin responds in a meaningful way to the users message. // Otherwise, the function returns false and the bot continues execution of other plugins. func (p *BeersPlugin) Message(message bot.Message) bool { - // This bot does not reply to anything + parts := strings.Fields(message.Body) + + if len(parts) == 0 { + return false + } + + channel := message.Channel + user := message.User + nick := user.Name + + // respond to the beers type of queries + if parts[0] == "beers" { + if len(parts) == 3 { + + // try to get a count out of parts[2] + count, err := strconv.Atoi(parts[2]) + if err != nil { + // if it's not a number, maybe it's a nick! + if p.doIKnow(parts[2]) { + p.reportCount(parts[2], false) + } else { + msg := fmt.Sprintf("Sorry, I don't know %s.", parts[2]) + p.Bot.SendMessage(channel, msg) + } + } + + if count < 0 { + // you can't be negative + msg := fmt.Sprintf("Sorry %s, you can't have negative beers!", nick) + p.Bot.SendMessage(channel, msg) + } + if parts[1] == "+=" { + p.setBeers(user, p.getBeers(user)+count) + } else if parts[1] == "=" { + if count == 0 { + p.puke(user) + } else { + p.setBeers(user, count) + p.reportCount(nick, true) + } + } else { + p.Bot.SendMessage(channel, "I don't know your math.") + } + } + + // no matter what, if we're in here, then we've responded + return true + } + + if message.Command && parts[0] == "imbibe" { + p.setBeers(user, p.getBeers(user)+1) + p.reportCount(nick, true) + return true + } + return false } @@ -34,3 +101,26 @@ func (p *BeersPlugin) LoadData() { func (p *BeersPlugin) Help(channel string, parts []string) { p.Bot.SendMessage(channel, "Sorry, Beers does not do a goddamn thing.") } + +func (p *BeersPlugin) setBeers(user *bot.User, amount int) { +} + +func (p *BeersPlugin) addBeers(user *bot.User) { +} + +func (p *BeersPlugin) getBeers(user *bot.User) int { + return 0 +} + +func (p *BeersPlugin) hasBeers(user *bot.User) { +} + +func (p *BeersPlugin) reportCount(user string, himself bool) { +} + +func (p *BeersPlugin) puke(user *bot.User) { +} + +func (p *BeersPlugin) doIKnow(user string) bool { + return false +}