-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
69 lines (60 loc) · 2.33 KB
/
plugin.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
var format = require('util').format;
var path = require('path');
var dbLogger = {
role: 'dblogger',
requiresRoles: ['dbcore'],
init: function(client, imports) {
const knex = imports.dbcore.knex;
var dbLoggerPromise = knex.migrate.latest({
tableName: 'tennu_dblogger_knex_migrations',
directory: path.join(__dirname, 'migrations')
}).then(function() {
return require('./lib/logger')(knex);
});
const handleMessage = function(IRCMessage) {
if (IRCMessage.command === 'notice') {
if (!IRCMessage.nickname) {
IRCMessage.nickname = IRCMessage.channel;
}
}
else if (IRCMessage.command === 'part' || IRCMessage.command === 'quit') {
if (!IRCMessage.reason) {
IRCMessage.message = IRCMessage.reason;
}
}
else if (IRCMessage.command === 'kick') {
IRCMessage.message = format('%s kicked %s for %s', IRCMessage.kicker, IRCMessage.kicked, IRCMessage.reason);
}
var normalizedMessage = (IRCMessage.message || null);
return dbLoggerPromise.then(function(logger) {
return logger.addMessage(IRCMessage.nickname, normalizedMessage, IRCMessage.command, IRCMessage.channel).then(function() {
// Supress returning the ID of the inserted record
return;
});
})
}
var handleTopic = function(IRCMessage) {
return dbLoggerPromise.then(function(logger) {
return logger.addTopic(IRCMessage.topic, IRCMessage.nickname, IRCMessage.channel).then(function() {
// Supress returning the ID of the inserted record
return;
})
});
}
return {
handlers: {
'privmsg': handleMessage,
'notice': handleMessage,
'join': handleMessage,
'part': handleMessage,
'quit': handleMessage,
'kick': handleMessage,
'topic': handleTopic
},
exports: {
dbLoggerPromise: dbLoggerPromise
}
};
}
};
module.exports = dbLogger;