-
Notifications
You must be signed in to change notification settings - Fork 2
/
core.js
118 lines (101 loc) · 3.46 KB
/
core.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
var fs = require('fs');
// Core functions for loading and reloading modules
module.exports = (function()
{
var core =
{
// Define core variables
client: false,
secrets: false,
loaded: {},
modules: [],
init: function(client, modules, secrets)
{
core.secrets = secrets;
// Only set the client if it hasn't been initialized yet
if(!core.client)
core.client = client;
// Get core modules
var core_modules = require("./config/core.js");
// Loop through core modules
for(var i = 0, l = core_modules.length; i < l; i++)
{
core.modules.push({type: 'core', name: core_modules[i]});
}
// Loop through all non-essential modules
for(var i = 0, l = modules.length; i < l; i++)
{
core.modules.push({type: 'modules', name: modules[i]});
}
// Now loop through all modules and load them
for(var i = 0, l = core.modules.length; i < l; i++)
{
core.load(core.modules[i]);
}
},
load: function(module)
{
var path = "./"+module.type+"/"+module.name+".js";
// Make sure the module exists!
if(fs.existsSync(path))
{
// Create a unique ID for the module based on it's type and name
var module_id = module.type + "/" + module.name;
var module;
// Now require the module
try
{
module = require(path);
}
catch(error)
{
console.log("Error: The module failed to require!", error);
}
core.loaded[module_id] = module;
// And check if it has a load function
if(typeof core.loaded[module_id].load == "function")
{
core.loaded[module_id].load(core.client, core);
}
else
{
console.log("Warning: Module '"+module.name+"' cannot be loaded!");
}
}
else
{
console.log("Warning: Module '"+module.name+"' does not exist!");
}
},
unload: function(module)
{
var module_id = module.type + "/" + module.name;
// Make sure this module is actually loaded
if(typeof core.loaded[module_id] != "undefined")
{
// Does this module have an unload function?
if(typeof core.loaded[module_id].unload == "function")
{
core.loaded[module_id].unload(core.client, core);
}
else
{
console.log("Warning: Module '"+module.name+"' cannot be unloaded!");
}
delete core.loaded[module];
delete require.cache[require.resolve("./"+module.type+"/"+module.name+".js")];
}
},
reload: function(module)
{
core.unload(module);
core.load(module);
}
};
return {
init: core.init,
load: core.load,
unload: core.unload,
reload: core.reload
};
})();