Added a lot more code to the beers plugin. Needs datastore still and some more

of its commands, but the skeleton is there. Added support for commands.
This commit is contained in:
Chris Sexton 2012-08-17 21:39:26 -04:00
parent 59b42b1556
commit 1dafd3af6e
6 changed files with 158 additions and 27 deletions

View File

@ -44,6 +44,7 @@ type User struct {
type Message struct { type Message struct {
User *User User *User
Channel, Body string Channel, Body string
Command bool
} }
// NewBot creates a Bot for a given connection and set of handlers. // NewBot creates a Bot for a given connection and set of handlers.

View File

@ -34,39 +34,76 @@ func (b *Bot) checkuser(nick string) *User {
return 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 // 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 // Check for the user
user := b.checkuser(line.Nick) user := b.checkuser(line.Nick)
channel := line.Args[0] channel := line.Args[0]
message := line.Args[1] message := line.Args[1]
iscmd := false
iscmd, message = b.isCmd(message)
parts := strings.Fields(strings.ToLower(message)) parts := strings.Fields(strings.ToLower(message))
if iscmd {
fmt.Println("Hey, I got a command!")
}
user.MessageLog = append(user.MessageLog, message) user.MessageLog = append(user.MessageLog, message)
if len(parts) > 0 && parts[0] == "help" { if strings.HasPrefix(message, "help") {
if len(parts) == 1 { b.checkHelp(channel, parts)
// 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)
}
}
return return
} }
@ -76,6 +113,7 @@ func (b *Bot) Msg_recieved(conn *irc.Conn, line *irc.Line) {
User: user, User: user,
Channel: channel, Channel: channel,
Body: message, Body: message,
Command: iscmd,
} }
if p.Message(msg) { if p.Message(msg) {
break break

View File

@ -7,6 +7,7 @@
"Server": "127.0.0.1:6666", "Server": "127.0.0.1:6666",
"Nick": "AlePaleTest", "Nick": "AlePaleTest",
"Pass": "AlePaleTest:test", "Pass": "AlePaleTest:test",
"CommandChar": "!",
"comment": "Follows is the old bot", "comment": "Follows is the old bot",

View File

@ -14,6 +14,7 @@ type Config struct {
Plugins []string Plugins []string
Nick, Server, Pass string Nick, Server, Pass string
Version string Version string
CommandChar string
} }
// Readconfig loads the config data out of a JSON file located in cfile // Readconfig loads the config data out of a JSON file located in cfile

View File

@ -43,12 +43,12 @@ func main() {
b := bot.NewBot(config, c) b := bot.NewBot(config, c)
// b.AddHandler(plugins.NewTestPlugin(b)) // b.AddHandler(plugins.NewTestPlugin(b))
b.AddHandler("skeleton", plugins.NewSkeletonPlugin(b))
b.AddHandler("talker", plugins.NewTalkerPlugin(b)) b.AddHandler("talker", plugins.NewTalkerPlugin(b))
b.AddHandler("beers", plugins.NewBeersPlugin(b)) b.AddHandler("beers", plugins.NewBeersPlugin(b))
b.AddHandler("skeleton", plugins.NewSkeletonPlugin(b))
c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) { c.AddHandler("PRIVMSG", func(conn *irc.Conn, line *irc.Line) {
b.Msg_recieved(conn, line) b.MsgRecieved(conn, line)
}) })
// Tell client to connect // Tell client to connect

View File

@ -1,6 +1,12 @@
package plugins 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. // 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. // 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. // 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. // Otherwise, the function returns false and the bot continues execution of other plugins.
func (p *BeersPlugin) Message(message bot.Message) bool { 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 return false
} }
@ -34,3 +101,26 @@ func (p *BeersPlugin) LoadData() {
func (p *BeersPlugin) Help(channel string, parts []string) { func (p *BeersPlugin) Help(channel string, parts []string) {
p.Bot.SendMessage(channel, "Sorry, Beers does not do a goddamn thing.") 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
}