forked from AzuraCast/azurabot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
67 lines (57 loc) · 1.16 KB
/
db.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
65
66
67
package main
import (
"github.com/boltdb/bolt"
"time"
)
func OpenDB() (*bolt.DB, error) {
db, err := bolt.Open("azurabot.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
return nil, err
}
return db, nil
}
// CreateDB create a database file if it if was not exist
func CreateDB() error {
db, err := OpenDB()
if err != nil {
return err
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("ChannelDB"))
if err != nil {
return err
}
return nil
})
return err
}
// PutDB ignore o unignore a test channel
func PutDB(channelID, ignored string) error {
db, err := OpenDB()
if err != nil {
return err
}
defer db.Close()
db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("ChannelDB"))
err := b.Put([]byte(channelID), []byte(ignored))
return err
})
return err
}
// GetDB read if a text channel is ignored
func GetDB(channelID string) string {
var v []byte
db, err := OpenDB()
if err != nil {
return ""
}
defer db.Close()
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("ChannelDB"))
v = b.Get([]byte(channelID))
return nil
})
return string(v)
}