-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
db.go
155 lines (137 loc) · 5.18 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package db
import (
"context"
"time"
log "github.com/sirupsen/logrus"
"github.com/divideprojects/Alita_Robot/alita/config"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Button struct {
Name string `bson:"name,omitempty" json:"name,omitempty"`
Url string `bson:"url,omitempty" json:"url,omitempty"`
SameLine bool `bson:"btn_sameline" json:"btn_sameline" default:"false"`
}
const (
// TEXT types of senders
TEXT int = 1
STICKER int = 2
DOCUMENT int = 3
PHOTO int = 4
AUDIO int = 5
VOICE int = 6
VIDEO int = 7
VideoNote int = 8
)
var (
// Contexts
tdCtx = context.TODO()
bgCtx = context.Background()
// define collections
adminSettingsColl *mongo.Collection
blacklistsColl *mongo.Collection
pinColl *mongo.Collection
userColl *mongo.Collection
reportChatColl *mongo.Collection
reportUserColl *mongo.Collection
devsColl *mongo.Collection
chatColl *mongo.Collection
channelColl *mongo.Collection
antifloodSettingsColl *mongo.Collection
connectionColl *mongo.Collection
connectionSettingsColl *mongo.Collection
disableColl *mongo.Collection
rulesColl *mongo.Collection
warnSettingsColl *mongo.Collection
warnUsersColl *mongo.Collection
greetingsColl *mongo.Collection
lockColl *mongo.Collection
filterColl *mongo.Collection
notesColl *mongo.Collection
notesSettingsColl *mongo.Collection
)
// dbInstance func
func init() {
mongoClient, err := mongo.NewClient(
options.Client().ApplyURI(config.DatabaseURI),
)
if err != nil {
log.Errorf("[Database][Client]: %v", err)
}
ctx, cancel := context.WithTimeout(bgCtx, 10*time.Second)
defer cancel()
err = mongoClient.Connect(ctx)
if err != nil {
log.Errorf("[Database][Connect]: %v", err)
}
// Open Connections to Collections
log.Info("Opening Database Collections...")
adminSettingsColl = mongoClient.Database(config.MainDbName).Collection("admin")
blacklistsColl = mongoClient.Database(config.MainDbName).Collection("blacklists")
pinColl = mongoClient.Database(config.MainDbName).Collection("pins")
userColl = mongoClient.Database(config.MainDbName).Collection("users")
reportChatColl = mongoClient.Database(config.MainDbName).Collection("report_chat_settings")
reportUserColl = mongoClient.Database(config.MainDbName).Collection("report_user_settings")
devsColl = mongoClient.Database(config.MainDbName).Collection("devs")
chatColl = mongoClient.Database(config.MainDbName).Collection("chats")
channelColl = mongoClient.Database(config.MainDbName).Collection("channels")
antifloodSettingsColl = mongoClient.Database(config.MainDbName).Collection("antiflood_settings")
connectionColl = mongoClient.Database(config.MainDbName).Collection("connection")
connectionSettingsColl = mongoClient.Database(config.MainDbName).Collection("connection_settings")
disableColl = mongoClient.Database(config.MainDbName).Collection("disable")
rulesColl = mongoClient.Database(config.MainDbName).Collection("rules")
warnSettingsColl = mongoClient.Database(config.MainDbName).Collection("warns_settings")
warnUsersColl = mongoClient.Database(config.MainDbName).Collection("warns_users")
greetingsColl = mongoClient.Database(config.MainDbName).Collection("greetings")
lockColl = mongoClient.Database(config.MainDbName).Collection("locks")
filterColl = mongoClient.Database(config.MainDbName).Collection("filters")
notesColl = mongoClient.Database(config.MainDbName).Collection("notes")
notesSettingsColl = mongoClient.Database(config.MainDbName).Collection("notes_settings")
log.Info("Done opening all database collections!")
}
// updateOne func to update one document
func updateOne(collecion *mongo.Collection, filter bson.M, data interface{}) (err error) {
_, err = collecion.UpdateOne(tdCtx, filter, bson.M{"$set": data}, options.Update().SetUpsert(true))
if err != nil {
log.Errorf("[Database][updateOne]: %v", err)
}
return
}
// findOne func to find one document
func findOne(collecion *mongo.Collection, filter bson.M) (res *mongo.SingleResult) {
res = collecion.FindOne(tdCtx, filter)
return
}
// countDocs func to count documents
func countDocs(collecion *mongo.Collection, filter bson.M) (count int64, err error) {
count, err = collecion.CountDocuments(tdCtx, filter)
if err != nil {
log.Errorf("[Database][countDocs]: %v", err)
}
return
}
// findAll func to find all documents
func findAll(collecion *mongo.Collection, filter bson.M) (cur *mongo.Cursor) {
cur, err := collecion.Find(tdCtx, filter)
if err != nil {
log.Errorf("[Database][findAll]: %v", err)
}
return
}
// deleteOne func to delete one document
func deleteOne(collecion *mongo.Collection, filter bson.M) (err error) {
_, err = collecion.DeleteOne(tdCtx, filter)
if err != nil {
log.Errorf("[Database][deleteOne]: %v", err)
}
return
}
// deleteMany func to delete many documents
func deleteMany(collecion *mongo.Collection, filter bson.M) (err error) {
_, err = collecion.DeleteMany(tdCtx, filter)
if err != nil {
log.Errorf("[Database][deleteMany]: %v", err)
}
return
}