-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
74 lines (60 loc) · 1.68 KB
/
database.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
68
69
70
71
72
73
74
package main
import (
"context"
"log"
"os"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
client *mongo.Client
collection *mongo.Collection
)
type ServerSetting struct {
GuildID string `bson:"guild_id"`
Settings map[string]bool `bson:"settings"`
}
func initDatabase() {
db := os.Getenv("MONGO_DB_URL")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
clientOptions := options.Client().ApplyURI(db)
var err error
client, err = mongo.Connect(ctx, clientOptions)
if err != nil {
log.Fatal(err)
}
err = client.Ping(ctx, nil)
if err != nil {
log.Fatal(err)
}
collection = client.Database("discord_bot").Collection("server_settings")
log.Println("Connected to MongoDB")
}
func getServerSettingFromDB(guildID, service string) bool {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
var result ServerSetting
err := collection.FindOne(ctx, bson.M{"guild_id": guildID}).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
return true
}
log.Printf("Error fetching server settings: %v", err)
return true
}
return !result.Settings[service]
}
func setServerSettingToDB(guildID, service string, enabled bool) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
filter := bson.M{"guild_id": guildID}
update := bson.M{"$set": bson.M{"settings." + service: !enabled}}
opts := options.Update().SetUpsert(true)
_, err := collection.UpdateOne(ctx, filter, update, opts)
if err != nil {
log.Printf("Error updating server settings: %v", err)
}
}