-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.js
204 lines (174 loc) · 5.58 KB
/
main.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* QQ-Telegram-IRC
* https://github.com/vjudge1/qq-tg-irc
*
* @author vjudge1
* @description
*/
'use strict';
const https = require('https');
const irc = require('irc');
const Telegraf = require('telegraf');
const QQBot = require('./lib/QQBot.js');
const proxy = require('./lib/proxy.js');
const {Context, Message} = require('./lib/handlers/Context.js');
const IRCMessageHandler = require('./lib/handlers/IRCMessageHandler.js');
const TelegramMessageHandler = require('./lib/handlers/TelegramMessageHandler.js');
const QQMessageHandler = require('./lib/handlers/QQMessageHandler.js');
// 所有擴充套件包括傳話機器人都只與該物件打交道
const pluginManager = {
handlers: new Map(),
handlerClasses: new Map(),
config: {},
global: {
Context,
Message,
},
plugins: {},
log: (message, isError = false) => {
let date = new Date().toISOString();
let output = `[${date.substring(0,10)} ${date.substring(11,19)}] ${message}`;
if (isError) {
console.error(output);
} else {
console.log(output);
}
},
};
/**
* 啟動機器人
*/
const config = require('./config.js');
if (config.IRC && !config.IRC.disabled) {
// 載入IRC機器人程式
let botcfg = config.IRC.bot;
pluginManager.log('Starting IRCBot...');
const ircClient = new irc.Client(botcfg.server, botcfg.nick, {
userName: botcfg.userName,
realName: botcfg.realName,
port: botcfg.port,
autoRejoin: true,
channels: botcfg.channels || [],
secure: botcfg.secure || false,
floodProtection: botcfg.floodProtection || true,
floodProtectionDelay: botcfg.floodProtectionDelay || 300,
sasl: botcfg.SASL,
password: botcfg.sasl_password,
encoding: botcfg.encoding || 'UTF-8',
autoConnect: false,
});
ircClient.on('registered', (message) => {
pluginManager.log('IRCBot Registered.');
if (botcfg.nick_password) {
ircClient.say('NickServ', `IDENTIFY ${botcfg.nick_password}`);
}
});
ircClient.on('join', (channel, nick, message) => {
if (nick === ircClient.nick) {
pluginManager.log(`IRCBot has joined channel: ${channel} as ${nick}`);
}
});
ircClient.on('error', (message) => {
pluginManager.log(`IRCBot Error: ${message.command}`, true);
});
ircClient.connect();
let options = config.IRC.options || {};
let options2 = {
maxLines: options.maxLines,
keepSilence: options.keepSilence,
};
const ircHandler = new IRCMessageHandler(ircClient, options2);
pluginManager.handlers.set('IRC', ircHandler);
pluginManager.handlerClasses.set('IRC', {
object: IRCMessageHandler,
options: options2
});
}
if (config.Telegram && !config.Telegram.disabled) {
let tgcfg = config.Telegram;
pluginManager.log('Starting TelegramBot...');
// 代理
let myAgent = https.globalAgent;
if (tgcfg.options.proxy && tgcfg.options.proxy.host) {
myAgent = new proxy.HttpsProxyAgent({
proxyHost: tgcfg.options.proxy.host,
proxyPort: tgcfg.options.proxy.port,
});
}
const tgBot = new Telegraf(tgcfg.bot.token, {
telegram: {
agent: myAgent,
},
username: tgcfg.bot.name,
});
tgBot.catch((err) => {
pluginManager.log(`TelegramBot Error: ${err.message}`, true);
});
tgBot.startPolling();
let options2 = {
botName: tgcfg.bot.name,
nickStyle: tgcfg.options.nickStyle,
keepSilence: tgcfg.options.keepSilence,
};
const telegramHandler = new TelegramMessageHandler(tgBot, options2);
pluginManager.handlers.set('Telegram', telegramHandler);
pluginManager.handlerClasses.set('Telegram', {
object: TelegramMessageHandler,
options: options2
});
pluginManager.log('TelegramBot started');
}
if (config.QQ && !config.QQ.disabled) {
let options = config.QQ.options || {};
let qqbot = new QQBot({
CoolQPro: options.CoolQPro,
host: config.QQ.host || '127.0.0.1',
port: config.QQ.port || 11235,
});
pluginManager.log('Starting QQBot...');
qqbot.on('error', (err) => {
pluginManager.log(`QQBot Error: ${err.error.toString()} (${err.event})`, true);
});
qqbot.start();
// 載入敏感詞清單
let badwords = [];
if (options.selfCensorship) {
try {
badwords = require('./badwords.js');
} catch (ex) {
pluginManager.log('Failed to load badwords.js', true);
}
}
let options2 = {
qq: config.QQ.qq,
selfCensorship: options.selfCensorship,
ignoreCash: options.ignoreCash,
badwords: badwords,
nickStyle: options.nickStyle,
CoolQPro: options.CoolQPro,
keepSilence: options.keepSilence,
};
const qqHandler = new QQMessageHandler(qqbot, options2);
pluginManager.handlers.set('QQ', qqHandler);
pluginManager.handlerClasses.set('QQ', {
object: QQMessageHandler,
options: options2,
});
pluginManager.log('QQBot started');
}
/**
* 載入擴充套件
*/
pluginManager.config = config;
for (let plugin of config.plugins) {
try {
let p = require(`./plugins/${plugin}.js`)(pluginManager, config[plugin] || {});
if (p) {
pluginManager.plugins[plugin] = p;
} else {
pluginManager.plugins[plugin] = true;
}
} catch (ex) {
pluginManager.log(`Error while loading plugin ${plugin}: ${ex.message}`, true);
}
}