-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbmanager.js
151 lines (123 loc) · 4.29 KB
/
dbmanager.js
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
const Token = require('../models/token')
const Muncipio = require('../models/municipio')
const Channel = require('../models/channel')
const Birthday = require('../models/birthday')
const Ban = require('../models/ban')
const Screenshot = require('../models/screenshot')
const tempsDeFlors = require('../models/tempsDeFlors')
const logConn = require('../db.openai')
function getToken (userId) {
return Token.findOne({userId: userId})
}
function updateToken (userId, update) {
return Token.updateOne({userId: userId}, update)
}
function getChannel (name) {
return Channel.findOne({name: name})
}
function updateChannel (name, update) {
return Channel.findOneAndUpdate({name: name}, {...update })
}
async function getMuncipioStartsWith (name) {
return Muncipio.findOne({nombre: { "$regex": "^" + name , "$options": "i"}}).lean()
}
async function getMuncipioEndsWith (name) {
return Muncipio.findOne({nombre: { "$regex": name + "$", "$options": "i"}}).lean()
}
async function getMuncipioContains (name) {
return Muncipio.findOne({nombre: { "$regex": name, "$options": "i"}}).lean()
}
async function getMunicipio (name) {
return Muncipio.findOne({nombre: { "$regex": "^" + name + "$", "$options": "i"}}).lean()
}
async function getBirthday (nick) {
return Birthday.findOne({nick: nick}).lean()
}
async function updateBirthday (nick, update) {
return Birthday.findOneAndUpdate({nick: nick}, update, {
new: true,
upsert: true
}).lean()
}
async function getBirthdayFromDate(day, month) {
return Birthday.find({day: day, month: month}).lean()
}
async function updateScreenshot (name, update) {
return Screenshot.findOneAndUpdate({name: name}, update, {
new: true,
upsert: true
}).lean()
}
async function getScreenshots(streamId) {
return Screenshot.find({streamId: streamId}).lean()
}
async function getTFSpot(roomId, number){
return tempsDeFlors.findOne({number: number, roomId: parseInt(roomId)}).lean()
}
async function setTFSpot(roomId, number, update, returnOldDoc){
const options = !returnOldDoc ? {returnNewDocument: true, returnDocument: 'after'} : {}
return tempsDeFlors.findOneAndUpdate({roomId: parseInt(roomId), number: number}, {...update }, options).lean()
}
async function getTFSpots(roomId){
return tempsDeFlors.find({roomId: parseInt(roomId)}).sort('number').lean()
}
async function addChatLogLine (roomId, nick, text, date) {
const conn = await logConn.getConnection()
return conn.model('chatLog').insertMany({roomId: roomId, nick: nick, text: text, date: date})
}
async function getChatLogLines (roomId, startOfDay, endOfDay) {
const conn = await logConn.getConnection()
return conn.model('chatLog')
.find({roomId: roomId, date: { $gte: startOfDay, $lt: endOfDay }})
.select('nick text date -_id').lean()
}
async function addTitleLogLine (roomId, title, date) {
const conn = await logConn.getConnection()
return conn.model('titleLog').insertMany({roomId: roomId, title: title, date: date})
}
async function addBan (roomId, userName, moderatorName, reason, creationDate, expiryDate) {
return Ban.insertMany({roomId: parseInt(roomId), userName: userName, moderatorName: moderatorName,
reason: reason, creationDate: creationDate, expiryDate: expiryDate})
}
async function removeBan (roomId, userName) {
return Ban.deleteOne({roomId: parseInt(roomId), userName: userName})
}
async function addBans (bans) {
return Ban.insertMany(bans)
}
async function clearBans (roomId) {
return Ban.deleteMany({roomId: parseInt(roomId)});
}
async function getPermanentBans(roomId) {
return Ban.find({roomId: parseInt(roomId), expiryDate: null,moderatorName: { $ne: "sery_bot" }}).lean()
}
async function getTimeouts(roomId) {
return Ban.find({roomId: parseInt(roomId), expiryDate: { $ne: null }}).lean()
}
module.exports = {
getToken,
updateToken,
getMunicipio,
getMuncipioStartsWith,
getMuncipioEndsWith,
getMuncipioContains,
getChannel,
updateChannel,
updateBirthday,
getBirthday,
getBirthdayFromDate,
updateScreenshot,
getScreenshots,
getTFSpot,
getTFSpots,
setTFSpot,
addTitleLogLine,
addChatLogLine,
addBan,
clearBans,
addBans,
getPermanentBans,
getTimeouts,
removeBan,
getChatLogLines
}