forked from acmpesuecc/DrinkWaterBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoring.js
79 lines (74 loc) · 2.24 KB
/
scoring.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
require('dotenv').config();
var admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
const db = admin.firestore()
// Function to increment the user's score
async function incScore(userid, points) {
try {
const userRef = db.collection('users').doc(userid);
await userRef.update({
score: admin.firestore.FieldValue.increment(points)
});
}
catch {
const data = {
score: 1
};
await db.collection('users').doc(userid).set(data);
}
}
// Function to get the user's score
async function getScore(userid) {
const userRef = db.collection('users').doc(userid);
const doc = await userRef.get();
if (!doc.exists) {
return 0;
} else {
var points = await doc.data().score;
return points;
}
}
// Function to count messages in last hour and send water reminder
async function msgCount(msg) {
let userid = msg.author.id;
let timestamp = msg.createdTimestamp;
const userRef = db.collection('users').doc(userid);
const doc = await userRef.get();
if (doc.exists) {
var firstTimestamp = await doc.data().firstTimestamp;
if (firstTimestamp === undefined) {
await userRef.update({
msgCount: 1,
firstTimestamp: timestamp
});
}
else {
if ((timestamp - firstTimestamp) / 1000 > 3600) {
await userRef.update({
msgCount: 1,
firstTimestamp: timestamp
});
}
else {
await userRef.update({
msgCount: admin.firestore.FieldValue.increment(1)
});
var msgCount = await doc.data().msgCount;
if (msgCount > 30) {
msg.author.send("Calm down champ. You've sent more than 30 messages in the last hour, go drink some water! 🥤")
await userRef.update({
msgCount: 1,
firstTimestamp: timestamp
});
}
}
}
}
}
module.exports = {
inc: incScore,
get: getScore,
msgCount: msgCount
};