-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.js
386 lines (318 loc) · 9.81 KB
/
db.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('db.json')
const db = low(adapter)
const uuid = require('uuid/v4')
const isYesterday = require('date-fns/is_yesterday')
const isToday = require('date-fns/is_today')
const format = require('date-fns/format')
const topics = require('./utils/topics')
db.defaults({
users: [],
streaks: [],
channels: [],
configs: []
})
.write()
exports.addStreak = msg => {
const user = db.get('users')
.find({ userID: msg.author.id })
.value()
if(!user) {
db.get('users')
.push({
userID: msg.author.id,
streaks: [],
messagesEnabled: true,
mentionsEnabled: true
})
.write()
}
const streakID = uuid()
const topic = topics.getTopicOrChannel(msg)
db.get('streaks')
.push({
streakID,
userID: msg.author.id,
messageID: msg.id,
guildID: msg.guild.id,
topic,
date: new Date(),
content: msg.content.split('!streak ')[1]
})
.write()
const userStreaks = db.get('users')
.find({ userID: msg.author.id })
.get('streaks')
const streak = userStreaks.find({topic})
if(streak.value()) {
streak.assign({
guildID: msg.guild.id,
topic: streak.value().topic,
streakLevel: streak.value().streakLevel + 1,
bestStreak: Math.max(streak.value().streakLevel+1, streak.value().bestStreak)
})
.write()
console.log(`${msg.author.username} continued a streak for ${topic} to ${streak.value().streakLevel}`)
} else {
userStreaks.push({
guildID: msg.guild.id,
topic,
streakLevel: 1,
bestStreak: 1
})
.write()
console.log(`${msg.author.username} started a streak for ${topic}`)
}
}
exports.getUserStreak = (userID, guildID, msg) => {
const topic = topics.getTopicOrChannel(msg)
let streak = db.get('users')
.find({userID})
.get('streaks')
.find({guildID, topic})
.value()
return streak || null
}
exports.getTopStreaks = guildID => {
let highscores = []
const users = db.get('users').value()
users.forEach(user => {
highscores.push(...user.streaks.filter(streak => streak.guildID === guildID && streak.streakLevel > 0).map(streak => {
return {
...streak,
userID: user.userID
}
}))
})
// sort them by highest
highscores.sort((a, b) => {
return b.streakLevel - a.streakLevel
})
highscores = highscores.filter(score => {
const anotherScore = highscores.find(s => s.userID !== score.userID && s.topic === score.topic)
if(!anotherScore) return true
return anotherScore.streakLevel >= score.streakLevel
})
return highscores
}
// Consider adding guildID to tell the user which guild
exports.checkStreaks = guildMembers => {
const users = db.get('users').value()
const streaks = db.get('streaks').value()
users.forEach(user => {
user.streaks.forEach(async userStreak => {
let foundStreakFromYesterday = false
streaks.forEach(streak => {
if(streak.userID === user.userID && streak.topic === userStreak.topic && isYesterday(streak.date)) {
foundStreakFromYesterday = true
}
})
if(!foundStreakFromYesterday && userStreak.streakLevel > 0) {
console.log(`${user.userID}'s ${userStreak.topic} streak ended`)
// send a message to them about it
if(user.messagesEnabled) {
const guildMember = await guildMembers.fetch(user.userID)
guildMember.send(`Unfortunately you missed a day and your ${userStreak.streakLevel} day ${userStreak.topic} streak has ended. Use !streak ${userStreak.topic} to start a new one!`)
}
userStreak.streakLevel = 0
}
})
})
db.get('users')
.assign(users)
.write()
}
exports.hasStreakedToday = (guildID, userID, msg) => {
const streaks = db.get('streaks').value()
const topic = topics.getTopicOrChannel(msg)
return streaks.some(streak => {
if(streak.guildID === guildID && streak.userID === userID) {
return streak.topic === topic && isToday(streak.date)
}
return false
})
}
exports.hasUpdatedStreakToday = (guildID, userID) => {
const streaks = db.get('streaks').value()
return streaks.some(streak => {
if(streak.guildID === guildID && streak.userID === userID) {
return isToday(streak.date)
}
return false
})
}
exports.isValidChannel = (guildID, channelName) => {
return exports.getChannels(guildID).indexOf(channelName) !== -1
}
exports.getUserStreaks = userID => {
return db.get('users')
.find({userID})
.get('streaks')
.value()
}
exports.getUserActiveStreaks = userID => {
const streaks = exports.getUserStreaks(userID)
if (streaks) {
return streaks.filter(streak => streak.streakLevel > 0)
} else {
return []
}
}
exports.getStatCount = table => {
return db.get(table).value().length
}
exports.toggleDMs = msg => {
let user = db.get('users')
.find({userID: msg.author.id})
let userValue = user.value()
userValue.messagesEnabled = !userValue.messagesEnabled
user.assign(userValue)
.write()
console.log(`${userValue.userID}'s notifications are now set to ${userValue.messagesEnabled}`)
if(userValue.messagesEnabled) {
msg.reply('I will now message you about your streaks ending and time warnings. Use `!toggledm` to disable these messages')
} else {
msg.reply('I will no longer message you about your streaks ending or time warnings. Use `!toggledm` to re-enable these messages')
}
}
exports.getDMSettingForUser = userID => {
let user = db.get('users')
.find({userID})
.value()
return user ? user.messagesEnabled : false
}
exports.toggleMentions = msg => {
let user = db.get('users')
.find({userID: msg.author.id})
let userValue = user.value()
userValue.mentionsEnabled = !userValue.mentionsEnabled
user.assign(userValue)
.write()
console.log(`${userValue.userID}'s mentions are now set to ${userValue.mentionsEnabled}`)
if(userValue.mentionsEnabled) {
msg.reply('I will now mention you if your streak is a highscore. Use `!togglementions` to disable these messages')
} else {
msg.reply('I will no longer message you if your streak is a highscore. Use `!togglementions` to re-enable these messages')
}
}
exports.getMentionSettingForUser = userID => {
let user = db.get('users')
.find({userID})
.value()
return user ? user.mentionsEnabled : false
}
exports.getActiveStreaksForChannel = (guildID, channelName) => {
let result = []
let users = exports.getUsers()
users.forEach(user => {
user.streaks.filter(streak => streak.guildID === guildID && streak.topic === `#${channelName}` && streak.streakLevel > 0).forEach(streak => {
result.push({
userID: user.userID,
channelName: channelName,
streakLevel: streak.streakLevel
})
})
})
return result
}
exports.getChannels = guildID => {
const channels = db.get('channels').value()
const guildChannels = channels.find(gc => gc.guildID === guildID)
return guildChannels ? guildChannels.channelNames : []
}
exports.getTopAllTimeStreaks = guildID => {
let highscores = []
const users = db.get('users').value()
users.forEach(user => {
highscores.push(...user.streaks.filter(streak => streak.guildID === guildID).map(streak => {
return {
...streak,
userID: user.userID
}
}))
})
// sort them by highest
return highscores.sort((a, b) => {
return b.bestStreak - a.bestStreak
}).filter(score => score.bestStreak > 0)
}
exports.getActiveStreaks = guildID => {
let result = []
let users = exports.getUsers()
users.forEach(user => {
user.streaks.filter(streak => streak.guildID === guildID && streak.streakLevel > 0).forEach(streak => {
result.push({
userID: user.userID,
topic: streak.topic,
streakLevel: streak.streakLevel
})
})
})
return result
}
exports.getUsers = () => {
return db.get('users').value()
}
exports.userHasHighscore = (guildID, userID) => {
const highscores = exports.getTopStreaks(guildID)
if(highscores.length === 0) return false
return Boolean(highscores.find(highscore => {
return highscore.userID === userID
}))
}
exports.getFirstStreakDate = guildID => {
const streaks = db.get('streaks').filter(streak => streak.guildID === guildID).sortBy('date').value()
if(streaks.length === 0) return null;
return format(streaks[0].date, 'do MMM YYYY')
}
exports.getStreaks = () => {
return db.get('streaks').value()
}
/**
* Add a role type definition to the database.
* @param roleID the id of the role to be added
* @param guildID id of the guild in which the role is to be defined
* @param type which type of role is to be added
*/
exports.addRole = (roleID, guildID, type) => {
if (!db.get('configs').value()) {
db.set('configs', []).write()
}
if (!db.get('configs').find({guildID}).value()) {
db.get('configs').push({guildID, top: "", active: ""}).write()
}
db.get('configs')
.find({guildID})
.assign({[type]: roleID})
.write()
}
/**
* Clear the definition of a role type from a guild.
* @param guildID which guild we're clearing it from.
* @param type which type of role should be cleared.
*/
exports.removeRole = (guildID, type) => {
exports.addRole("", guildID, type)
}
/**
* Get the id of a role type from a server.
* @param guildID Which server to look for the role in.
* @param which Which type of role we're looking for.
* @returns The roleID if it exists, undefined otherwise.
*/
exports.getRole = (guildID, which) => {
return db.get('configs')
.find({guildID})
.get(which)
.value()
}
exports.addChannels = (channels, guildID) => {
if (!db.get('channels').find({guildID}).value()) {
db.get('channels').push({guildID, channelNames: []}).write()
}
db.get('channels')
.find({guildID})
.assign({channelNames: channels})
.write()
}