forked from pokemontrades/porygon-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
159 lines (147 loc) · 6.19 KB
/
bot.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
'use strict';
const _ = require('lodash');
const Promise = require('bluebird');
var config = require('./config');
var db = require('./services/db');
var commands = require('./commands');
const tasks = require('./tasks');
const warn = _.memoize(console.warn);
const bot = require('./services/irc').setUp(config.irc);
if (!config.db) {
// Old config. Maybe we should give the user an option to rewrite the config
console.error("Config format has changed, please reformat");
process.exit(1);
}
if (config.db.enabled) {
db.setUp(config.db, commands);
} else {
console.log("The following modules, which require database connectivity, have been disabled: ["+db.listModules().join(", ")+"]");
}
function outputResponse(target, messages) {
if (!messages) {
return;
}
if (typeof messages === 'string') {
bot.say(target, messages);
} else if (Array.isArray(messages)) {
for (let i = 0; i < messages.length; i++) {
outputResponse(target, messages[i]);
}
} else if (_.isObject(messages) && typeof messages.then === 'function') {
messages.then(function (results) {
outputResponse(target, results);
}, function (error) {
handleError(target, error);
});
} else if (typeof messages === 'object' && ('response_type' in messages)) {
if ('target' in messages) {
target = messages['target'];
}
switch (messages['response_type']) {
case 'text':
bot.say(target, messages['message']);
break;
case 'action':
bot.action(target, messages['message']);
break;
default:
console.log("Message containing invalid `response_type` passed to outputResponse()");
}
} else {
throw 'Invalid `messages` argument passed to outputResponse()';
}
}
function defaultAllow ({isPM, isMod, isAuthenticated}) { // The default allow() function that gets used for a command if allow() is not provided
return !isPM || isMod && isAuthenticated;
}
// Main listener for channel messages/PMs
function executeCommands (event, author, channel, text) {
let isPM = channel === bot.nick;
let target = isPM ? author : channel;
for (let i in commands[event]) {
let message_match = (commands[event][i].message_regex || /.*/).exec(text);
let author_match = (commands[event][i].author_regex || /.*/).exec(author);
if (message_match && author_match && author !== bot.nick && (isPM || checkEnabled(channel, i, config.irc.channels[channel]))) {
Promise.join(checkIfUserIsMod(author), checkAuthenticated(author), (isMod, isAuthenticated) => {
if ((commands[event][i].allow || defaultAllow)({isPM, isMod, isAuthenticated})) {
outputResponse(target, commands[event][i].response({bot, message_match, author_match, channel, isMod, isAuthenticated, eventType: event, isPM}));
} else if (config.debug) {
outputResponse(target, "You are not authorised to run that command");
}
}).catch(_.partial(handleError, target));
}
}
}
function handleError (target, error) {
if (error.error_message) {
outputResponse(target, error.error_message);
}
if (_.isError(error)) {
console.error(error);
}
}
function checkIfUserIsMod (username) { // Returns a Promise that will resolve as true if the user is in the mod database, and false otherwise
if (!config.db.enabled || db.conn == null) {
return Promise.resolve(true);
}
return db.conn.query('SELECT * FROM User U JOIN Alias A ON U.UserID = A.UserID WHERE A.Alias = ? AND A.isNick = TRUE', [username]).then(res => !!res.length);
}
function checkAuthenticated (username) { // Returns a Promise that will resolve as true if the user is identified, and false otherwise
bot.say('NickServ', `STATUS ${username}`);
var awaitResponse = () => new Promise(resolve => {
bot.once('notice', (nick, to, text) => {
if (nick === 'NickServ' && to === bot.nick && text.indexOf(`STATUS ${username} `) === 0) {
resolve(text.slice(-1) === '3');
} else { // The notice was something unrelated, set up the listener again
resolve(awaitResponse());
}
});
});
return awaitResponse().timeout(5000, 'Timed out waiting for NickServ response');
}
function checkEnabled (channelName, itemName, itemConfig) {
if (itemConfig === undefined) {
warn(`Warning: No channel-specific configuration found for the channel ${channelName}. All commands on this channel will be ignored.`);
return false;
}
if (_.isBoolean(itemConfig)) {
return itemConfig;
}
if (_.isRegExp(itemConfig)) {
return itemConfig.test(itemName);
}
if (_.isArray(itemConfig)) {
return _.includes(itemConfig, itemName);
}
if (_.isString(itemConfig)) {
return itemConfig === itemName;
}
if (_.isFunction(itemConfig)) {
return !!itemConfig(itemName);
}
warn(`Warning: Failed to parse channel-specific configuration for the channel ${channelName}. All commands on this channel will be ignored.`);
return false;
}
bot.on('error', console.error);
bot.on('message', _.partial(executeCommands, 'message'));
bot.on('join', (chan, user) => executeCommands('join', user, chan));
bot.on('action', _.partial(executeCommands, 'action'));
bot.on('+mode', (chan, by, mode, argument) => executeCommands(`mode +${mode}`, by, chan, argument));
bot.on('-mode', (chan, by, mode, argument) => executeCommands(`mode -${mode}`, by, chan, argument));
function executeTask(taskName) {
const params = tasks[taskName];
const iteratee = params.concurrent ? params.task : _.once(params.task);
_.forOwn(config.irc.tasks, (channelConfig, channel) => {
if (checkEnabled(channel, taskName, channelConfig)) {
outputResponse(channel, iteratee({bot, channel: params.concurrent ? channel : null}));
}
});
}
bot.once('join', () => {
_.forOwn(tasks, (params, taskName) => {
if (params.onStart) {
executeTask(taskName);
}
setInterval(executeTask, params.period * 1000, taskName);
});
});