Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const Promise = require('bluebird');
- const fs = Promise.promisifyAll(require('fs'));
- const path = require('path');
- const dbPath = path.join(__dirname, 'db.json');
- const factoryDefaults = {
- defaultPrefix: ";",
- servers: []
- }
- class Server {
- constructor(id) {
- this.id = id;
- }
- }
- function saveJSON(jsonObject) {
- fs.writeFileSync(dbPath, JSON.stringify(jsonObject, null, 2), 'utf-8');
- }
- function getDB() {
- return new Promise(async resolve => {
- let data = await fs.readFileAsync(dbPath);
- try {
- let json = JSON.parse(data);
- resolve(json);
- } catch (ex) {
- saveJSON(factoryDefaults);
- resolve(factoryDefaults);
- }
- });
- }
- function getValue(key) {
- return new Promise(async resolve => {
- let json = await getDB();
- if (typeof json[key] !== "undefined")
- resolve(json[key]);
- else throw new Error(`Key ${key} doesn't exist in the database.`);
- });
- }
- function editValue(key, value) {
- return new Promise(async resolve => {
- let db = await getDB();
- db[key] = value;
- saveJSON(db);
- });
- }
- module.exports = {
- getDB: getDB,
- getValue: getValue,
- editValue: editValue
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement