mirror of https://github.com/velour/catbase.git
Get the bot online; make counters work
Mostly just silly stuff to get everything working.
This commit is contained in:
parent
1efa7ebcd4
commit
782d074bdf
67
bot/bot.go
67
bot/bot.go
|
@ -148,39 +148,39 @@ func NewBot(config *config.Config, c *irc.Client) *Bot {
|
||||||
func (b *Bot) migrateDB() {
|
func (b *Bot) migrateDB() {
|
||||||
_, err := b.DB.Exec(`create table if not exists version (version integer);`)
|
_, err := b.DB.Exec(`create table if not exists version (version integer);`)
|
||||||
if err != nil {
|
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)
|
err = b.DB.QueryRow("select max(version) from version").Scan(&version)
|
||||||
switch {
|
if err != nil {
|
||||||
case err == sql.ErrNoRows:
|
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!.")
|
log.Printf("No versions, we're the first!.")
|
||||||
_, err := b.DB.Exec(`insert into version (version) values (1)`)
|
_, err := b.DB.Exec(`insert into version (version) values (1)`)
|
||||||
if err != nil {
|
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 (
|
if _, err := b.DB.Exec(`create table if not exists variables (
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
name string,
|
name string,
|
||||||
perms string,
|
perms string,
|
||||||
type string
|
type string
|
||||||
);`); err != nil {
|
);`); 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,
|
id integer primary key,
|
||||||
varId integer,
|
varId integer,
|
||||||
value string
|
value string
|
||||||
);`); err != nil {
|
);`); 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)
|
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 {
|
func (b *Bot) Who(channel string) []User {
|
||||||
out := []User{}
|
out := []User{}
|
||||||
for _, u := range b.Users {
|
for _, u := range b.Users {
|
||||||
|
|
|
@ -16,6 +16,48 @@ import (
|
||||||
"code.google.com/p/velour/irc"
|
"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
|
// Interface used for compatibility with the Plugin interface
|
||||||
type Handler interface {
|
type Handler interface {
|
||||||
Message(message Message) bool
|
Message(message Message) bool
|
||||||
|
@ -201,7 +243,7 @@ func (b *Bot) getVar(varName string) (string, error) {
|
||||||
case err == sql.ErrNoRows:
|
case err == sql.ErrNoRows:
|
||||||
return "", fmt.Errorf("No factoid found")
|
return "", fmt.Errorf("No factoid found")
|
||||||
case err != nil:
|
case err != nil:
|
||||||
log.Fatal(err)
|
log.Fatal("getVar error: ", err)
|
||||||
}
|
}
|
||||||
return text, nil
|
return text, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ type User struct {
|
||||||
//bot *Bot
|
//bot *Bot
|
||||||
}
|
}
|
||||||
|
|
||||||
var users map[string]*User
|
var users = map[string]*User{}
|
||||||
|
|
||||||
func (b *Bot) GetUser(nick string) *User {
|
func (b *Bot) GetUser(nick string) *User {
|
||||||
if _, ok := users[nick]; !ok {
|
if _, ok := users[nick]; !ok {
|
||||||
|
|
8
main.go
8
main.go
|
@ -57,17 +57,17 @@ func main() {
|
||||||
Bot = bot.NewBot(Config, Client)
|
Bot = bot.NewBot(Config, Client)
|
||||||
// Bot.AddHandler(plugins.NewTestPlugin(Bot))
|
// Bot.AddHandler(plugins.NewTestPlugin(Bot))
|
||||||
Bot.AddHandler("admin", plugins.NewAdminPlugin(Bot))
|
Bot.AddHandler("admin", plugins.NewAdminPlugin(Bot))
|
||||||
Bot.AddHandler("first", plugins.NewFirstPlugin(Bot))
|
// Bot.AddHandler("first", plugins.NewFirstPlugin(Bot))
|
||||||
Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot))
|
// Bot.AddHandler("downtime", plugins.NewDowntimePlugin(Bot))
|
||||||
Bot.AddHandler("talker", plugins.NewTalkerPlugin(Bot))
|
Bot.AddHandler("talker", plugins.NewTalkerPlugin(Bot))
|
||||||
Bot.AddHandler("dice", plugins.NewDicePlugin(Bot))
|
Bot.AddHandler("dice", plugins.NewDicePlugin(Bot))
|
||||||
Bot.AddHandler("beers", plugins.NewBeersPlugin(Bot))
|
Bot.AddHandler("beers", plugins.NewBeersPlugin(Bot))
|
||||||
Bot.AddHandler("counter", plugins.NewCounterPlugin(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("skeleton", plugins.NewSkeletonPlugin(Bot))
|
||||||
Bot.AddHandler("your", plugins.NewYourPlugin(Bot))
|
Bot.AddHandler("your", plugins.NewYourPlugin(Bot))
|
||||||
// catches anything left, will always return true
|
// catches anything left, will always return true
|
||||||
Bot.AddHandler("factoid", plugins.NewFactoidPlugin(Bot))
|
// Bot.AddHandler("factoid", plugins.NewFactoidPlugin(Bot))
|
||||||
|
|
||||||
handleConnection()
|
handleConnection()
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ func (p *AdminPlugin) handleVariables(message bot.Message) bool {
|
||||||
return true
|
return true
|
||||||
case err != nil:
|
case err != nil:
|
||||||
p.Bot.SendMessage(message.Channel, "I'm broke and need attention in my variable creation code.")
|
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
|
return true
|
||||||
}
|
}
|
||||||
p.Bot.SendMessage(message.Channel, "I've already got that one.")
|
p.Bot.SendMessage(message.Channel, "I've already got that one.")
|
||||||
|
|
|
@ -101,7 +101,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool {
|
||||||
subject := strings.ToLower(nick)
|
subject := strings.ToLower(nick)
|
||||||
itemName := strings.ToLower(parts[1])
|
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;")
|
p.Bot.SendMessage(channel, "Something went wrong removing that counter;")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
@ -126,7 +126,7 @@ func (p *CounterPlugin) Message(message bot.Message) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
var item Item
|
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(
|
where nick = ? and item = ?`, subject, itemName).Scan(
|
||||||
&item.Nick, &item.Item, &item.Count,
|
&item.Nick, &item.Item, &item.Count,
|
||||||
)
|
)
|
||||||
|
@ -184,7 +184,7 @@ func (p *CounterPlugin) update(subject, itemName string, delta int) Item {
|
||||||
switch {
|
switch {
|
||||||
case err == sql.ErrNoRows:
|
case err == sql.ErrNoRows:
|
||||||
// insert it
|
// 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)
|
values (?, ?, ?)`, subject, itemName, delta)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
|
Loading…
Reference in New Issue