mirror of https://github.com/velour/catbase.git
bid: add title to !bid command response
This commit is contained in:
parent
f8ee4224a1
commit
ffde8e2492
|
@ -81,10 +81,10 @@ func (p *NewsBid) message(conn bot.Connector, k bot.Kind, message msg.Message, a
|
||||||
}
|
}
|
||||||
amount, _ := strconv.Atoi(parts[1])
|
amount, _ := strconv.Atoi(parts[1])
|
||||||
url := parts[2]
|
url := parts[2]
|
||||||
if err := p.ws.Bid(message.User.Name, amount, url); err != nil {
|
if bid, err := p.ws.Bid(message.User.Name, amount, url); err != nil {
|
||||||
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error placing bid: %s", err))
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Error placing bid: %s", err))
|
||||||
} else {
|
} else {
|
||||||
p.bot.Send(conn, bot.Message, ch, "Your bid has been placed.")
|
p.bot.Send(conn, bot.Message, ch, fmt.Sprintf("Your bid has been placed on %s", bid.Title))
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -278,33 +278,40 @@ func (w *Webshit) GetAllBalances() ([]Balance, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bid allows a user to place a bid on a particular story
|
// Bid allows a user to place a bid on a particular story
|
||||||
func (w *Webshit) Bid(user string, amount int, URL string) error {
|
func (w *Webshit) Bid(user string, amount int, URL string) (Bid, error) {
|
||||||
bal := w.GetBalance(user)
|
bal := w.GetBalance(user)
|
||||||
if bal < amount {
|
if bal < amount {
|
||||||
return fmt.Errorf("cannot bid more than balance, %d", bal)
|
return Bid{}, fmt.Errorf("cannot bid more than balance, %d", bal)
|
||||||
}
|
}
|
||||||
story, err := w.getStoryByURL(URL)
|
story, err := w.getStoryByURL(URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return Bid{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ts := time.Now().Unix()
|
||||||
|
|
||||||
tx := w.db.MustBegin()
|
tx := w.db.MustBegin()
|
||||||
_, err = tx.Exec(`insert into webshit_bids (user,title,url,bid,placed) values (?,?,?,?,?)`,
|
_, err = tx.Exec(`insert into webshit_bids (user,title,url,bid,placed) values (?,?,?,?,?)`,
|
||||||
user, story.Title, story.URL, amount, time.Now().Unix())
|
user, story.Title, story.URL, amount, ts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return Bid{}, err
|
||||||
}
|
}
|
||||||
q := `insert into webshit_balances (user,balance,score) values (?,?,0)
|
q := `insert into webshit_balances (user,balance,score) values (?,?,0)
|
||||||
on conflict(user) do update set balance=?`
|
on conflict(user) do update set balance=?`
|
||||||
_, err = tx.Exec(q, user, bal-amount, bal-amount)
|
_, err = tx.Exec(q, user, bal-amount, bal-amount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return Bid{}, err
|
||||||
}
|
}
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
|
||||||
return err
|
return Bid{
|
||||||
|
User: user,
|
||||||
|
Title: story.Title,
|
||||||
|
URL: story.URL,
|
||||||
|
Placed: ts,
|
||||||
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// getStoryByURL scrapes the URL for a title
|
// getStoryByURL scrapes the URL for a title
|
||||||
|
|
Loading…
Reference in New Issue