beers: reorder checkin logic

This commit is contained in:
Chris Sexton 2020-06-17 14:24:34 -04:00 committed by Chris Sexton
parent 6556f7e100
commit 7b3179fe1d
3 changed files with 75 additions and 21 deletions

View File

@ -105,10 +105,14 @@ func New(config *config.Config, connector Connector) Bot {
return bot return bot
} }
// DefaultConnector is the main connector used for the bot
// If more than one connector is on, some users may not see all messages if this is used.
// Usage should be limited to out-of-band communications such as timed messages.
func (b *bot) DefaultConnector() Connector { func (b *bot) DefaultConnector() Connector {
return b.conn return b.conn
} }
// WhoAmI returns the bot's current registered name
func (b *bot) WhoAmI() string { func (b *bot) WhoAmI() string {
return b.me.Name return b.me.Name
} }
@ -149,6 +153,8 @@ func (b *bot) AddPlugin(h Plugin) {
b.pluginOrdering = append(b.pluginOrdering, name) b.pluginOrdering = append(b.pluginOrdering, name)
} }
// Who returns users for a channel the bot is in
// Check the particular connector for channel values
func (b *bot) Who(channel string) []user.User { func (b *bot) Who(channel string) []user.User {
names := b.conn.Who(channel) names := b.conn.Who(channel)
users := []user.User{} users := []user.User{}
@ -263,6 +269,8 @@ func (b *bot) RegisterWeb(root, name string) {
b.httpEndPoints = append(b.httpEndPoints, EndPoint{name, root}) b.httpEndPoints = append(b.httpEndPoints, EndPoint{name, root})
} }
// GetPassword returns a random password generated for the bot
// Passwords expire in 24h and are used for the web interface
func (b *bot) GetPassword() string { func (b *bot) GetPassword() string {
if b.passwordCreated.Before(time.Now().Add(-24 * time.Hour)) { if b.passwordCreated.Before(time.Now().Add(-24 * time.Hour)) {
adjs := b.config.GetArray("bot.passwordAdjectives", []string{"very"}) adjs := b.config.GetArray("bot.passwordAdjectives", []string{"very"})
@ -275,10 +283,12 @@ func (b *bot) GetPassword() string {
return b.password return b.password
} }
// SetQuiet is called to silence the bot from sending channel messages
func (b *bot) SetQuiet(status bool) { func (b *bot) SetQuiet(status bool) {
b.quiet = status b.quiet = status
} }
// RefreshPluginBlacklist loads data for which plugins are disabled for particular channels
func (b *bot) RefreshPluginBlacklist() error { func (b *bot) RefreshPluginBlacklist() error {
blacklistItems := []struct { blacklistItems := []struct {
Channel string Channel string
@ -295,6 +305,7 @@ func (b *bot) RefreshPluginBlacklist() error {
return nil return nil
} }
// GetPluginNames returns an ordered list of plugins loaded (used for blacklisting)
func (b *bot) GetPluginNames() []string { func (b *bot) GetPluginNames() []string {
names := []string{} names := []string{}
for _, name := range b.pluginOrdering { for _, name := range b.pluginOrdering {

View File

@ -44,50 +44,92 @@ type CallbackMap map[string]map[Kind][]Callback
type Bot interface { type Bot interface {
// Config allows access to the bot's configuration system // Config allows access to the bot's configuration system
Config() *config.Config Config() *config.Config
// DB gives access to the current database // DB gives access to the current database
DB() *sqlx.DB DB() *sqlx.DB
// Who lists users in a particular channel // Who lists users in a particular channel
// The channel should be represented as an ID for slack (check the connector for details)
Who(string) []user.User Who(string) []user.User
// WhoAmI gives a nick for the bot // WhoAmI gives a nick for the bot
WhoAmI() string WhoAmI() string
// AddPlugin registers a new plugin handler // AddPlugin registers a new plugin handler
AddPlugin(Plugin) AddPlugin(Plugin)
// First arg should be one of bot.Message/Reply/Action/etc
// Send transmits a message to a Connector.
// Kind is listed in the bot's enum, one of bot.Message/Reply/Action/etc
// Usually, the first vararg should be a channel ID, but refer to the Connector for info
Send(Connector, Kind, ...interface{}) (string, error) Send(Connector, Kind, ...interface{}) (string, error)
// First arg should be one of bot.Message/Reply/Action/etc
// Bot receives from a Connector.
// The Kind arg should be one of bot.Message/Reply/Action/etc
Receive(Connector, Kind, msg.Message, ...interface{}) bool Receive(Connector, Kind, msg.Message, ...interface{}) bool
// Register a callback
// Register a plugin callback
// Kind will be matched to the event for the callback
Register(Plugin, Kind, Callback) Register(Plugin, Kind, Callback)
// Filter prepares a message to be sent
// This loads the message with things like $variables
Filter(msg.Message, string) string Filter(msg.Message, string) string
// LastMessage returns the last message the bot has seen
LastMessage(string) (msg.Message, error) LastMessage(string) (msg.Message, error)
// CheckAdmin returns a user's admin status
CheckAdmin(string) bool CheckAdmin(string) bool
// GetEmojiList returns known emoji
GetEmojiList() map[string]string GetEmojiList() map[string]string
// RegisterFilter creates a filter function for message processing
RegisterFilter(string, func(string) string) RegisterFilter(string, func(string) string)
// RegisterWeb records a web endpoint for the UI
RegisterWeb(string, string) RegisterWeb(string, string)
// DefaultConnector returns the base connector, which may not be the only connector
DefaultConnector() Connector DefaultConnector() Connector
// GetWebNavigation returns the current known web endpoints
GetWebNavigation() []EndPoint GetWebNavigation() []EndPoint
// GetPassword generates a unique password for modification commands on the public website
GetPassword() string GetPassword() string
// SetQuiet toggles the bot's ability to send messages
SetQuiet(bool) SetQuiet(bool)
// GetPluginNames returns an ordered list of registered plugins
GetPluginNames() []string GetPluginNames() []string
// RefreshPluginBlacklist reloads the list of plugins disabled per room from the DB
RefreshPluginBlacklist() error RefreshPluginBlacklist() error
} }
// Connector represents a server connection to a chat service // Connector represents a server connection to a chat service
type Connector interface { type Connector interface {
// RegisterEvent creates a callback to watch Connector events
RegisterEvent(Callback) RegisterEvent(Callback)
// Send transmits a message on the connector
Send(Kind, ...interface{}) (string, error) Send(Kind, ...interface{}) (string, error)
// GetEmojiList returns a connector's known custom emoji
GetEmojiList() map[string]string GetEmojiList() map[string]string
// Serve starts a connector's connection routine
Serve() error Serve() error
// Who returns a user list for a channel
Who(string) []string Who(string) []string
// Profile returns a user's information given an ID
Profile(string) (user.User, error) Profile(string) (user.User, error)
} }
// Plugin interface used for compatibility with the Plugin interface // Plugin interface used for compatibility with the Plugin interface
// Uhh it turned empty, but we're still using it to ID plugins // Uhh it turned empty, but we're still using it to ID plugins
type Plugin interface { type Plugin interface{}
}

View File

@ -442,23 +442,8 @@ func (p *BeersPlugin) sendCheckin(c bot.Connector, channel string, user untappdU
log.Debug(). log.Debug().
Msgf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s", Msgf("user.chanNick: %s, user.untappdUser: %s, checkin.User.User_name: %s",
user.chanNick, user.untappdUser, checkin.User.User_name) user.chanNick, user.untappdUser, checkin.User.User_name)
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", args := []interface{}{}
user.chanNick, beerName, breweryName, venue, drunken)
if checkin.Rating_score > 0 {
msg = fmt.Sprintf("%s. Rating: %.2f", msg, checkin.Rating_score)
}
if checkin.Checkin_comment != "" {
msg = fmt.Sprintf("%s -- %s",
msg, checkin.Checkin_comment)
}
args := []interface{}{
channel,
msg,
}
if checkin.Badges.Count > 0 { if checkin.Badges.Count > 0 {
for _, b := range checkin.Badges.Items { for _, b := range checkin.Badges.Items {
args = append(args, bot.ImageAttachment{ args = append(args, bot.ImageAttachment{
@ -486,6 +471,22 @@ func (p *BeersPlugin) sendCheckin(c bot.Connector, channel string, user untappdU
delete(p.untapdCache, checkin.Checkin_id) delete(p.untapdCache, checkin.Checkin_id)
} }
// Don't add beers till after a photo has been detected (or failed once)
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 {
msg = fmt.Sprintf("%s. Rating: %.2f", msg, checkin.Rating_score)
}
if checkin.Checkin_comment != "" {
msg = fmt.Sprintf("%s -- %s",
msg, checkin.Checkin_comment)
}
args = append([]interface{}{channel, msg}, args...)
user.lastCheckin = checkin.Checkin_id user.lastCheckin = checkin.Checkin_id
_, err := p.db.Exec(`update untappd set _, err := p.db.Exec(`update untappd set
lastCheckin = ? lastCheckin = ?