mirror of https://github.com/velour/catbase.git
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:
parent
59b42b1556
commit
1dafd3af6e
|
@ -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.
|
||||
|
|
|
@ -34,19 +34,9 @@ func (b *Bot) checkuser(nick string) *User {
|
|||
return user
|
||||
}
|
||||
|
||||
// Handles incomming PRIVMSG requests
|
||||
func (b *Bot) Msg_recieved(conn *irc.Conn, line *irc.Line) {
|
||||
// Check for the user
|
||||
user := b.checkuser(line.Nick)
|
||||
|
||||
channel := line.Args[0]
|
||||
message := line.Args[1]
|
||||
parts := strings.Fields(strings.ToLower(message))
|
||||
|
||||
user.MessageLog = append(user.MessageLog, message)
|
||||
|
||||
if len(parts) > 0 && parts[0] == "help" {
|
||||
if len(parts) == 1 {
|
||||
// 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 {
|
||||
|
@ -67,6 +57,53 @@ func (b *Bot) Msg_recieved(conn *irc.Conn, line *irc.Line) {
|
|||
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) 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 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
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
"Server": "127.0.0.1:6666",
|
||||
"Nick": "AlePaleTest",
|
||||
"Pass": "AlePaleTest:test",
|
||||
"CommandChar": "!",
|
||||
|
||||
"comment": "Follows is the old bot",
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
4
main.go
4
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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue