Upload 3 files
Browse files- includes/database/currencies.js +96 -0
- includes/database/threads.js +161 -0
- includes/database/users.js +189 -0
includes/database/currencies.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module.exports = function ({ models, Users }) {
|
| 2 |
+
const { readFileSync, writeFileSync } = require("fs-extra");
|
| 3 |
+
var path = __dirname + "/data/usersData.json";
|
| 4 |
+
try {
|
| 5 |
+
var Currencies = require(path)
|
| 6 |
+
} catch {
|
| 7 |
+
writeFileSync(path, "{}", { flag: 'a+' });
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
async function saveData(data) {
|
| 11 |
+
try {
|
| 12 |
+
if (!data) throw new Error('Data cannot be left blank');
|
| 13 |
+
writeFileSync(path, JSON.stringify(data, null, 4))
|
| 14 |
+
return true
|
| 15 |
+
} catch (error) {
|
| 16 |
+
return false
|
| 17 |
+
}
|
| 18 |
+
}
|
| 19 |
+
async function getData(userID) {
|
| 20 |
+
try {
|
| 21 |
+
if (!userID) throw new Error("User ID cannot be blank");
|
| 22 |
+
if (isNaN(userID)) throw new Error("Invalid user ID");
|
| 23 |
+
if (!userID) throw new Error("userID cannot be empty");
|
| 24 |
+
if (!Currencies.hasOwnProperty(userID)) console.log(`User ID: ${userID} does not exist in Database`);
|
| 25 |
+
const data = await Users.getData(userID);
|
| 26 |
+
return data
|
| 27 |
+
}
|
| 28 |
+
catch (error) {
|
| 29 |
+
console.log(error)
|
| 30 |
+
return false
|
| 31 |
+
};
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
async function setData(userID, options = {}) {
|
| 35 |
+
try {
|
| 36 |
+
if (!userID) throw new Error("User ID cannot be blank");
|
| 37 |
+
if (isNaN(userID)) throw new Error("Invalid user ID");
|
| 38 |
+
if (!userID) throw new Error("userID cannot be empty");
|
| 39 |
+
if (!Currencies.hasOwnProperty(userID)) throw new Error(`User ID: ${userID} does not exist in Database`);
|
| 40 |
+
if (typeof options != 'object') throw new Error("The options parameter passed must be an object");
|
| 41 |
+
Currencies[userID] = {...Currencies[userID], ...options};
|
| 42 |
+
await saveData(Currencies);
|
| 43 |
+
return Currencies[userID];
|
| 44 |
+
} catch (error) {
|
| 45 |
+
return false
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
async function delData(userID, callback) {
|
| 50 |
+
try {
|
| 51 |
+
if (!userID) throw new Error("User ID cannot be blank");
|
| 52 |
+
if (isNaN(userID)) throw new Error("Invalid user ID");
|
| 53 |
+
if (!usersData.hasOwnProperty(userID)) throw new Error(`User ID: ${userID} does not exist in Database`);
|
| 54 |
+
usersData[userID].money = 0;
|
| 55 |
+
await saveData(usersData);
|
| 56 |
+
if (callback && typeof callback == "function") callback(null, usersData);
|
| 57 |
+
return usersData;
|
| 58 |
+
} catch (error) {
|
| 59 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 60 |
+
return false
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
async function increaseMoney(userID, money) {
|
| 65 |
+
if (typeof money != 'number') throw global.getText("currencies", "needNumber");
|
| 66 |
+
try {
|
| 67 |
+
let balance = (await getData(userID)).money;
|
| 68 |
+
await setData(userID, { money: balance + money });
|
| 69 |
+
return true;
|
| 70 |
+
}
|
| 71 |
+
catch (error) {
|
| 72 |
+
console.error(error);
|
| 73 |
+
throw new Error(error);
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
async function decreaseMoney(userID, money) {
|
| 78 |
+
if (typeof money != 'number') throw global.getText("currencies", "needNumber");
|
| 79 |
+
try {
|
| 80 |
+
let balance = (await getData(userID)).money;
|
| 81 |
+
if (balance < money) return false;
|
| 82 |
+
await setData(userID, { money: balance - money });
|
| 83 |
+
return true;
|
| 84 |
+
} catch (error) {
|
| 85 |
+
throw new Error(error);
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
return {
|
| 90 |
+
getData,
|
| 91 |
+
setData,
|
| 92 |
+
delData,
|
| 93 |
+
increaseMoney,
|
| 94 |
+
decreaseMoney
|
| 95 |
+
};
|
| 96 |
+
};
|
includes/database/threads.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module.exports = function ({ api }) {
|
| 2 |
+
const Users = require("./users")({ api });
|
| 3 |
+
const logger =require("../../utils/log.js");
|
| 4 |
+
const { writeFileSync, readFileSync } = require("fs-extra");
|
| 5 |
+
var path = __dirname + "/data/threadsData.json";
|
| 6 |
+
|
| 7 |
+
try {
|
| 8 |
+
var threadsData = require(path)
|
| 9 |
+
} catch {
|
| 10 |
+
writeFileSync(path, "{}", { flag: 'a+' });
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
async function getInfo(threadID) {
|
| 14 |
+
try {
|
| 15 |
+
const result = await api.getThreadInfo(threadID);
|
| 16 |
+
return result;
|
| 17 |
+
}
|
| 18 |
+
catch (error) {
|
| 19 |
+
throw new Error(error);
|
| 20 |
+
return false
|
| 21 |
+
};
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
async function getData(threadID, callback) {
|
| 25 |
+
try {
|
| 26 |
+
if (!threadID) throw new Error("threadID cannot be empty");
|
| 27 |
+
if (isNaN(threadID)) throw new Error("Invalid threadID");
|
| 28 |
+
if (!threadsData.hasOwnProperty(threadID)) await createData(threadID);
|
| 29 |
+
const data = threadsData[threadID];
|
| 30 |
+
if (callback && typeof callback == "function") callback(null, data);
|
| 31 |
+
return data;
|
| 32 |
+
} catch (error) {
|
| 33 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 34 |
+
return false
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
async function saveData(data) {
|
| 39 |
+
try {
|
| 40 |
+
if (!data) throw new Error('Data cannot be left blank');
|
| 41 |
+
writeFileSync(path, JSON.stringify(data, null, 4))
|
| 42 |
+
} catch (error) {
|
| 43 |
+
return false
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
async function getAll(keys, callback) {
|
| 47 |
+
try {
|
| 48 |
+
if (!keys) {
|
| 49 |
+
if (Object.keys(threadsData).length == 0) return [];
|
| 50 |
+
else if (Object.keys(threadsData).length > 0) {
|
| 51 |
+
var db = [];
|
| 52 |
+
for (var i of Object.keys(threadsData)) db.push(threadsData[i]);
|
| 53 |
+
return db;
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
if (!Array.isArray(keys)) throw new Error("The input parameter must be an array");
|
| 57 |
+
const data = [];
|
| 58 |
+
for (var ID in threadsData) {
|
| 59 |
+
const database = {
|
| 60 |
+
ID: ID
|
| 61 |
+
};
|
| 62 |
+
const threadData = threadsData[ID];
|
| 63 |
+
for (var i of keys) database[i] = threadData[i];
|
| 64 |
+
data.push(database);
|
| 65 |
+
}
|
| 66 |
+
if (callback && typeof callback == "function") callback(null, data);
|
| 67 |
+
return data;
|
| 68 |
+
} catch (error) {
|
| 69 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 70 |
+
return false
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
async function setData(threadID, options, callback) {
|
| 74 |
+
try {
|
| 75 |
+
if (!threadID) throw new Error("threadID cannot be empty");
|
| 76 |
+
if (isNaN(threadID)) throw new Error("Invalid threadID");
|
| 77 |
+
if (!threadsData.hasOwnProperty(threadID)) throw new Error(`Threads with ID: ${threadID} does not exist in Database`);
|
| 78 |
+
if (typeof options != 'object') throw new Error("The options parameter passed must be an object");
|
| 79 |
+
threadsData[threadID] = {
|
| 80 |
+
...threadsData[threadID],
|
| 81 |
+
...options
|
| 82 |
+
}
|
| 83 |
+
await saveData(threadsData);
|
| 84 |
+
if (callback && typeof callback == "function") callback(null, threadsData[threadID]);
|
| 85 |
+
return threadsData[threadID];
|
| 86 |
+
}
|
| 87 |
+
catch(error) {
|
| 88 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 89 |
+
return false
|
| 90 |
+
}
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
async function delData(threadID, callback) {
|
| 94 |
+
try {
|
| 95 |
+
if (!threadID) throw new Error("threadID cannot be empty");
|
| 96 |
+
if (isNaN(threadID)) throw new Error("Invalid threadID");
|
| 97 |
+
if (!threadsData.hasOwnProperty(threadID)) throw new Error(`Threads with ID: ${threadID} does not exist in Database`);
|
| 98 |
+
delete threadsData[threadID];
|
| 99 |
+
await saveData(threadsData);
|
| 100 |
+
if (callback && typeof callback == "function") callback(null, "REMOVE THREAD"+ threadID + "SUCCESS");
|
| 101 |
+
return true;
|
| 102 |
+
} catch(error) {
|
| 103 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 104 |
+
return false
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
async function createData(threadID, callback) {
|
| 109 |
+
try {
|
| 110 |
+
if (!threadID) throw new Error("threadID cannot be empty");
|
| 111 |
+
if (isNaN(threadID)) throw new Error("Invalid threadID");
|
| 112 |
+
if (threadsData.hasOwnProperty(threadID)) throw new Error(`Threads with ID: ${threadID} already exists in Database`);
|
| 113 |
+
var threadInfo = await api.getThreadInfo(threadID);
|
| 114 |
+
var data = {
|
| 115 |
+
[threadID]: {
|
| 116 |
+
threadInfo: {
|
| 117 |
+
threadID: threadID,
|
| 118 |
+
threadName: threadInfo.threadName,
|
| 119 |
+
emoji: threadInfo.emoji,
|
| 120 |
+
adminIDs: threadInfo.adminIDs,
|
| 121 |
+
participantIDs: threadInfo.participantIDs,
|
| 122 |
+
isGroup: threadInfo.isGroup,
|
| 123 |
+
},
|
| 124 |
+
createTime: {
|
| 125 |
+
timestamp: Date.now()
|
| 126 |
+
},
|
| 127 |
+
data: {
|
| 128 |
+
timestamp: Date.now()
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
}
|
| 132 |
+
Object.assign(threadsData, data);
|
| 133 |
+
const dataUser = global.data.allUserID
|
| 134 |
+
for (singleData of threadInfo.userInfo) {
|
| 135 |
+
if(singleData.gender != undefined) {
|
| 136 |
+
try {
|
| 137 |
+
if(dataUser.includes(singleData.id) || Users.hasOwnProperty(singleData.id)) continue
|
| 138 |
+
dataUser.push(singleData.id)
|
| 139 |
+
await Users.createData(singleData.id)
|
| 140 |
+
logger.log(global.getText('handleCreateDatabase', 'newUser', singleData.id), 'DATABASE');
|
| 141 |
+
} catch(e) { console.log(e) };
|
| 142 |
+
}
|
| 143 |
+
}
|
| 144 |
+
await saveData(threadsData)
|
| 145 |
+
if (callback && typeof callback == "function") callback(null, data);
|
| 146 |
+
return data;
|
| 147 |
+
} catch (error) {
|
| 148 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 149 |
+
return false
|
| 150 |
+
}
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
return {
|
| 154 |
+
getInfo,
|
| 155 |
+
getAll,
|
| 156 |
+
getData,
|
| 157 |
+
setData,
|
| 158 |
+
delData,
|
| 159 |
+
createData
|
| 160 |
+
};
|
| 161 |
+
};
|
includes/database/users.js
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
module.exports = function ({ api }) {
|
| 2 |
+
const { writeFileSync } = require("fs-extra");
|
| 3 |
+
var path = __dirname + "/data/usersData.json";
|
| 4 |
+
|
| 5 |
+
try {
|
| 6 |
+
var usersData = require(path)
|
| 7 |
+
} catch {
|
| 8 |
+
writeFileSync(path, "{}", { flag: 'a+' });
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
async function saveData(data) {
|
| 12 |
+
try {
|
| 13 |
+
if (!data) throw new Error('Data cannot be left blank');
|
| 14 |
+
writeFileSync(path, JSON.stringify(data, null, 4))
|
| 15 |
+
return true
|
| 16 |
+
} catch (error) {
|
| 17 |
+
return false
|
| 18 |
+
}
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
async function getInfo(id) {
|
| 22 |
+
return (await api.getUserInfo(id))[id];
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
async function getNameUser(userID) {
|
| 26 |
+
try {
|
| 27 |
+
if (!userID) throw new Error("User ID cannot be blank");
|
| 28 |
+
if (isNaN(userID)) throw new Error("Invalid user ID");
|
| 29 |
+
var userInfo = await api.getUserInfo(userID);
|
| 30 |
+
return `User ID: ${userID}`;
|
| 31 |
+
} catch (error) {
|
| 32 |
+
return `Facebook users`
|
| 33 |
+
}
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
async function getUserFull(id) {
|
| 37 |
+
var resolveFunc = function () { };
|
| 38 |
+
var rejectFunc = function () { };
|
| 39 |
+
var returnPromise = new Promise(function (resolve, reject) {
|
| 40 |
+
resolveFunc = resolve;
|
| 41 |
+
rejectFunc = reject;
|
| 42 |
+
});
|
| 43 |
+
try {
|
| 44 |
+
api.httpGet(`https://graph.facebook.com/${id}?fields=email,about,birthday,link&access_token=${global.account.accessToken}`, (e, i) => {
|
| 45 |
+
if (e) return rejectFunc(e)
|
| 46 |
+
var t = JSON.parse(i);
|
| 47 |
+
var dataUser = {
|
| 48 |
+
error: 0,
|
| 49 |
+
author: 'D-Jukie',
|
| 50 |
+
data: {
|
| 51 |
+
uid: t.id || null,
|
| 52 |
+
about: t.about || null,
|
| 53 |
+
link: t.link || null,
|
| 54 |
+
imgavt: `https://graph.facebook.com/${t.id}/picture?height=1500&width=1500&access_token=1073911769817594|aa417da57f9e260d1ac1ec4530b417de`
|
| 55 |
+
}
|
| 56 |
+
};
|
| 57 |
+
return resolveFunc(dataUser)
|
| 58 |
+
});
|
| 59 |
+
return returnPromise
|
| 60 |
+
} catch (error) {
|
| 61 |
+
return resolveFunc({
|
| 62 |
+
error: 1,
|
| 63 |
+
author: 'D-Jukie',
|
| 64 |
+
data: {}
|
| 65 |
+
})
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
async function getAll(keys, callback) {
|
| 70 |
+
try {
|
| 71 |
+
if (!keys) {
|
| 72 |
+
if (Object.keys(usersData).length == 0) return [];
|
| 73 |
+
else if (Object.keys(usersData).length > 0) {
|
| 74 |
+
var db = [];
|
| 75 |
+
for (var i of Object.keys(usersData)) db.push(usersData[i]);
|
| 76 |
+
return db;
|
| 77 |
+
}
|
| 78 |
+
}
|
| 79 |
+
if (!Array.isArray(keys)) throw new Error("The input parameter must be an array");
|
| 80 |
+
const data = [];
|
| 81 |
+
for (var userID in usersData) {
|
| 82 |
+
var database = {
|
| 83 |
+
ID: userID
|
| 84 |
+
};
|
| 85 |
+
var userData = usersData[userID];
|
| 86 |
+
for (var i of keys) database[i] = userData[i];
|
| 87 |
+
data.push(database);
|
| 88 |
+
}
|
| 89 |
+
if (callback && typeof callback == "function") callback(null, data);
|
| 90 |
+
return data;
|
| 91 |
+
} catch (error) {
|
| 92 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 93 |
+
return false
|
| 94 |
+
}
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
async function getData(userID, callback) {
|
| 98 |
+
try {
|
| 99 |
+
if (!userID) throw new Error("User ID cannot be blank");
|
| 100 |
+
if (isNaN(userID)) throw new Error("Invalid user ID");
|
| 101 |
+
if (!usersData.hasOwnProperty(userID)) await createData(userID, (error, info) => {
|
| 102 |
+
return info;
|
| 103 |
+
});
|
| 104 |
+
const data = usersData[userID];
|
| 105 |
+
if (callback && typeof callback == "function") callback(null, data);
|
| 106 |
+
return data;
|
| 107 |
+
} catch (error) {
|
| 108 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 109 |
+
return false
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
async function setData(userID, options, callback) {
|
| 114 |
+
try {
|
| 115 |
+
if (!userID) throw new Error("User ID cannot be blank");
|
| 116 |
+
if (isNaN(userID)) throw new Error("Invalid user ID");
|
| 117 |
+
if (!userID) throw new Error("userID cannot be empty");
|
| 118 |
+
if (global.config.autoCreateDB) {
|
| 119 |
+
if (!usersData.hasOwnProperty(userID)) throw new Error(`User ID: ${userID} does not exist in Database`);
|
| 120 |
+
}
|
| 121 |
+
if (typeof options != 'object') throw new Error("The options parameter passed must be an object");
|
| 122 |
+
usersData[userID] = { ...usersData[userID], ...options };
|
| 123 |
+
await saveData(usersData);
|
| 124 |
+
if (callback && typeof callback == "function") callback(null, dataUser[userID]);
|
| 125 |
+
return usersData[userID];
|
| 126 |
+
} catch (error) {
|
| 127 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 128 |
+
return false
|
| 129 |
+
}
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
async function delData(userID, callback) {
|
| 133 |
+
try {
|
| 134 |
+
if (!userID) throw new Error("User ID cannot be blank");
|
| 135 |
+
if (isNaN(userID)) throw new Error("Invalid user ID");
|
| 136 |
+
if (global.config.autoCreateDB) {
|
| 137 |
+
if (!usersData.hasOwnProperty(userID)) throw new Error(`User ID: ${userID} does not exist in Database`);
|
| 138 |
+
}
|
| 139 |
+
delete usersData[userID];
|
| 140 |
+
await saveData(usersData);
|
| 141 |
+
if (callback && typeof callback == "function") callback(null, usersData);
|
| 142 |
+
return usersData;
|
| 143 |
+
} catch (error) {
|
| 144 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 145 |
+
return false
|
| 146 |
+
}
|
| 147 |
+
}
|
| 148 |
+
|
| 149 |
+
async function createData(userID, callback) {
|
| 150 |
+
try {
|
| 151 |
+
if (!userID) throw new Error("User ID cannot be blank");
|
| 152 |
+
if (isNaN(userID)) throw new Error("Invalid user ID");
|
| 153 |
+
var userInfo = await getInfo(userID);
|
| 154 |
+
if (usersData.hasOwnProperty(userID)) return false
|
| 155 |
+
var data = {
|
| 156 |
+
[userID]: {
|
| 157 |
+
userID: userID,
|
| 158 |
+
money: 0,
|
| 159 |
+
exp: 0,
|
| 160 |
+
createTime: {
|
| 161 |
+
timestamp: Date.now()
|
| 162 |
+
},
|
| 163 |
+
data: {
|
| 164 |
+
timestamp: Date.now()
|
| 165 |
+
},
|
| 166 |
+
lastUpdate: Date.now()
|
| 167 |
+
}
|
| 168 |
+
}
|
| 169 |
+
Object.assign(usersData, data);
|
| 170 |
+
await saveData(usersData);
|
| 171 |
+
if (callback && typeof callback == "function") callback(null, data);
|
| 172 |
+
return data;
|
| 173 |
+
} catch (error) {
|
| 174 |
+
if (callback && typeof callback == "function") callback(error, null);
|
| 175 |
+
return false
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
return {
|
| 180 |
+
getInfo,
|
| 181 |
+
getNameUser,
|
| 182 |
+
getAll,
|
| 183 |
+
getData,
|
| 184 |
+
setData,
|
| 185 |
+
delData,
|
| 186 |
+
createData,
|
| 187 |
+
getUserFull
|
| 188 |
+
};
|
| 189 |
+
};
|