-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdatabase.js
112 lines (85 loc) · 2.29 KB
/
database.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
const bot = require('./bot')
// set(or modify) functions
exports.addTextMessage = (user, text, time) => {
bot.dbi.createOrIgnoreUser(user)
bot.dbi.addChatMessage(user, text, time)
}
exports.addPlaytime = (user, value) => {
bot.dbi.getOrCreateUser(user, (user_obj)=>{
bot.dbi.updateUser(user, 'playtime', user_obj.playtime + value)
})
}
exports.addTotalLogins = (user, value) => {
bot.dbi.getOrCreateUser(user, (user)=> {
if (user.total_logins === 0) {
bot.dbi.updateUser(user, 'first_login', Date.now() / 1000)
}
bot.dbi.updateUser(user, 'total_logins', user.total_logins + value)
})
}
exports.setLastLogin = (user, value) => {
bot.dbi.createOrIgnoreUser(user)
bot.dbi.updateUser(user, 'last_login', value)
}
exports.setFirstLogin = (user, value) => {
bot.dbi.createOrIgnoreUser(user)
bot.dbi.updateUser(user, 'first_login', value)
}
// get functions
exports.getRandomTextMessage = (user, callback) => {
bot.dbi.getChatMessages(user, (result) => {
if (result === undefined || result.length === 0) {
callback(null)
} else {
let random_index = Math.floor(Math.random() * result.length);
callback(result[random_index]);
}
})
}
exports.getPlaytime = (user, callback) => {
bot.dbi.getUser(user, (result) => {
if (result === undefined) {
callback(null)
} else {
callback(result.playtime)
}
})
}
exports.getLastLogin = (user = '', callback) => {
bot.dbi.getUser(user, (result) => {
if (result === undefined) {
callback(null)
} else {
callback(result.last_login)
}
})
}
exports.getFirstLogin = (user = '', callback) => {
bot.dbi.getUser(user, (result) => {
if (result === undefined) {
callback(null)
} else {
callback(result.first_login)
}
})
}
exports.getTotalLogins = (user = '', callback) => {
bot.dbi.getUser(user, (result) => {
if (result === undefined) {
callback(null)
} else {
callback(result.total_logins)
}
})
}
//
exports.getFirstMessage = (user, callback) => {
// TODO: instead of having first_message in user_info as a field, just search in chat_log table first by date.
bot.dbi.getUser(user, (result) => {
if (result === undefined) {
callback(null)
} else {
callback(result.first_message)
}
})
}