Browse Source

Updated Discord.js package on backend.

KrisVos130 7 years ago
parent
commit
93a058d052
3 changed files with 186 additions and 189 deletions
  1. 185 187
      backend/index.js
  2. 1 1
      backend/logic/i18n.js
  3. 0 1
      backend/package.json

+ 185 - 187
backend/index.js

@@ -44,11 +44,8 @@ const getError = (err) => {
 };
 
 client.on('ready', () => {
-	discordClientCBS.forEach((cb) => {
-		cb();
-	});
-	discordClientCBS = [];
 	console.log(`Logged in to Discord as ${client.user.username}#${client.user.discriminator}`);
+	initialize();
 });
 
 client.on('disconnect', (err) => {
@@ -57,34 +54,26 @@ client.on('disconnect', (err) => {
 
 client.login(config.get('apis.discord.token'));
 
-let discordClientCBS = [];
-const getDiscordClient = (cb) => {
-	if (client.status === 0) return cb();
-	else discordClientCBS.push(cb);
-};
-
 const logToDiscord = (message, color, type, critical, extraFields, cb = ()=>{}) => {
-	getDiscordClient(() => {
-		let richEmbed = new Discord.RichEmbed();
-		richEmbed.setAuthor("Musare Logger", config.get("domain")+"/favicon-194x194.png", config.get("domain"));
-		richEmbed.setColor(color);
-		richEmbed.setDescription(message);
-		//richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
-		//richEmbed.setImage("https://musare.com/favicon-194x194.png");
-		//richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
-		richEmbed.setTimestamp(new Date());
-		richEmbed.setTitle("MUSARE ALERT");
-		richEmbed.setURL(config.get("domain"));
-		richEmbed.addField("Type:", type, true);
-		richEmbed.addField("Critical:", (critical) ? 'True' : 'False', true);
-		extraFields.forEach((extraField) => {
-			richEmbed.addField(extraField.name, extraField.value, extraField.inline);
-		});
-		client.channels.get(config.get('apis.discord.loggingChannel')).sendEmbed(richEmbed).then(() => {
-			cb();
-		}).then((reason) => {
-			cb(reason);
-		});
+	let richEmbed = new Discord.RichEmbed();
+	richEmbed.setAuthor("Musare Logger", config.get("domain")+"/favicon-194x194.png", config.get("domain"));
+	richEmbed.setColor(color);
+	richEmbed.setDescription(message);
+	//richEmbed.setFooter("Footer", "https://musare.com/favicon-194x194.png");
+	//richEmbed.setImage("https://musare.com/favicon-194x194.png");
+	//richEmbed.setThumbnail("https://musare.com/favicon-194x194.png");
+	richEmbed.setTimestamp(new Date());
+	richEmbed.setTitle("MUSARE ALERT");
+	richEmbed.setURL(config.get("domain"));
+	richEmbed.addField("Type:", type, true);
+	richEmbed.addField("Critical:", (critical) ? 'True' : 'False', true);
+	extraFields.forEach((extraField) => {
+		richEmbed.addField(extraField.name, extraField.value, extraField.inline);
+	});
+	client.channels.get(config.get('apis.discord.loggingChannel')).send("", { embed: richEmbed }).then(() => {
+		cb();
+	}).then((reason) => {
+		cb(reason);
 	});
 };
 
@@ -103,165 +92,174 @@ function errorCb(message, err, component) {
 	logToDiscord(message, "#FF0000", message, true, [{name: "Error:", value: err, inline: false}, {name: "Component:", value: component, inline: true}]);
 }
 
-async.waterfall([
-
-	// setup our translation
-	(next) => {
-		currentComponent = 'Translation';
-		i18n.init(next);
-	},
-
-	// setup our Redis cache
-	(next) => {
-		currentComponent = 'Cache';
-		cache.init(config.get('redis').url, config.get('redis').password, errorCb, () => {
-			next();
-		});
-	},
-
-	// setup our MongoDB database
-	(next) => {
-		initializedComponents.push(cache);
-		currentComponent = 'DB';
-		db.init(config.get("mongo").url, errorCb, next);
-	},
-
-	// setup the express server
-	(next) => {
-		initializedComponents.push(db);
-		currentComponent = 'App';
-		app.init(next);
-	},
-
-	// setup the mail
-	(next) => {
-		initializedComponents.push(app);
-		currentComponent = 'Mail';
-		mail.init(next);
-	},
-
-	// setup the socket.io server (all client / server communication is done over this)
-	(next) => {
-		initializedComponents.push(mail);
-		currentComponent = 'IO';
-		io.init(next);
-	},
-
-	// setup the punishment system
-	(next) => {
-		initializedComponents.push(io);
-		currentComponent = 'Punishments';
-		punishments.init(next);
-	},
-
-	// setup the notifications
-	(next) => {
-		initializedComponents.push(punishments);
-		currentComponent = 'Notifications';
-		notifications.init(config.get('redis').url, config.get('redis').password, errorCb, next);
-	},
-
-	// setup the stations
-	(next) => {
-		initializedComponents.push(notifications);
-		currentComponent = 'Stations';
-		stations.init(next)
-	},
-
-	// setup the songs
-	(next) => {
-		initializedComponents.push(stations);
-		currentComponent = 'Songs';
-		songs.init(next)
-	},
-
-	// setup the playlists
-	(next) => {
-		initializedComponents.push(songs);
-		currentComponent = 'Playlists';
-		playlists.init(next)
-	},
-
-	// setup the API
-	(next) => {
-		initializedComponents.push(playlists);
-		currentComponent = 'API';
-		api.init(next)
-	},
-
-	// setup the logger
-	(next) => {
-		initializedComponents.push(api);
-		currentComponent = 'Logger';
-		logger.init(next)
-	},
-
-	// setup the tasks system
-	(next) => {
-		initializedComponents.push(logger);
-		currentComponent = 'Tasks';
-		tasks.init(next)
-	},
-
-	// setup the frontend for local setups
-	(next) => {
-		initializedComponents.push(tasks);
-		currentComponent = 'Windows';
-		if (!config.get("isDocker")) {
-			const express = require('express');
-			const app = express();
-			app.listen(config.get("frontendPort"));
-			const rootDir = __dirname.substr(0, __dirname.lastIndexOf("backend")) + "frontend/dist/";
-			const rootDirAssets = __dirname.substr(0, __dirname.lastIndexOf("backend")) + "frontend/app/";
-			const rootDirLocales = __dirname.substr(0, __dirname.lastIndexOf("backend"));
-
-			app.get("/locales/*", (req, res) => {
-				let path = req.path;
-				console.log(rootDirLocales, path, rootDirLocales + path);
-				fs.access(rootDirLocales + path, function(err) {
-					console.log("Error: ", !!err);
-					if (!err) {
-						res.sendFile(rootDirLocales + path);
-					} else {
-						res.redirect("/");
-					}
-				});
+let initialized = false;
+function initialize() {
+	if (!initialized) initialized = true;
+	else return;
+	async.waterfall([
+
+		// setup our translation
+		(next) => {
+			currentComponent = 'Translation';
+			i18n.init(next);
+		},
+
+		// setup our Redis cache
+		(next) => {
+			currentComponent = 'Cache';
+			cache.init(config.get('redis').url, config.get('redis').password, errorCb, () => {
+				next();
 			});
+		},
+
+		// setup our MongoDB database
+		(next) => {
+			initializedComponents.push(cache);
+			currentComponent = 'DB';
+			db.init(config.get("mongo").url, errorCb, next);
+		},
+
+		// setup the express server
+		(next) => {
+			initializedComponents.push(db);
+			currentComponent = 'App';
+			app.init(next);
+		},
+
+		// setup the mail
+		(next) => {
+			initializedComponents.push(app);
+			currentComponent = 'Mail';
+			mail.init(next);
+		},
+
+		// setup the socket.io server (all client / server communication is done over this)
+		(next) => {
+			initializedComponents.push(mail);
+			currentComponent = 'IO';
+			io.init(next);
+		},
+
+		// setup the punishment system
+		(next) => {
+			initializedComponents.push(io);
+			currentComponent = 'Punishments';
+			punishments.init(next);
+		},
+
+		// setup the notifications
+		(next) => {
+			initializedComponents.push(punishments);
+			currentComponent = 'Notifications';
+			notifications.init(config.get('redis').url, config.get('redis').password, errorCb, next);
+		},
+
+		// setup the stations
+		(next) => {
+			initializedComponents.push(notifications);
+			currentComponent = 'Stations';
+			stations.init(next)
+		},
+
+		// setup the songs
+		(next) => {
+			initializedComponents.push(stations);
+			currentComponent = 'Songs';
+			songs.init(next)
+		},
+
+		// setup the playlists
+		(next) => {
+			initializedComponents.push(songs);
+			currentComponent = 'Playlists';
+			playlists.init(next)
+		},
+
+		// setup the API
+		(next) => {
+			initializedComponents.push(playlists);
+			currentComponent = 'API';
+			api.init(next)
+		},
+
+		// setup the logger
+		(next) => {
+			initializedComponents.push(api);
+			currentComponent = 'Logger';
+			logger.init(next)
+		},
+
+		// setup the tasks system
+		(next) => {
+			initializedComponents.push(logger);
+			currentComponent = 'Tasks';
+			tasks.init(next)
+		},
+
+		// setup the frontend for local setups
+		(next) => {
+			initializedComponents.push(tasks);
+			currentComponent = 'Windows';
+			if (!config.get("isDocker")) {
+				const express = require('express');
+				const app = express();
+				app.listen(config.get("frontendPort"));
+				const rootDir = __dirname.substr(0, __dirname.lastIndexOf("backend")) + "frontend/dist/";
+				const rootDirAssets = __dirname.substr(0, __dirname.lastIndexOf("backend")) + "frontend/app/";
+				const rootDirLocales = __dirname.substr(0, __dirname.lastIndexOf("backend"));
+
+				app.get("/locales/*", (req, res) => {
+					let path = req.path;
+					console.log(rootDirLocales, path, rootDirLocales + path);
+					fs.access(rootDirLocales + path, function (err) {
+						console.log("Error: ", !!err);
+						if (!err) {
+							res.sendFile(rootDirLocales + path);
+						} else {
+							res.redirect("/");
+						}
+					});
+				});
 
-			app.get("/assets/*", (req, res) => {
-				const path = req.path;
-				console.log(rootDirAssets, path, rootDirAssets + path);
-				fs.access(rootDirAssets + path, function(err) {
-					console.log("Error: ", !!err);
-					if (!err) {
-						res.sendFile(rootDirAssets + path);
-					} else {
-						res.redirect("/");
-					}
+				app.get("/assets/*", (req, res) => {
+					const path = req.path;
+					console.log(rootDirAssets, path, rootDirAssets + path);
+					fs.access(rootDirAssets + path, function (err) {
+						console.log("Error: ", !!err);
+						if (!err) {
+							res.sendFile(rootDirAssets + path);
+						} else {
+							res.redirect("/");
+						}
+					});
 				});
-			});
 
-			app.get("/*", (req, res) => {
-				const path = req.path;
-				fs.access(rootDir + path, function(err) {
-					if (!err) {
-						res.sendFile(rootDir + path);
-					} else {
-						res.sendFile(rootDir + "index.html");
-					}
+				app.get("/*", (req, res) => {
+					const path = req.path;
+					fs.access(rootDir + path, function (err) {
+						if (!err) {
+							res.sendFile(rootDir + path);
+						} else {
+							res.sendFile(rootDir + "index.html");
+						}
+					});
 				});
-			});
+			}
+			if (lockdownB) return;
+			next();
 		}
-		if (lockdownB) return;
-		next();
-	}
-], (err) => {
-	if (err && err !== true) {
-		lockdown();
-		logToDiscord("An error occurred while initializing the backend server.", "#FF0000", "Startup error", true, [{name: "Error:", value: err, inline: false}, {name: "Component:", value: currentComponent, inline: true}]);
-		console.error('An error occurred while initializing the backend server');
-	} else {
-		logToDiscord("The backend server started successfully.", "#00AA00", "Startup", false, []);
-		console.info('Backend server has been successfully started');
-	}
-});
+	], (err) => {
+		if (err && err !== true) {
+			lockdown();
+			logToDiscord("An error occurred while initializing the backend server.", "#FF0000", "Startup error", true, [{
+				name: "Error:",
+				value: err,
+				inline: false
+			}, {name: "Component:", value: currentComponent, inline: true}]);
+			console.error('An error occurred while initializing the backend server');
+		} else {
+			logToDiscord("The backend server started successfully.", "#00AA00", "Startup", false, []);
+			console.info('Backend server has been successfully started');
+		}
+	});
+}

+ 1 - 1
backend/logic/i18n.js

@@ -16,7 +16,7 @@ module.exports = {
 			.use(Backend)
 			.init({
 				lng: "en",
-				debug: true,
+				debug: false,
 				fallbackLng: "en",
 				referenceLng: "en",
 				backend: {

+ 0 - 1
backend/package.json

@@ -28,7 +28,6 @@
     "mongoose": "^4.9.0",
     "oauth": "^0.9.14",
     "passport": "^0.3.2",
-    "passport-discord": "^0.1.1",
     "passport-github": "^1.1.0",
     "passport-local": "^1.0.0",
     "passport.socketio": "^3.7.0",