/
wallet.go
44 lines (37 loc) · 1.02 KB
/
wallet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package bot
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/margostino/anfield/common"
"github.com/margostino/anfield/db"
"github.com/margostino/anfield/domain"
"log"
)
type Wallet struct {
Command string
Users *db.Collection
}
func (w Wallet) shouldReply(input string) bool {
return w.Command == input
}
func (w Wallet) reply(update *tgbotapi.Update) (interface{}, string, bufferEnabled) {
userId := update.Message.From.ID
wallet, err := w.getWallet(userId)
if err != nil {
return nil, "Wallet not found", false
}
reply := fmt.Sprintf("💰 Budget: $ %.2f.\n"+
"📅 Last updated: %s",
wallet.Budget,
common.UTC(wallet.LastUpdated))
return nil, reply, false
}
func (w Wallet) getWallet(userId int) (*domain.WalletDocument, error) {
var user domain.UserDocument
filter := db.FilterBy(string(userId))
err := w.Users.FindOne(filter, &user)
if err != nil {
log.Println(fmt.Sprintf("User %d search failed with error %s", userId, err.Error()))
}
return user.Wallet, err
}