dashboard-core/index.js

118 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2023-06-22 19:36:52 +05:00
const EventEmitter = require("events"),
express = require("express"),
app = express(),
session = require("express-session"),
bodyParser = require("body-parser"),
partials = require("express-partials"),
router = require("./router"),
initServer = require("./InitFunctions/initServer");
2023-06-19 13:57:11 +05:00
2023-06-22 19:36:52 +05:00
const version = require("./package.json").version;
const DBDEvents = new EventEmitter();
2023-06-19 13:57:11 +05:00
class Dashboard {
constructor(config) {
2023-06-22 19:36:52 +05:00
const notSetYetAndRequired = [];
if (!config.port) notSetYetAndRequired.push("port");
if (!config.theme) notSetYetAndRequired.push("theme");
if (!config.client) notSetYetAndRequired.push("client");
if (!config.redirectUri) notSetYetAndRequired.push("redirectUri");
if (!config.bot) notSetYetAndRequired.push("bot");
if (!config.settings) notSetYetAndRequired.push("settings");
if (!config.domain) notSetYetAndRequired.push("domain");
if (notSetYetAndRequired[0]) throw new Error(`You need to define some more things: ${notSetYetAndRequired.join(", ")}.`);
2023-06-19 13:57:11 +05:00
this.config = config;
this.modules = [];
}
async init() {
2023-06-22 19:36:52 +05:00
const config = this.config,
modules = this.modules;
app.use(bodyParser.urlencoded({ extended: true }));
2023-06-19 13:57:11 +05:00
app.use(bodyParser.json());
app.use(partials());
2023-06-22 19:36:52 +05:00
app.use(express.static(config.theme.staticPath));
app.use("/", express.static(config.theme.staticPath));
app.use("/:a/", express.static(config.theme.staticPath));
app.use("/:a/:b/", express.static(config.theme.staticPath));
app.use("/:a/:b/:c/", express.static(config.theme.staticPath));
app.use("/:a/:b/:c/:d/", express.static(config.theme.staticPath));
app.set("views", config.theme.viewsPath);
app.set("view engine", "ejs");
const sessionData = {
secret: config.cookiesSecret || "total_secret_cookie_secret",
2023-06-19 13:57:11 +05:00
resave: true,
saveUninitialized: true,
cookie: {
2023-06-24 19:38:15 +05:00
maxAge: 3600000,
2023-06-19 13:57:11 +05:00
},
};
2023-06-22 19:36:52 +05:00
2023-06-19 13:57:11 +05:00
config.sessionSaveSession ? sessionData.store = config.sessionSaveSession : null;
app.use(session(sessionData));
2023-06-22 19:36:52 +05:00
const themeConfig = config.theme.themeConfig;
2023-06-19 13:57:11 +05:00
2023-06-22 19:36:52 +05:00
app.get("*", (req, res, next) => {
DBDEvents.emit("websiteView", req.session.user ? req.session.user : { loggedIn: false });
2023-06-19 13:57:11 +05:00
next();
});
app.use((req, res, next) => {
req.DBDEvents = DBDEvents;
2023-06-22 19:36:52 +05:00
if (req.session.loggedInLastTime == true) {
2023-06-19 13:57:11 +05:00
req.displayLoggedInInfo = true;
req.session.loggedInLastTime = false;
}
if (!req.body) req.body = {};
req.client = config.client;
req.redirectUri = config.redirectUri;
req.themeConfig = themeConfig;
req.botToken = config.bot.token;
req.guildAfterAuthorization = config.guildAfterAuthorization || {};
2023-06-22 19:36:52 +05:00
req.websiteTitle = config.websiteTitle || "Discord Bot Dashboard";
req.iconUrl = config.iconUrl || "https://www.nomadfoods.com/wp-content/uploads/2018/08/placeholder-1-e1533569576673.png";
2023-06-19 13:57:11 +05:00
req.app = app;
req.config = config;
next();
});
2023-06-22 19:36:52 +05:00
router(app, config, themeConfig, modules);
2023-06-19 13:57:11 +05:00
this.app = app;
2023-06-22 19:36:52 +05:00
const sio = initServer(app, config, themeConfig, modules);
2023-06-19 13:57:11 +05:00
this.server = sio.server;
this.io = sio.io;
}
getApp() {
return this.app;
}
useThirdPartyModule(module) {
this.modules.push(module);
}
}
module.exports = {
Dashboard,
2023-06-22 19:36:52 +05:00
formTypes: require("./ModuleExportsFunctions/formTypes"),
customPagesTypes: require("./ModuleExportsFunctions/customPagesTypes"),
2023-06-19 14:21:58 +05:00
DBDEvents,
2023-06-22 19:36:52 +05:00
version,
};