Get the bot online; make counters work

Mostly just silly stuff to get everything working.
This commit is contained in:
Chris Sexton 2016-01-15 08:59:26 -05:00
parent 1efa7ebcd4
commit 782d074bdf
6 changed files with 66 additions and 63 deletions

View File

@ -148,39 +148,39 @@ func NewBot(config *config.Config, c *irc.Client) *Bot {
func (b *Bot) migrateDB() {
_, err := b.DB.Exec(`create table if not exists version (version integer);`)
if err != nil {
log.Fatal(err)
log.Fatal("Initial DB migration create version table: ", err)
}
var version int64
var version sql.NullInt64
err = b.DB.QueryRow("select max(version) from version").Scan(&version)
switch {
case err == sql.ErrNoRows:
if err != nil {
log.Fatal("Initial DB migration get version: ", err)
}
if version.Valid {
b.DBVersion = version.Int64
log.Printf("Database version: %v\n", b.DBVersion)
} else {
log.Printf("No versions, we're the first!.")
_, err := b.DB.Exec(`insert into version (version) values (1)`)
if err != nil {
log.Fatal(err)
log.Fatal("Initial DB migration insert: ", err)
}
case err != nil:
log.Fatal(err)
default:
b.DBVersion = version
log.Printf("Database version: %d\n", version)
}
if version == 1 {
if b.DBVersion == 1 {
if _, err := b.DB.Exec(`create table if not exists variables (
id integer primary key,
name string,
perms string,
type string
);`); err != nil {
log.Fatal(err)
log.Fatal("Initial DB migration create variables table: ", err)
}
if _, err := b.DB.Exec(`create table if not exists values (
if _, err := b.DB.Exec(`create table if not exists 'values' (
id integer primary key,
varId integer,
value string
);`); err != nil {
log.Fatal(err)
log.Fatal("Initial DB migration create values table: ", err)
}
}
}
@ -233,45 +233,6 @@ func (b *Bot) SendAction(channel, message string) {
b.SendMessage(channel, message)
}
// Handles incomming PRIVMSG requests
func (b *Bot) MsgRecieved(client *irc.Client, inMsg irc.Msg) {
if inMsg.User == "" {
return
}
msg := b.buildMessage(client, inMsg)
if strings.HasPrefix(msg.Body, "help") && msg.Command {
parts := strings.Fields(strings.ToLower(msg.Body))
b.checkHelp(msg.Channel, parts)
goto RET
}
for _, name := range b.PluginOrdering {
p := b.Plugins[name]
if p.Message(msg) {
break
}
}
RET:
b.logIn <- msg
return
}
func (b *Bot) EventRecieved(conn *irc.Client, inMsg irc.Msg) {
if inMsg.User == "" {
return
}
msg := b.buildMessage(conn, inMsg)
for _, name := range b.PluginOrdering {
p := b.Plugins[name]
if p.Event(inMsg.Cmd, msg) {
break
}
}
}
func (b *Bot) Who(channel string) []User {
out := []User{}
for _, u := range b.Users {

View File

@ -16,6 +16,48 @@ import (
"code.google.com/p/velour/irc"
)
// Handles incomming PRIVMSG requests
func (b *Bot) MsgRecieved(client *irc.Client, inMsg irc.Msg) {
log.Println("Recieved message: ", inMsg)
if inMsg.User == "" {
return
}
msg := b.buildMessage(client, inMsg)
if strings.HasPrefix(msg.Body, "help") && msg.Command {
parts := strings.Fields(strings.ToLower(msg.Body))
b.checkHelp(msg.Channel, parts)
goto RET
}
for _, name := range b.PluginOrdering {
p := b.Plugins[name]
if p.Message(msg) {
break
}
}
RET:
b.logIn <- msg
return
}
// Handle incoming events
func (b *Bot) EventRecieved(conn *irc.Client, inMsg irc.Msg) {
log.Println("Recieved event: ", inMsg)
if inMsg.User == "" {
return
}
msg := b.buildMessage(conn, inMsg)
for _, name := range b.PluginOrdering {
p := b.Plugins[name]
if p.Event(inMsg.Cmd, msg) {
break
}
}
}
// Interface used for compatibility with the Plugin interface
type Handler interface {
Message(message Message) bool
@ -201,7 +243,7 @@ func (b *Bot) getVar(varName string) (string, error) {
case err == sql.ErrNoRows:
return "", fmt.Errorf("No factoid found")
case err != nil:
log.Fatal(err)
log.Fatal("getVar error: ", err)
}
return text, nil
}

View File

@ -19,7 +19,7 @@ type User struct {
//bot *Bot
}
var users map[string]*User
var users = map[string]*User{}
func (b *Bot) GetUser(nick string) *User {
if _, ok := users[nick]; !ok {

View File

@ -57,17 +57,17 @@ func main() {
Bot = bot.NewBot(Config, Client)
// Bot.AddHandler(plugins.NewTestPlugin(Bot))
Bot.AddHandler("admin", plugins.NewAdminPlugin(Bot))
Bot.AddHandler("first", plugins.NewFirstPlugin(Bot))
Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot))
// Bot.AddHandler("first", plugins.NewFirstPlugin(Bot))
// Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot))
Bot.AddHandler("talker", plugins.NewTalkerPlugin(Bot))
Bot.AddHandler("dice", plugins.NewDicePlugin(Bot))
Bot.AddHandler("beers", plugins.NewBeersPlugin(Bot))
Bot.AddHandler("counter", plugins.NewCounterPlugin(Bot))
Bot.AddHandler("remember", plugins.NewRememberPlugin(Bot))
// Bot.AddHandler("remember", plugins.NewRememberPlugin(Bot))
Bot.AddHandler("skeleton", plugins.NewSkeletonPlugin(Bot))
Bot.AddHandler("your", plugins.NewYourPlugin(Bot))
// catches anything left, will always return true
Bot.AddHandler("factoid", plugins.NewFactoidPlugin(Bot))
// Bot.AddHandler("factoid", plugins.NewFactoidPlugin(Bot))
handleConnection()

View File

@ -72,7 +72,7 @@ func (p *AdminPlugin) handleVariables(message bot.Message) bool {
return true
case err != nil:
p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.")
log.Println(err)
log.Println("Admin error: ", err)
return true
}
p.Bot.SendMessage(message.Channel, "I've already got that one.")

View File

@ -101,7 +101,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool {
subject := strings.ToLower(nick)
itemName := strings.ToLower(parts[1])
if _, err := p.DB.Exec(`delete from counters where nick = ? and item = ?`, subject, itemName); err != nil {
if _, err := p.DB.Exec(`delete from counter where nick = ? and item = ?`, subject, itemName); err != nil {
p.Bot.SendMessage(channel, "Something went wrong removing that counter;")
return true
}
@ -126,7 +126,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool {
}
var item Item
err := p.DB.QueryRow(`select nick, item, count from counters
err := p.DB.QueryRow(`select nick, item, count from counter
where nick = ? and item = ?`, subject, itemName).Scan(
&item.Nick, &item.Item, &item.Count,
)
@ -184,7 +184,7 @@ func (p *CounterPlugin) update(subject, itemName string, delta int) Item {
switch {
case err == sql.ErrNoRows:
// insert it
res, err := p.DB.Exec(`insert into counter (nick, item, count)
res, err := p.DB.Exec(`insert into counter (nick, item, count)
values (?, ?, ?)`, subject, itemName, delta)
if err != nil {
log.Println(err)