forked from SlavyanDesu/BocchiBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel.js
More file actions
174 lines (164 loc) · 3.88 KB
/
Copy pathlevel.js
File metadata and controls
174 lines (164 loc) · 3.88 KB
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
const fs = require('fs-extra')
/**
* Get user ID from db.
* @param {string} userId
* @param {object} _dir
* @returns {string}
*/
const getLevelingId = (userId, _dir) => {
let pos = null
let found = false
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
pos = i
found = true
}
})
if (found === false && pos === null) {
const obj = { id: userId, xp: 0, level: 1 }
_dir.push(obj)
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
return userId
} else {
return _dir[pos].id
}
}
/**
* Get user level from db.
* @param {string} userId
* @param {object} _dir
* @returns {number}
*/
const getLevelingLevel = (userId, _dir) => {
let pos = null
let found = false
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
pos = i
found = true
}
})
if (found === false && pos === null) {
const obj = { id: userId, xp: 0, level: 1 }
_dir.push(obj)
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
return 1
} else {
return _dir[pos].level
}
}
/**
* Get user XP from db.
* @param {string} userId
* @param {object} _dir
* @returns {number}
*/
const getLevelingXp = (userId, _dir) => {
let pos = null
let found = false
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
pos = i
found = true
}
})
if (found === false && pos === null) {
const obj = { id: userId, xp: 0, level: 1 }
_dir.push(obj)
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
return 0
} else {
return _dir[pos].xp
}
}
/**
* Add user level to db.
* @param {string} userId
* @param {number} amount
* @param {object} _dir
*/
const addLevelingLevel = (userId, amount, _dir) => {
let position = null
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
}
})
if (position !== null) {
_dir[position].level += amount
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
}
}
/**
* Add user XP to db.
* @param {string} userId
* @param {number} amount
* @param {object} _dir
*/
const addLevelingXp = (userId, amount, _dir) => {
let position = null
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
}
})
if (position !== null) {
_dir[position].xp += amount
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
}
}
/**
* Get user rank.
* @param {string} userId
* @param {object} _dir
* @returns {number}
*/
const getUserRank = (userId, _dir) => {
let position = null
let found = false
_dir.sort((a, b) => (a.xp < b.xp) ? 1 : -1)
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
position = i
found = true
}
})
if (found === false && position === null) {
const obj = { id: userId, xp: 0, level: 1 }
_dir.push(obj)
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
return 99
} else {
return position + 1
}
}
// Cooldown XP gains to prevent spam
const xpGain = new Set()
/**
* Check is user exist in set.
* @param {string} userId
* @returns {boolean}
*/
const isGained = (userId) => {
return !!xpGain.has(userId)
}
/**
* Add user in set and delete it when it's 1 minute.
* @param {string} userId
*/
const addCooldown = (userId) => {
xpGain.add(userId)
setTimeout(() => {
return xpGain.delete(userId)
}, 60000) // Each minute
}
module.exports = {
getLevelingId,
getLevelingLevel,
getLevelingXp,
addLevelingLevel,
addLevelingXp,
getUserRank,
isGained,
addCooldown
}