mirror of https://github.com/velour/catbase.git
Fix beer plugin bugs
This commit is contained in:
parent
25f32918b1
commit
b345506e7f
|
@ -48,7 +48,7 @@ func NewBeersPlugin(bot *bot.Bot) *BeersPlugin {
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
nick string,
|
nick string,
|
||||||
count integer,
|
count integer,
|
||||||
lastDrunk integer
|
lastDrunk datetime
|
||||||
);`); err != nil {
|
);`); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ func NewBeersPlugin(bot *bot.Bot) *BeersPlugin {
|
||||||
id integer primary key,
|
id integer primary key,
|
||||||
untappdUser string
|
untappdUser string
|
||||||
channel string
|
channel string
|
||||||
lastCheckin integer
|
lastCheckin datetime
|
||||||
chanNick string
|
chanNick string
|
||||||
);`); err != nil {
|
);`); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -77,9 +77,9 @@ func NewBeersPlugin(bot *bot.Bot) *BeersPlugin {
|
||||||
func (u *userBeers) Save(db *sql.DB) error {
|
func (u *userBeers) Save(db *sql.DB) error {
|
||||||
if !u.saved {
|
if !u.saved {
|
||||||
res, err := db.Exec(`insert into beers (
|
res, err := db.Exec(`insert into beers (
|
||||||
nick string,
|
nick,
|
||||||
count integer,
|
count,
|
||||||
lastDrunk integer
|
lastDrunk
|
||||||
) values (?, ?, ?)`, u.nick, u.count, u.lastDrunk)
|
) values (?, ?, ?)`, u.nick, u.count, u.lastDrunk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -89,12 +89,21 @@ func (u *userBeers) Save(db *sql.DB) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
u.id = id
|
u.id = id
|
||||||
|
} else {
|
||||||
|
_, err := db.Exec(`update beers set
|
||||||
|
count = ?,
|
||||||
|
lastDrunk = ?
|
||||||
|
where id = ?;`, u.count, time.Now(), u.id)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error updating beers: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUserBeers(db *sql.DB, nick string) *userBeers {
|
func getUserBeers(db *sql.DB, nick string) *userBeers {
|
||||||
var ub userBeers
|
ub := userBeers{saved: true}
|
||||||
err := db.QueryRow(`select id, nick, count, lastDrunk from beers
|
err := db.QueryRow(`select id, nick, count, lastDrunk from beers
|
||||||
where nick = ?`, nick).Scan(
|
where nick = ?`, nick).Scan(
|
||||||
&ub.id,
|
&ub.id,
|
||||||
|
@ -102,8 +111,15 @@ func getUserBeers(db *sql.DB, nick string) *userBeers {
|
||||||
&ub.count,
|
&ub.count,
|
||||||
&ub.lastDrunk,
|
&ub.lastDrunk,
|
||||||
)
|
)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
log.Println("DIDN'T FIND THAT USER: ", err)
|
||||||
|
return &userBeers{
|
||||||
|
nick: nick,
|
||||||
|
count: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil && err != sql.ErrNoRows {
|
||||||
log.Println(err)
|
log.Println("Error finding beers: ", err)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,6 +156,7 @@ func (p *BeersPlugin) Message(message bot.Message) bool {
|
||||||
// you can't be negative
|
// you can't be negative
|
||||||
msg := fmt.Sprintf("Sorry %s, you can't have negative beers!", nick)
|
msg := fmt.Sprintf("Sorry %s, you can't have negative beers!", nick)
|
||||||
p.Bot.SendMessage(channel, msg)
|
p.Bot.SendMessage(channel, msg)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
if parts[1] == "+=" {
|
if parts[1] == "+=" {
|
||||||
p.setBeers(nick, p.getBeers(nick)+count)
|
p.setBeers(nick, p.getBeers(nick)+count)
|
||||||
|
@ -272,7 +289,9 @@ func (p *BeersPlugin) setBeers(user string, amount int) {
|
||||||
ub := getUserBeers(p.db, user)
|
ub := getUserBeers(p.db, user)
|
||||||
ub.count = amount
|
ub.count = amount
|
||||||
ub.lastDrunk = time.Now()
|
ub.lastDrunk = time.Now()
|
||||||
ub.Save(p.db)
|
if err := ub.Save(p.db); err != nil {
|
||||||
|
log.Println("Error saving beers: ", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BeersPlugin) addBeers(user string) {
|
func (p *BeersPlugin) addBeers(user string) {
|
||||||
|
|
Loading…
Reference in New Issue