catbase/plugins/beers/beers.go

472 lines
12 KiB
Go
Raw Normal View History

2016-01-17 18:00:44 +00:00
// © 2013 the CatBase Authors under the WTFPL. See AUTHORS for the list of authors.
package beers
import (
2012-12-01 23:01:03 +00:00
"encoding/json"
"errors"
"fmt"
2012-12-01 23:01:03 +00:00
"io/ioutil"
2012-08-25 04:49:48 +00:00
"math/rand"
2012-12-01 23:01:03 +00:00
"net/http"
"strconv"
"strings"
"time"
2014-04-20 19:24:45 +00:00
2016-03-19 18:02:46 +00:00
"github.com/jmoiron/sqlx"
2019-03-07 16:35:42 +00:00
"github.com/rs/zerolog/log"
2016-01-17 18:00:44 +00:00
"github.com/velour/catbase/bot"
"github.com/velour/catbase/bot/msg"
2016-03-19 18:27:02 +00:00
"github.com/velour/catbase/plugins/counter"
)
// This is a skeleton plugin to serve as an example and quick copy/paste for new plugins.
2018-01-05 21:54:12 +00:00
const itemName = ":beer:"
type BeersPlugin struct {
Bot bot.Bot
2016-03-19 18:02:46 +00:00
db *sqlx.DB
}
type untappdUser struct {
id int64
untappdUser string
channel string
lastCheckin int
chanNick string
}
// New BeersPlugin creates a new BeersPlugin with the Plugin interface
func New(b bot.Bot) *BeersPlugin {
if _, err := b.DB().Exec(`create table if not exists untappd (
id integer primary key,
2016-03-11 17:48:41 +00:00
untappdUser string,
channel string,
lastCheckin integer,
chanNick string
);`); err != nil {
2019-03-07 16:35:42 +00:00
log.Fatal().Err(err)
}
2019-02-15 20:12:09 +00:00
p := &BeersPlugin{
Bot: b,
db: b.DB(),
}
for _, channel := range b.Config().GetArray("Untappd.Channels", []string{}) {
2019-05-27 23:21:53 +00:00
go p.untappdLoop(b.DefaultConnector(), channel)
2012-12-01 23:01:03 +00:00
}
b.Register(p, bot.Message, p.message)
b.Register(p, bot.Help, p.help)
2019-02-15 20:12:09 +00:00
return p
}
// 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.
2019-05-27 23:21:53 +00:00
func (p *BeersPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
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
parts[0] = strings.ToLower(parts[0]) // support iPhone/Android saying "Beers"
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!
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, "Sorry, that didn't make any sense.")
}
if count < 0 {
// you can't be negative
msg := fmt.Sprintf("Sorry %s, you can't have negative beers!", nick)
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, msg)
2016-01-15 14:44:22 +00:00
return true
}
if parts[1] == "+=" {
2016-03-19 18:27:02 +00:00
p.addBeers(nick, count)
2019-05-27 23:21:53 +00:00
p.randomReply(c, channel)
} else if parts[1] == "=" {
if count == 0 {
2019-05-27 23:21:53 +00:00
p.puke(c, nick, channel)
} else {
2012-12-01 23:01:03 +00:00
p.setBeers(nick, count)
2019-05-27 23:21:53 +00:00
p.randomReply(c, channel)
}
} else {
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, "I don't know your math.")
}
} else if len(parts) == 2 {
if p.doIKnow(parts[1]) {
2019-05-27 23:21:53 +00:00
p.reportCount(c, parts[1], channel, false)
} else {
msg := fmt.Sprintf("Sorry, I don't know %s.", parts[1])
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, msg)
}
} else if len(parts) == 1 {
2019-05-27 23:21:53 +00:00
p.reportCount(c, nick, channel, true)
}
// no matter what, if we're in here, then we've responded
return true
} else if parts[0] == "puke" {
2019-05-27 23:21:53 +00:00
p.puke(c, nick, channel)
return true
}
if message.Command && parts[0] == "imbibe" {
2016-03-19 18:27:02 +00:00
p.addBeers(nick, 1)
2019-05-27 23:21:53 +00:00
p.randomReply(c, channel)
return true
}
2012-12-01 23:01:03 +00:00
if message.Command && parts[0] == "reguntappd" {
chanNick := message.User.Name
channel := message.Channel
2012-12-01 23:01:03 +00:00
if len(parts) < 2 {
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, "You must also provide a user name.")
} else if len(parts) == 3 {
chanNick = parts[2]
} else if len(parts) == 4 {
chanNick = parts[2]
channel = parts[3]
2012-12-01 23:01:03 +00:00
}
u := untappdUser{
untappdUser: parts[1],
chanNick: chanNick,
channel: channel,
2012-12-01 23:01:03 +00:00
}
2019-03-07 16:35:42 +00:00
log.Info().
Str("untappdUser", u.untappdUser).
Str("nick", u.chanNick).
Msg("Creating Untappd user")
var count int
2016-01-15 14:44:22 +00:00
err := p.db.QueryRow(`select count(*) from untappd
where untappdUser = ?`, u.untappdUser).Scan(&count)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msgf("Error registering untappd")
}
if count > 0 {
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, "I'm already watching you.")
return true
}
_, err = p.db.Exec(`insert into untappd (
untappdUser,
channel,
lastCheckin,
chanNick
2016-03-11 17:48:41 +00:00
) values (?, ?, ?, ?);`,
u.untappdUser,
u.channel,
0,
u.chanNick,
)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msgf("Error registering untappd")
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, "I can't see.")
return true
}
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, "I'll be watching you.")
2019-05-27 23:21:53 +00:00
p.checkUntappd(c, channel)
2012-12-01 23:01:03 +00:00
return true
}
if message.Command && parts[0] == "checkuntappd" {
2019-03-07 16:35:42 +00:00
log.Info().
Str("user", message.User.Name).
Msgf("Checking untappd at request of user.")
2019-05-27 23:21:53 +00:00
p.checkUntappd(c, channel)
2012-12-01 23:01:03 +00:00
return true
}
return false
}
// Help responds to help requests. Every plugin must implement a help function.
2019-05-27 23:21:53 +00:00
func (p *BeersPlugin) help(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
msg := "Beers: imbibe by using either beers +=,=,++ or with the !imbibe/drink " +
"commands. I'll keep a count of how many beers you've had and then if you want " +
"to reset, just !puke it all up!"
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, message.Channel, msg)
return true
}
2016-03-19 18:27:02 +00:00
func getUserBeers(db *sqlx.DB, user string) counter.Item {
2018-01-05 21:54:12 +00:00
booze, _ := counter.GetItem(db, user, itemName)
2016-03-19 18:27:02 +00:00
return booze
}
2012-12-01 23:01:03 +00:00
func (p *BeersPlugin) setBeers(user string, amount int) {
ub := getUserBeers(p.db, user)
2016-03-19 18:27:02 +00:00
err := ub.Update(amount)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msgf("Error saving beers")
2016-01-15 14:44:22 +00:00
}
}
2016-03-19 18:27:02 +00:00
func (p *BeersPlugin) addBeers(user string, delta int) {
ub := getUserBeers(p.db, user)
err := ub.UpdateDelta(delta)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msgf("Error saving beers")
2016-03-19 18:27:02 +00:00
}
}
func (p *BeersPlugin) getBeers(nick string) int {
ub := getUserBeers(p.db, nick)
2016-03-19 18:27:02 +00:00
return ub.Count
}
2019-05-27 23:21:53 +00:00
func (p *BeersPlugin) reportCount(c bot.Connector, nick, channel string, himself bool) {
beers := p.getBeers(nick)
msg := fmt.Sprintf("%s has had %d beers so far.", nick, beers)
if himself {
if beers == 0 {
msg = fmt.Sprintf("You really need to get drinkin, %s!", nick)
} else {
msg = fmt.Sprintf("You've had %d beers so far, %s.", beers, nick)
}
}
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, msg)
}
2019-05-27 23:21:53 +00:00
func (p *BeersPlugin) puke(c bot.Connector, user string, channel string) {
p.setBeers(user, 0)
2012-12-01 23:01:03 +00:00
msg := fmt.Sprintf("Ohhhhhh, and a reversal of fortune for %s!", user)
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, msg)
}
func (p *BeersPlugin) doIKnow(nick string) bool {
var count int
err := p.db.QueryRow(`select count(*) from beers where nick = ?`, nick).Scan(&count)
if err != nil {
return false
}
return count > 0
}
// Sends random affirmation to the channel. This could be better (with a datastore for sayings)
2019-05-27 23:21:53 +00:00
func (p *BeersPlugin) randomReply(c bot.Connector, channel string) {
replies := []string{"ZIGGY! ZAGGY!", "HIC!", "Stay thirsty, my friend!"}
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, channel, replies[rand.Intn(len(replies))])
2012-08-25 04:49:48 +00:00
}
2012-12-01 23:01:03 +00:00
type checkin struct {
Checkin_id int
Created_at string
Checkin_comment string
Rating_score float64
Beer map[string]interface{}
Brewery map[string]interface{}
Venue interface{}
User mrUntappd
2016-05-01 20:56:58 +00:00
Media struct {
Count int
Items []struct {
Photo_id int
Photo struct {
Photo_img_sm string
Photo_img_md string
Photo_img_lg string
2016-05-01 20:56:58 +00:00
Photo_img_og string
}
}
}
Badges struct {
Count int
Items []struct {
BadgeName string `json:"badge_name"`
BadgeDescription string `json:"badge_description"`
BadgeImage string `json:"badge_image"`
}
}
}
type mrUntappd struct {
Uid int
User_name string
Relationship string
2012-12-01 23:01:03 +00:00
}
type checkins struct {
Count int
Items []checkin
}
type resp struct {
Checkins checkins
}
type Beers struct {
Response resp
}
func (p *BeersPlugin) pullUntappd() ([]checkin, error) {
2019-01-22 00:16:57 +00:00
token := p.Bot.Config().Get("Untappd.Token", "NONE")
if token == "NONE" {
return []checkin{}, fmt.Errorf("No untappd token")
}
access_token := "?access_token=" + token
2014-04-20 19:24:45 +00:00
baseUrl := "https://api.untappd.com/v4/checkin/recent/"
url := baseUrl + access_token + "&limit=25"
resp, err := http.Get(url)
2016-03-29 14:20:44 +00:00
if err != nil {
return []checkin{}, err
}
2016-03-11 17:48:41 +00:00
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2016-03-11 17:48:41 +00:00
return []checkin{}, err
}
2016-03-11 17:48:41 +00:00
if resp.StatusCode == 500 {
2019-03-07 16:35:42 +00:00
log.Error().Msgf("Error querying untappd: %s, %s", resp.Status, body)
return []checkin{}, errors.New(resp.Status)
}
var beers Beers
err = json.Unmarshal(body, &beers)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err)
2016-03-11 17:48:41 +00:00
return []checkin{}, err
}
return beers.Response.Checkins.Items, nil
2012-12-01 23:01:03 +00:00
}
2019-05-27 23:21:53 +00:00
func (p *BeersPlugin) checkUntappd(c bot.Connector, channel string) {
2019-01-22 00:16:57 +00:00
token := p.Bot.Config().Get("Untappd.Token", "NONE")
if token == "NONE" {
2019-03-07 16:35:42 +00:00
log.Info().
Msg(`Set config value "untappd.token" if you wish to enable untappd`)
2012-12-01 23:01:03 +00:00
return
}
2016-03-11 17:48:41 +00:00
userMap := make(map[string]untappdUser)
rows, err := p.db.Query(`select id, untappdUser, channel, lastCheckin, chanNick from untappd;`)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msg("Error getting untappd users")
return
}
for rows.Next() {
u := untappdUser{}
2016-03-11 17:48:41 +00:00
err := rows.Scan(&u.id, &u.untappdUser, &u.channel, &u.lastCheckin, &u.chanNick)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Fatal().Err(err)
2016-03-11 17:48:41 +00:00
}
userMap[u.untappdUser] = u
2016-03-11 17:48:41 +00:00
if u.chanNick == "" {
2019-03-07 16:35:42 +00:00
log.Fatal().Msg("Empty chanNick for no good reason.")
2016-03-11 17:48:41 +00:00
}
}
chks, err := p.pullUntappd()
2016-03-11 17:48:41 +00:00
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msg("Untappd ERROR")
return
2016-03-11 17:48:41 +00:00
}
for i := len(chks); i > 0; i-- {
checkin := chks[i-1]
if checkin.Checkin_id <= userMap[checkin.User.User_name].lastCheckin {
continue
}
2012-12-01 23:01:03 +00:00
venue := ""
switch v := checkin.Venue.(type) {
case map[string]interface{}:
venue = " at " + v["venue_name"].(string)
}
beerName := checkin.Beer["beer_name"].(string)
breweryName := checkin.Brewery["brewery_name"].(string)
user, ok := userMap[checkin.User.User_name]
if !ok {
continue
}
2019-03-07 16:35:42 +00:00
log.Debug().
Msgf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s",
user.chanNick, user.untappdUser, checkin.User.User_name)
2016-03-19 18:27:02 +00:00
p.addBeers(user.chanNick, 1)
drunken := p.getBeers(user.chanNick)
msg := fmt.Sprintf("%s just drank %s by %s%s, bringing his drunkeness to %d",
user.chanNick, beerName, breweryName, venue, drunken)
if checkin.Rating_score > 0 {
2018-12-25 02:59:30 +00:00
msg = fmt.Sprintf("%s. Rating: %.2f", msg, checkin.Rating_score)
}
if checkin.Checkin_comment != "" {
2018-12-25 02:59:30 +00:00
msg = fmt.Sprintf("%s -- %s",
msg, checkin.Checkin_comment)
}
2018-12-25 02:59:30 +00:00
args := []interface{}{
channel,
msg,
}
2016-05-01 20:56:58 +00:00
if checkin.Media.Count > 0 {
if strings.Contains(checkin.Media.Items[0].Photo.Photo_img_lg, "photos-processing") {
continue
}
args = append(args, bot.ImageAttachment{
URL: checkin.Media.Items[0].Photo.Photo_img_lg,
AltTxt: "Here's a photo",
})
2016-05-01 20:56:58 +00:00
}
if checkin.Badges.Count > 0 {
msg = msg + "\nThis checkin earned the following badge(s): "
for _, b := range checkin.Badges.Items {
msg = msg + b.BadgeName
args = append(args, bot.ImageAttachment{
URL: b.BadgeImage,
AltTxt: b.BadgeDescription,
})
}
}
2016-05-01 20:56:58 +00:00
user.lastCheckin = checkin.Checkin_id
_, err := p.db.Exec(`update untappd set
lastCheckin = ?
where id = ?`, user.lastCheckin, user.id)
if err != nil {
2019-03-07 16:35:42 +00:00
log.Error().Err(err).Msg("UPDATE ERROR!")
}
2012-12-01 23:01:03 +00:00
2019-03-07 16:35:42 +00:00
log.Debug().
Int("checkin_id", checkin.Checkin_id).
Str("msg", msg).
Msg("checkin")
2019-05-27 23:21:53 +00:00
p.Bot.Send(c, bot.Message, args...)
}
}
2012-12-01 23:01:03 +00:00
2019-05-27 23:21:53 +00:00
func (p *BeersPlugin) untappdLoop(c bot.Connector, channel string) {
2019-01-22 00:16:57 +00:00
frequency := p.Bot.Config().GetInt("Untappd.Freq", 120)
if frequency == 0 {
return
}
2012-12-01 23:01:03 +00:00
2019-03-07 16:35:42 +00:00
log.Info().Msgf("Checking every %v seconds", frequency)
for {
time.Sleep(time.Duration(frequency) * time.Second)
2019-05-27 23:21:53 +00:00
p.checkUntappd(c, channel)
2012-12-01 23:01:03 +00:00
}
}