forked from cheald/memebot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
94 lines (79 loc) · 2.12 KB
/
index.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
"use strict";
const config = require("./config");
const IRC = require("irc");
const winston = require("winston");
const util = require("util");
// https://github.com/winstonjs/winston/tree/2.x
winston.configure({
transports: [
new winston.transports.Console({ colorize: 'all' }),
]
});
var d = require("domain").create();
var bot;
d.on("error", e => winston.error(util.inspect(e)));
const commands = new Map();
let BOT_TRIGGER_RE = null;
function handleCommand(from, to, text, message) {
let isMatch = false;
let rest = null;
if (to.trim() === bot.nick.trim()) {
isMatch = true;
rest = message.args[1];
} else {
const matches = message.args[1].match(BOT_TRIGGER_RE);
if (matches) {
isMatch = true;
rest = matches[1];
}
}
if (isMatch) {
const parts = rest.trim().split(/\s+/);
const key = parts[0].toLowerCase();
if (commands.has(key)) {
commands.get(key)(bot, parts, from, to);
return true;
}
}
return false;
}
function start() {
bot = new IRC.Client(config.server, config.botName, {
channels: config.channels,
retryDelay: 10000,
debug: true
});
setInterval(() => {
if (bot.nick.trim() !== config.botName.trim()) {
winston.info("renaming:", bot.nick, "!=", config.botName);
bot.send("NICK", config.botName);
}
}, 10000);
bot.addListener("message", (from, to, text, message) => {
d.run(() => {
if (handleCommand(from, to, text, message)) {
return;
}
});
});
bot.addListener("error", message => winston.error(message));
bot.addListener("registered", () => {
BOT_TRIGGER_RE = new RegExp(`^${bot.nick}[: ]+(.*)`);
if (config.password) {
bot.say("NickServ", "IDENTIFY " + config.password);
}
});
var normalizedPath = require("path").join(__dirname, "modules");
require("fs")
.readdirSync(normalizedPath)
.forEach(function(file) {
if (file.match(/\.js$/)) {
console.log( "Loading module " + file );
let module = require("./modules/" + file);
if (module.setup) {
module.setup(bot, commands);
}
}
});
}
start();