plugins/listenbrainz/db.go (view raw)
1package listenbrainz
2
3import (
4 "fmt"
5
6 "git.icyphox.sh/taigobot/db"
7 "github.com/dgraph-io/badger/v3"
8)
9
10// Store the Listenbrainz username against the nick.
11func Setup(lbzUser, nick string) error {
12 err := db.DB.Update(func(txn *badger.Txn) error {
13 err := txn.Set([]byte(fmt.Sprintf("lbz/%s", nick)), []byte(lbzUser))
14 return err
15 })
16 if err != nil {
17 return err
18 }
19
20 return nil
21}
22
23// Gets the Listenbrainz username from the DB.
24func GetUser(nick string) (string, error) {
25 var user string
26 err := db.DB.View(func(txn *badger.Txn) error {
27 item, err := txn.Get([]byte(fmt.Sprintf("lbz/%s", nick)))
28 if err != nil {
29 return err
30 }
31
32 err = item.Value(func(val []byte) error {
33 user = string(val)
34 return nil
35 })
36 return nil
37 })
38
39 if err != nil {
40 return "", err
41 }
42 return user, nil
43}