-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodules.js
113 lines (107 loc) · 3.35 KB
/
modules.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
const fs = require('fs')
const MODULE_DIR = './modules'
function loadModule (self, bot, file) {
const curr = require(file)
if (curr.commands) {
self.addCommands(bot, curr.commands)
}
if (curr.events) {
self.addEvents(bot, curr.events)
}
if (curr.onload) {
bot.log('debug', `Running onload function for ${file}`)
curr.onload(bot)
}
}
function enumerateModules (dir) {
return new Promise((resolve, reject) => {
fs.readdir(dir, function (err, files) {
if (err) { reject(err) } else { resolve(files.filter(name => name.substr(-3) === '.js')) }
})
})
}
const self = module.exports = {
loadAllModules: async function (bot, dir) {
dir = dir || MODULE_DIR // default param
// clear existing:
bot.events = {}
bot.modules = [] // informational only
bot.commands = []
const modules = await enumerateModules(dir)
await Promise.all(modules.map(name => {
return self.loadModule(bot, `${dir}/${name}`)
.then(() => bot.modules.push(name))
.catch((err) => {
bot.log('error', `Loading ${name} failed`)
bot.say(bot.config.get('irc.control'), `Failed to load ${name}`)
if (err.code === 'MODULE_NOT_FOUND') {
const npmModule = err.message.split('\'')[1]
bot.log('error', `Dependency missing: run \`npm install --save ${npmModule}\``)
} else {
bot.log('error', err)
bot.log('error', err.stack)
}
})
}))
},
loadModule: function (bot, file) {
const loader = () => loadModule(this, bot, file)
return Promise.resolve(this.unloadModule(bot, file))
.catch(err => {
bot.log('error', `Error in ${file} unload ${err}`)
bot.say(bot.config.get('irc.control'), `Failed to unload ${file}`)
return loader()
})
.then(loader)
},
unloadModule: function (bot, file) {
// check if module already loaded:
const filename = require.resolve(file)
if (filename in require.cache) {
const alreadyLoadedModule = require(file)
if (alreadyLoadedModule.onunload) {
try {
return alreadyLoadedModule.onunload(bot)
} catch (e) {
// output error, but carry on anyway
bot.log('error', `Error running ${file} onunload hook`)
bot.log('error', e)
bot.log('error', e.stack)
}
}
} else {
// no module loaded -- don't bother unloading
return
}
// delete the the module from cache:
delete require.cache[filename]
},
addCommands: function (bot, commands, clobber) {
const keys = Object.keys(commands)
for (let i = 0; i < keys.length; i++) {
const cmd = commands[keys[i]]
if (bot.commands[keys[i]] && clobber) {
bot.log('debug', 'Won\'t clobber: ' + keys[i])
continue
}
bot.commands[keys[i]] = cmd
bot.log('debug', 'Added command: ' + keys[i])
if (cmd.aliases) {
for (let j = 0; j < cmd.aliases.length; j++) {
bot.commands[cmd.aliases[j]] = commands[keys[i]]
bot.log('silly', `Aliased ${cmd.aliases[j]} to ${keys[i]}`)
}
}
}
},
addEvents: function (bot, events) {
const keys = Object.keys(events)
for (let i = 0; i < keys.length; i++) {
const x = keys[i]
if (!bot.events[x]) {
bot.events[x] = []
}
bot.events[x].push(events[x])
}
}
}