-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrency.js
More file actions
37 lines (31 loc) · 1.07 KB
/
currency.js
File metadata and controls
37 lines (31 loc) · 1.07 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
const fs = require("fs");
const data = require("./data.json");
function getCurrencyPath(userID) {
return "./currency/"+userID+".dat";
}
function getCurrency(userID) {
if(!fs.existsSync(getCurrencyPath(userID))) {
fs.writeFileSync(getCurrencyPath(userID), '0', (err) => {});
return 0;
}else return parseInt(fs.readFileSync(getCurrencyPath(userID), 'UTF-8').split(/\r?\n/)[0]);
}
async function setCurrency(userID, num) {
if(!fs.existsSync(getCurrencyPath(userID))) {
fs.writeFileSync(getCurrencyPath(userID), num, (err) => {});
}else {
var newCurrency = ""+num;
// fs.unlink(getCurrencyPath(userID), (err) => {});
fs.writeFileSync(getCurrencyPath(userID), newCurrency, (err) => {});
}
}
async function addCurrency(userID, num) {
var currentCurrency = getCurrency(userID);
var newCurrency = currentCurrency+num;
await setCurrency(userID, newCurrency);
}
function cname() {
const names = data.currencyNames;
const name = names[Math.floor(Math.random() * names.length)];
return name;
}
module.exports = { getCurrency, setCurrency, addCurrency, cname };