-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginman.js
159 lines (128 loc) · 3.5 KB
/
pluginman.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
var fs = require('fs');
var u = require('./cmdutil');
var m = {
loaded: {},
load: function(text, con, msg) {
if (u.notadmin(msg)) return;
var args = u.args(text);
// We must have at least one plugin to load
if (args.length === 0) {
return;
}
// If the first argument is 'all', load everything
if (args[0] === 'all') {
return m.loadall(con);
}
// Otherwise, load every name in the list
args.forEach(function(arg) {
m.loadplugin(arg, con);
});
},
unload: function(text, con, msg) {
if (u.notadmin(msg)) return;
var args = u.args(text);
// We must have at least one plugin to unload
if (args.length === 0) {
return;
}
// If the first argument is 'all', unload everything
if (args[0] === 'all') {
return m.unloadall(con);
}
// Otherwise, load every name in the list
args.forEach(function(arg) {
m.unloadplugin(arg, con);
});
},
reload: function(text, con, msg) {
if (u.notadmin(msg)) return;
var args = u.args(text);
// We must have at least one plugin to reload
if (args.length === 0) {
return;
}
// If the first argument is 'all', reload everything
if (args[0] === 'all') {
return m.reloadall(con);
}
// Otherwise, reload every name in the list
args.forEach(function(arg) {
m.reloadplugin(arg, con);
});
},
loadplugin: function(file, con) {
// Idempotence
if (m.loaded[file]) {
return false;
}
// Make sure the file is valid and exists
if (file[0] === '#' || file[0] === '.' || !fs.existsSync('./plugins/' + file)) {
return false;
}
// Then load it
var plug = require('./plugins/' + file);
// Register chat listeners
if (plug.listeners) {
Object.keys(plug.listeners).forEach(function(key) {
con.on(key, plug.listeners[key]);
});
}
// Register commands
if (plug.commands) {
Object.keys(plug.commands).forEach(function(key) {
con.on('!' + key, plug.commands[key]);
});
}
// Store in the loaded plugins map
m.loaded[file] = plug;
con.info('Plugin ' + file + ' loaded.');
return true;
},
unloadplugin: function(file, con) {
// Idempotence
if (!m.loaded[file]) {
return false;
}
// Grab the plugin object
var plug = m.loaded[file];
// Unregister chat listeners
if (plug.listeners) {
Object.keys(plug.listeners).forEach(function(key) {
con.removeListener(key, plug.listeners[key]);
});
}
// Unregister commands
if (plug.commands) {
Object.keys(plug.commands).forEach(function(key) {
con.removeListener('!' + key, plug.commands[key]);
});
}
// Remove from plugin map, and remove from cache
m.loaded[file] = false;
delete require.cache[require.resolve('./plugins/' + file)];
con.info('Plugin ' + file + ' unloaded.');
return true;
},
reloadplugin: function(file, con) {
return m.unloadplugin(file, con) && m.loadplugin(file, con);
},
loadall: function(con) {
//con.info('Loading plugins...');
fs.readdir('./plugins', function(err, files) {
files.forEach(function(file) {
m.loadplugin(file, con);
});
//con.info('Done');
});
},
unloadall: function(con) {
Object.keys(m.loaded).forEach(function(file) {
m.unloadplugin(file, con);
});
},
reloadall: function(con) {
m.unloadall(con);
m.loadall(con);
}
};
module.exports = m;