-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
64 lines (60 loc) · 1.59 KB
/
data.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
const Discord = require('discord.js')
const fs = require('node:fs');
const path = require('node:path');
const LRUCache = require('./lru_cache');
const cache = new LRUCache(10);
module.exports = {
getPath: (id) => {
return path.join('data', id + '.json')
},
/**
*
* @param {string | Discord.GuildResolvable} key
* @returns {object}
*/
get: (id) => {
let result = cache.get(id);
if (result == null) {
const filepath = module.exports.getPath(id);
if (fs.existsSync(filepath)) {
result = JSON.parse(fs.readFileSync(filepath));
} else {
result = {}
fs.writeFileSync(filepath, JSON.stringify(result));
}
cache.put(id, result);
}
return result;
},
/**
*
* @param {string} id
* @param {object} data
*/
put: (id, data) => {
cache.put(id, data);
fs.writeFileSync(module.exports.getPath(id), JSON.stringify(data));
},
/**
*
* @param {string} id
* @param {*} key
* @returns
*/
getKey: (id, key) => {
return module.exports.get(id)[key];
},
setKey: (id, key, value) => {
const data = module.exports.get(id);
data[key] = value;
fs.writeFileSync(module.exports.getPath(id), JSON.stringify(data));
},
delKey: (id, key) => {
const data = module.exports.get(id);
delete data[key];
fs.writeFileSync(id, JSON.stringify(data));
},
delete: (id) => {
return cache.delete(id);
}
}