-
Notifications
You must be signed in to change notification settings - Fork 0
/
bind.go
64 lines (58 loc) · 1.39 KB
/
bind.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"strings"
"go.etcd.io/bbolt"
"rkfg.me/ns2query/db"
)
func bind(player string, name string) (id uint32, err error) {
id, err = playerIDFromSteamID(player)
if err != nil {
return
}
err = putBind(id, name)
return
}
func putBind(playerID uint32, name string) (err error) {
return bdb.Update(func(t *bbolt.Tx) (err error) {
err = db.NewUsersBucket(t).PutValue(name, playerID)
if err != nil {
return
}
steamBucket := db.NewSteamToDiscordBucket(t)
err = steamBucket.PutValue(playerID, name)
if err != nil {
return
}
return db.NewLowercaseBucket(t).PutValue(name, name)
})
}
func getBind(username string) (playerID uint32, err error) {
err = bdb.View(func(t *bbolt.Tx) (err error) {
playerID, err = db.NewUsersBucket(t).GetValue(username)
return
})
if err != nil {
return 0, fmt.Errorf("player %s isn't in the database. Use `-bind <Steam ID>` to register", username)
}
return
}
func deleteBind(name string) (err error) {
return bdb.Update(func(t *bbolt.Tx) (err error) {
userBucket := db.NewUsersBucket(t)
steamBucket := db.NewSteamToDiscordBucket(t)
steamId, _ := userBucket.GetValue(name)
err = userBucket.DeleteValue(name)
if err != nil {
return
}
if steamId > 0 {
err = steamBucket.DeleteValue(steamId)
if err != nil {
return
}
}
err = db.NewLowercaseBucket(t).DeleteValue(strings.ToLower(name))
return
})
}