-
Notifications
You must be signed in to change notification settings - Fork 924
Expand file tree
/
Copy pathmongodb.js
More file actions
72 lines (65 loc) · 2.86 KB
/
Copy pathmongodb.js
File metadata and controls
72 lines (65 loc) · 2.86 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
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
const { MongoClient } = require('mongodb');
const colors = require('./UI/colors/colors');
const config = require("./config.js");
require('dotenv').config();
let client;
if (config.mongodbUri) {
const uri = config.mongodbUri;
client = new MongoClient(uri);
} else {
try {
const { getLangSync } = require('./utils/languageLoader.js');
const lang = getLangSync();
console.warn("\x1b[33m[ WARNING ]\x1b[0m " + (lang.console?.mongodb?.uriNotDefined || "MongoDB URI is not defined in the configuration."));
} catch (e) {
console.warn("\x1b[33m[ WARNING ]\x1b[0m MongoDB URI is not defined in the configuration.");
}
}
async function connectToDatabase() {
try {
const { getLangSync } = require('./utils/languageLoader.js');
const lang = getLangSync();
if (!client) {
console.warn("\x1b[33m[ WARNING ]\x1b[0m " + (lang.console?.mongodb?.skippingConnection || "Skipping MongoDB connection as URI is not provided."));
return;
}
try {
await client.connect();
console.log('\n' + '─'.repeat(40));
console.log(`${colors.magenta}${colors.bright}${lang.console?.bot?.databaseConnection || '🕸️ DATABASE CONNECTION'}${colors.reset}`);
console.log('─'.repeat(40));
console.log('\x1b[36m[ DATABASE ]\x1b[0m', '\x1b[32m' + (lang.console?.mongodb?.connected || 'Connected to MongoDB ✅') + '\x1b[0m');
} catch (err) {
console.warn("\x1b[33m[ WARNING ]\x1b[0m " + (lang.console?.mongodb?.connectionFailed || "Could not connect to MongoDB. Continuing without database functionality."));
console.error(err.message);
}
} catch (e) {
if (!client) {
console.warn("\x1b[33m[ WARNING ]\x1b[0m Skipping MongoDB connection as URI is not provided.");
return;
}
try {
await client.connect();
console.log('\n' + '─'.repeat(40));
console.log(`${colors.magenta}${colors.bright}🕸️ DATABASE CONNECTION${colors.reset}`);
console.log('─'.repeat(40));
console.log('\x1b[36m[ DATABASE ]\x1b[0m', '\x1b[32mConnected to MongoDB ✅\x1b[0m');
} catch (err) {
console.warn("\x1b[33m[ WARNING ]\x1b[0m Could not connect to MongoDB. Continuing without database functionality.");
console.error(err.message);
}
}
}
const db = client ? client.db("PrimeMusicSSRR") : null;
const playlistCollection = db ? db.collection("SongPlayLists") : null;
const autoplayCollection = db ? db.collection("AutoplaySettings") : null;
const languageCollection = db ? db.collection("GuildLanguages") : null;
function getLanguageCollection() {
return languageCollection;
}
module.exports = {
connectToDatabase,
playlistCollection,
autoplayCollection,
getLanguageCollection,
};