-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
273 lines (240 loc) · 8.4 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const express = require('express');
const session = require('express-session');
const favicon = require('serve-favicon');
const { existsSync, readdirSync } = require('fs');
const { join } = require('path');
const ejs = require('ejs');
const { EventEmitter } = require('events');
const { PermissionsBitField } = require('discord.js');
class Dashboard extends EventEmitter {
constructor(client, options) {
super();
if (+process.versions.node.split('.')[0] < 16) { throw new Error('Discord-easy-dashboard only supports node-v16+'); }
if (!client) throw new Error('Client is a required parameter.');
this.client = client;
this.app = express();
this.details = {
name: options?.name || client?.user?.username || null,
description: options?.description || null,
faviconPath: options?.faviconPath || null,
serverUrl: options?.serverUrl || null,
inviteUrl: options?.inviteUrl || null,
};
if (!client.isReady()) {
client.on(
'ready',
() =>
(this.details.name =
this.details.name === null ? this.client.user.username : this.details.name),
);
}
this._commands = [];
this._settings = [];
this.config = {
baseUrl: options?.baseUrl || 'http://localhost',
port: options?.port || 3000,
noPortIncallbackUrl: options?.noPortIncallbackUrl || false,
secret: options?.secret,
logRequests: options?.logRequests || false,
injectCSS: options?.injectCSS || null,
theme: this._getTheme(options?.theme),
permissions: options?.permissions || ['ManageGuild'],
session: options?.session || null,
};
if (!this.config.secret) {
console.warn(
'Without the client.secret parameter, some features of discord-easy-dashboard will be disabled, like Discord authentification or guild settings...',
);
}
this._setup();
this._checkRoutes();
this._loadRoutes();
this._start();
}
_getTheme(theme) {
if (!theme) return require(join(__dirname, 'themes', 'light'));
if (typeof theme === 'object') return theme;
if (!existsSync(join(__dirname, 'themes', theme))) throw new Error(`Theme ${theme} not found!`);
return require(join(__dirname, 'themes', theme));
}
_deserializePermissions(permissions) {
const permissionsInt = BigInt(permissions);
const permissionsObj = {};
// Iterate over each permission flag in the table
for (const [permissionName, permissionFlag] of Object.entries(PermissionsBitField.Flags)) {
permissionsObj[permissionName] = (permissionsInt & BigInt(permissionFlag)) !== 0n;
}
return permissionsObj;
}
_setup() {
this.app.set('port', this.config.port || 3000);
this.app.set('views', join(__dirname, 'views'));
this.app.set('view engine', 'ejs');
this.app.engine('ejs', async (path, data, cb) => {
try {
const html = await ejs.renderFile(path, data, { async: true });
cb(null, html);
}
catch (e) {
cb(e, '');
}
});
if (this.details.faviconPath) this.app.use(favicon(this.details.faviconPath));
this.app.use(express.static(join(__dirname, 'public')));
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
if (this.config.logRequests) {
const morgan = require('morgan');
this.app.use(morgan('dev'));
}
if (this.config.session) {
this.app.use(session(this.config.session));
}
else {
this.app.use(
session({
secret: `discord-easy-dashboard-${Date.now()}-${this.client.id}-${Math.random().toString(36)}`,
resave: false,
saveUninitialized: false,
}),
);
}
this.app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Credentials', true);
req.user = req.session.user;
req.dashboardConfig = this.config;
req.dashboardDetails = this.details;
req.dashboardCommands = this._commands;
req.client = this.client;
req.dashboardEmit = (...args) => this.emit(...args);
req.deserializePermissions = (permissions) => this._deserializePermissions(permissions);
req.dashboardSettings = this._settings;
next();
});
}
_loadRoutes() {
const files = readdirSync(join(__dirname, 'routes'));
const routes = files.filter((c) => c.split('.').pop() === 'js');
if (files.length === 0 || routes.length === 0) throw new Error('No routes were found!');
for (let i = 0; i < routes.length; i++) {
if (
(!this.config.secret &&
['auth.js', 'manage.js', 'selector.js'].includes(routes[i])) ||
routes[i] === 'custom.js'
) { continue; }
const route = require(`./routes/${routes[i]}`);
this.app.use(route.name, route.Router);
}
// Register 404 route for fallback
const fallback = require('./routes/custom');
this.app.use(fallback.name, fallback.Router);
}
_checkRoutes() {
// Manual checking because the structure for 404 is weird
if (!this.config.theme[404]) {
console.warn(
'No key found in the theme object for "404", falling back to the default one',
);
}
for (const routeFile of readdirSync(join(__dirname, 'routes')).filter((e) =>
e.endsWith('.js'),
)) {
if (['auth.js', 'custom.js'].includes(routeFile)) continue;
const route = require(`./routes/${routeFile}`);
let routeName;
switch (route.name) {
case '/':
routeName = 'home';
break;
case '/manage':
routeName = 'guild';
break;
default:
routeName = route.name.split('/')[1];
}
if (!this.config.theme[routeName]) {
console.warn(
`No key found in the theme object for "${route.name}", falling back to the default one`,
);
}
}
}
_start() {
try {
this.app.listen(this.app.get('port'));
}
catch (e) {
throw new Error(e);
}
}
/**
* Register a command
* @param name - The name of the command.
* @param description - A description of the command.
* @param usage - The usage string for the command.
*/
registerCommand(name, description, usage) {
this._commands.push({ name, description, usage });
}
/**
* Adds a text input to the settings page
* @param name - The name of the setting. This is the name that will be used to access the setting.
* @param description - A description of the setting.
* @param validator - A function that takes the input and returns a boolean indicating whether the
* input is valid.
* @param setter - a function that takes a value and sets the setting.
* @param getter - A function that returns the value of the setting.
*/
addTextInput(name, description, validator, setter, getter) {
this._settings.push({
name,
description,
type: 'text input',
validator,
set: setter,
get: getter,
});
}
/**
* Adds a boolean input to the settings page
* @param name - The name of the setting.
* @param description - A description of the setting.
* @param setter - a function that takes a boolean value and sets the setting to that value.
* @param getter - A function that returns the current value of the setting.
*/
addBooleanInput(name, description, setter, getter) {
this._settings.push({ name, description, type: 'boolean input', set: setter, get: getter });
}
/**
* Adds a color input to the settings page
* @param name - The name of the setting. This is the name that will be used to access the setting.
* @param description - A description of the setting.
* @param setter - a function that takes a string and sets the value of the setting.
* @param getter - A function that returns the current value of the setting.
*/
addColorInput(name, description, setter, getter) {
this._settings.push({ name, description, type: 'color input', set: setter, get: getter });
}
/**
* It adds a selector to the settings page
* @param name - The name of the setting. This is the name that will be used to access the setting.
* @param description - A description of the setting.
* @param getSelectorEntries - a function that returns an array of couples [id, value].
* @param setter - a function that takes a value and sets the setting.
* @param getter - A function that returns the current value of the setting.
*/
addSelector(name, description, getSelectorEntries, setter, getter) {
this._settings.push({
name,
description,
type: 'selector',
getSelectorEntries,
set: setter,
get: getter,
});
}
}
module.exports = Dashboard;