Skip to content

Commit de5d6ac

Browse files
committed
New leveling formula, limit, and many more
1 parent 4052e54 commit de5d6ac

File tree

16 files changed

+851
-349
lines changed

16 files changed

+851
-349
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ If you want to unlock premium commands, please buy me a coffee at least 1 on Ko-
217217
| Sholat schedule | ✔️ |
218218
| Latest Line stickers | ✔️ |
219219
| Check postage | ✔️ |
220-
| Spoiler text | ✔️ |
221220
| Sending email | ✔️ |
222221
| Random quotes | ✔️ |
223222
| Genshin chara info | ✔️ |

database/bot/mute.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[]
1+
[]

database/bot/registered.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[]
1+
[]

database/group/leveling.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[]
1+
[]

database/user/card/background.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[]
1+
[]

database/user/level.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[]
1+
[]

function/card.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
const fs = require('fs-extra')
22

3-
/**
4-
* Add background card.
5-
* @param {String} userId
6-
* @param {Object} _dir
7-
*/
8-
const addBg = (userId, _dir) => {
9-
const obj = { id: userId, link: 'https://i.ibb.co/tYf3jmz/amos-yan-no-entry-1.jpg' }
10-
_dir.push(obj)
11-
fs.writeFileSync('./database/user/card/background.json', JSON.stringify(_dir))
12-
}
13-
143
/**
154
* Get background.
165
* @param {String} userId
176
* @param {Object} _dir
187
* @returns {String}
198
*/
209
const getBg = (userId, _dir) => {
21-
let position = null
10+
let pos = null
11+
let found = false
2212
Object.keys(_dir).forEach((i) => {
2313
if (_dir[i].id === userId) {
24-
position = i
14+
pos = i
15+
found = true
2516
}
2617
})
27-
if (position !== null) {
28-
return _dir[position].link
18+
if (found === false && pos === null) {
19+
const obj = { id: userId, link: 'https://i.ibb.co/tYf3jmz/amos-yan-no-entry-1.jpg' }
20+
_dir.push(obj)
21+
fs.writeFileSync('./database/user/card/background.json', JSON.stringify(_dir))
22+
return 'https://i.ibb.co/tYf3jmz/amos-yan-no-entry-1.jpg'
23+
} else {
24+
return _dir[pos].link
2925
}
3026
}
3127

@@ -49,7 +45,6 @@ const replaceBg = (userId, link, _dir) => {
4945
}
5046

5147
module.exports = {
52-
addBg,
5348
getBg,
5449
replaceBg
5550
}

function/daily.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const fs = require('fs-extra')
2+
3+
/**
4+
* Add limit.
5+
* @param {String} userId
6+
* @param {String} dir
7+
* @param {Object} _dir
8+
*/
9+
const addLimit = (userId, _dir) => {
10+
const obj = { id: userId, time: Date.now() }
11+
_dir.push(obj)
12+
fs.writeFileSync('./database/user/daily.json', JSON.stringify(_dir))
13+
}
14+
15+
/**
16+
* Get time limit.
17+
* @param {String} userId
18+
* @param {Object} _dir
19+
* @returns {Number}
20+
*/
21+
const getLimit = (userId, _dir) => {
22+
let position = null
23+
Object.keys(_dir).forEach((i) => {
24+
if (_dir[i].id === userId) {
25+
position = i
26+
}
27+
})
28+
if (position !== null) {
29+
return _dir[position].time
30+
}
31+
}
32+
33+
module.exports = {
34+
addLimit,
35+
getLimit
36+
}

function/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
exports.level = require('./level')
2+
exports.daily = require('./daily')
23
exports.limit = require('./limit')
34
exports.card = require('./card')
45
exports.register = require('./register')

function/level.js

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const fs = require('fs-extra')
2+
const { getBg } = require('./card')
3+
const _bg = JSON.parse(fs.readFileSync('./database/user/card/background.json'))
24

35
/**
46
* Get user ID from db.
@@ -7,14 +9,22 @@ const fs = require('fs-extra')
79
* @returns {String}
810
*/
911
const getLevelingId = (userId, _dir) => {
10-
let position = null
12+
let pos = null
13+
let found = false
14+
getBg(userId, _bg)
1115
Object.keys(_dir).forEach((i) => {
1216
if (_dir[i].id === userId) {
13-
position = i
17+
pos = i
18+
found = true
1419
}
1520
})
16-
if (position !== null) {
17-
return _dir[position].id
21+
if (found === false && pos === null) {
22+
const obj = { id: userId, xp: 0, level: 1 }
23+
_dir.push(obj)
24+
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
25+
return userId
26+
} else {
27+
return _dir[pos].id
1828
}
1929
}
2030

@@ -25,14 +35,22 @@ const getLevelingId = (userId, _dir) => {
2535
* @returns {Number}
2636
*/
2737
const getLevelingLevel = (userId, _dir) => {
28-
let position = null
38+
let pos = null
39+
let found = false
40+
getBg(userId, _bg)
2941
Object.keys(_dir).forEach((i) => {
3042
if (_dir[i].id === userId) {
31-
position = i
43+
pos = i
44+
found = true
3245
}
3346
})
34-
if (position !== null) {
35-
return _dir[position].level
47+
if (found === false && pos === null) {
48+
const obj = { id: userId, xp: 0, level: 1 }
49+
_dir.push(obj)
50+
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
51+
return 1
52+
} else {
53+
return _dir[pos].level
3654
}
3755
}
3856

@@ -43,28 +61,25 @@ const getLevelingLevel = (userId, _dir) => {
4361
* @returns {Number}
4462
*/
4563
const getLevelingXp = (userId, _dir) => {
46-
let position = null
64+
let pos = null
65+
let found = false
66+
getBg(userId, _bg)
4767
Object.keys(_dir).forEach((i) => {
4868
if (_dir[i].id === userId) {
49-
position = i
69+
pos = i
70+
found = true
5071
}
5172
})
52-
if (position !== null) {
53-
return _dir[position].xp
73+
if (found === false && pos === null) {
74+
const obj = { id: userId, xp: 0, level: 1 }
75+
_dir.push(obj)
76+
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
77+
return 0
78+
} else {
79+
return _dir[pos].xp
5480
}
5581
}
5682

57-
/**
58-
* Add user to db.
59-
* @param {String} userId
60-
* @param {Object} _dir
61-
*/
62-
const addLevelingId = (userId, _dir) => {
63-
const obj = { id: userId, xp: 0, level: 1 }
64-
_dir.push(obj)
65-
fs.writeFileSync('./database/user/level.json', JSON.stringify(_dir))
66-
}
67-
6883
/**
6984
* Add user level to db.
7085
* @param {String} userId
@@ -103,11 +118,26 @@ const addLevelingXp = (userId, amount, _dir) => {
103118
}
104119
}
105120

121+
// Cooldown XP gains to prevent spam
122+
const xpGain = new Set()
123+
124+
const isGained = (from) => {
125+
return !!xpGain.has(from)
126+
}
127+
128+
const addCooldown = (from) => {
129+
xpGain.add(from)
130+
setTimeout(() => {
131+
return xpGain.delete(from)
132+
}, 60000) // Each minute
133+
}
134+
106135
module.exports = {
107136
getLevelingId,
108137
getLevelingLevel,
109138
getLevelingXp,
110-
addLevelingId,
111139
addLevelingLevel,
112-
addLevelingXp
140+
addLevelingXp,
141+
isGained,
142+
addCooldown
113143
}

0 commit comments

Comments
 (0)