This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
generated from NamVr/DiscordBot-Template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.js
108 lines (100 loc) · 3.16 KB
/
db.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/**
* @author: Vachan MN
* @file: This file contains the database object class
* @version: 1.0
*
*/
const fs = require("fs");
class Database {
/**
* @description Constructor for the Database class
* @param {string} path Path to the database file
* @param {Object} client bot client
*/
constructor(path, client){
this.path = path;
this.client = client;
let db = undefined;
try{
db = fs.readdirSync(path);
console.log("Loaded database");
}catch(e){
console.log("Database not found, Creating new Database");
fs.mkdirSync(path);
db = fs.readdirSync(path);
console.log("Loaded Database");
}
const Guilds = client.guilds.cache.map(g => g);
for (const server of Guilds) {
try{
fs.readdirSync(path + "/" + server.id);
console.log("Loaded server database for " + server.name);
} catch(e){
console.log("Database not found, Creating new Database for " + server.name);
fs.mkdirSync(path + "/" + server.id);
}
}
}
/**
* @description Get a server db object
* @param {string} serverId Server ID
* @returns {fs.Dir} Server Database Object
*/
getServerDb(serverId){
const path = this.path + "/" + serverId;
const db = fs.readdirSync(path);
return db;
}
/**
* @description Add a new collection to the database
* @param {string} serverId Server ID
* @param {string} collectionName Collection Name
* @returns {Object} Collection json object
*/
addCollection(serverId, collectionName){
const path = this.path + "/" + serverId + "/" + collectionName + ".json";
let collection = undefined;
try{
const collection = fs.readdirSync(path);
console.log("Collection already exists");
} catch (e){
fs.writeFileSync(path, JSON.stringify({}));
console.log("Collection created");
collection = fs.readdirSync(path);
}
return db;
}
/**
* @description Get a collection from the database
* @param {string} serverId Server ID
* @param {string} collectionName Collection Name
* @returns {Object} Collection json object
*/
getCollection(serverId, collectionName){
const path = this.path + "/" + serverId + "/" + collectionName + ".json";
let collection = undefined;
try{
collection = fs.readdirSync(path);
console.log("Collection found");
} catch (e){
console.log("Collection not found");
}
return collection;
}
/**
* @description update the collection
* @param {string} serverId Server ID
* @param {string} collectionName Collection Name
* @param {Object} collection Collection Object
*/
updateCollection(serverId, collectionName, collection){
const path = this.path + "/" + serverId + "/" + collectionName + ".json";
try{
fs.writeFileSync(path, JSON.stringify(collection));
console.log("Collection updated");
} catch (e){
console.log("Collection not found");
}
}
}
module.exports = Database;