-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbanned.js
91 lines (85 loc) · 2.09 KB
/
banned.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
const fs = require('fs')
const toMs = require('ms')
/**
* Add user to bannedList datauser
* @param {String} userId
* @param {String} expired
* @param {Object} _data
*/
const addBanned = (userId, expired, _data) => {
let success = false
if (expired === undefined) {
expired = 'PERMANENT'
} else {
expired = expired
}
let expired_at = 'PERMANENT'
if (expired === 'PERMANENT') {
expired_at = 'PERMANENT'
} else {
expired_at = Date.now() + toMs(expired)
}
const obj = {
id: userId,
expired: expired_at
}
_data.push(obj)
fs.writeFileSync('./database/datauser/banned.json', JSON.stringify(_data))
}
/**
* Unbanned someone.
* @param {String} userId
* @param {Object} _dir
* @returns {Number}
*/
const unBanned = (userId, _data) => {
let position = null
Object.keys(_data).forEach((i) => {
if (_data[i].id === userId) {
position = i
}
})
if (position !== null) {
_data.splice(position, 1)
fs.writeFileSync('./database/datauser/banned.json', JSON.stringify(_data))
}
return true
}
const BannedExpired = (_dir) => {
setInterval(() => {
let position = null
Object.keys(_dir).forEach((i) => {
if (_dir[i].expired === 'PERMANENT') {
position = null
} else if (Date.now() >= _dir[i].expired) {
position = i
}
})
if (position !== null) {
console.log(`Banned expired: ${_dir[position].id}`)
_dir.splice(position, 1)
fs.writeFileSync('./database/datauser/banned.json', JSON.stringify(_dir))
}
}, 1000)
}
/**
* Check user is premium.
* @param {String} userId
* @param {Object} _dir
* @returns {Boolean}
*/
const cekBannedUser = (userId, _dir) => {
let status = false
Object.keys(_dir).forEach((i) => {
if (_dir[i].id === userId) {
status = true
}
})
return status
}
module.exports = {
addBanned,
unBanned,
BannedExpired,
cekBannedUser
}