-
Notifications
You must be signed in to change notification settings - Fork 0
/
Registry.js
52 lines (37 loc) · 1.26 KB
/
Registry.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
const path = require('path');
const { Endpoint } = require('./interfaces/');
const Util = require('./util/Util.js');
class Registry {
constructor(client) {
this.routes = new Map();
Object.defineProperty(this, 'client', {
value: client
});
}
async loadEndpoints() {
const directory = path.join(process.cwd(), 'endpoints');
const files = Util.readdirRecursive(directory);
const loaded = [];
for(const path of files) {
const func = require(path);
if(typeof func !== 'function') {
this.client.logger.error("Attempted to index an invalid function as a route.");
continue;
}
const route = new func(this.client);
loaded.push(await this.loadRoute(route, path));
}
return loaded;
}
async loadRoute(route, directory) {
if(this.routes.has(route.id)) {
this.client.logger.error("Attempted to reload an existing route.");
return null;
}
if(directory) route._directory = directory;
this.routes.set(route.id, route);
this.client.logger.log(`Loaded route ${route.id}`);
return route;
}
}
module.exports = Registry;