forked from mrhso/LilyWhiteBot
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
168 lines (148 loc) · 5.29 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
/**
* LilyWhiteBot
* https://github.com/mrhso/LilyWhiteBot
*
* @author vjudge1, Ishisashi
* @description
*/
'use strict';
const winston = require('winston');
const {Context, Message} = require('./lib/handlers/Context.js');
const { loadConfig, checkDeprecatedConfig } = require('./lib/util.js');
const allHandlers = new Map([
['IRC', 'IRCMessageHandler'],
['Telegram', 'TelegramMessageHandler'],
['QQ', 'QQHttpApiMessageHandler'],
['Discord', 'DiscordMessageHandler']
]);
// 所有擴充套件包括傳話機器人都只與該物件打交道
const pluginManager = {
handlers: new Map(),
handlerClasses: new Map(),
config: {},
global: {
Context,
Message,
},
plugins: {},
};
// 日志初始化
const logFormat = winston.format(info => {
info.level = info.level.toUpperCase();
if (info.stack) {
info.message = `${info.message}\n${info.stack}`;
}
return info;
});
winston.add(new winston.transports.Console({
format: winston.format.combine(
logFormat(),
winston.format.colorize(),
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
winston.format.printf(info => `${info.timestamp} [${info.level}] ${info.message}`)
),
}));
process.on('unhandledRejection', (reason, promise) => {
promise.catch(e => {
winston.error('Unhandled Rejection: ', e);
});
});
process.on('uncaughtException', (err, origin) => {
winston.error(`Uncaught exception:`, err);
});
process.on('rejectionHandled', promise => {
// 忽略
});
const config = loadConfig('config');
if (config === null) {
winston.error('No config file found. Exit.');
process.exit(1);
}
// 日志等级、文件设置
if (config.logging && config.logging.level) {
winston.level = config.logging.level;
} else {
winston.level = 'info';
}
if (config.logging && config.logging.logfile) {
const files = new winston.transports.File({
filename: config.logging.logfile,
format: winston.format.combine(
logFormat(),
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
winston.format.printf(info => `${info.timestamp} [${info.level}] ${info.message}`)
)
});
winston.add(files);
}
// 欢迎信息
winston.info('LilyWhiteBot: Multi-platform message transport bot.');
winston.info(`Version: ${require('./package.json').version}`);
winston.info();
checkDeprecatedConfig(config, 'QQ.host', 'cqsocketapi is no longer maintained and will be removed in the next version, please use CoolQ HTTP API (https://cqhttp.cc/) instead.');
checkDeprecatedConfig(config, 'QQ.port');
checkDeprecatedConfig(config, 'QQ.qq', 'Move to QQ.bot.qq');
checkDeprecatedConfig(config, 'QQ.apiRoot', 'Move to QQ.bot.apiRoot');
checkDeprecatedConfig(config, 'QQ.accessToken', 'Move to QQ.bot.accessToken');
checkDeprecatedConfig(config, 'QQ.secret', 'Move to QQ.bot.secret');
checkDeprecatedConfig(config, 'QQ.listen', 'Move to QQ.bot.listen');
checkDeprecatedConfig(config, 'QQ.options.CoolQAirA', 'No longer used.');
checkDeprecatedConfig(config, 'Telegram.options.proxy', 'Moved to Telegram.bot.proxy');
checkDeprecatedConfig(config, 'Telegram.options.webhook', 'Moved to Telegram.bot.webhook');
checkDeprecatedConfig(config, 'Telegram.options.apiRoot', 'Moved to Telegram.bot.apiRoot');
checkDeprecatedConfig(config, 'Telegram.options.noCheckCertificate', 'No longer used, use NODE_TLS_REJECT_UNAUTHORIZED=0 instead.');
checkDeprecatedConfig(config, 'transport.options.hidenick');
checkDeprecatedConfig(config, 'transport.options.servemedia.coolqCache');
checkDeprecatedConfig(config, 'transport.options.servemedia.legacy');
checkDeprecatedConfig(config, 'transport.options.servemedia.webp2png', 'No longer used.');
checkDeprecatedConfig(config, 'transport.options.servemedia.webpPath', 'No longer used.');
if (config.QQ && !config.QQ.apiRoot && !(config.QQ.bot && config.QQ.bot.apiRoot)) {
allHandlers.set('QQ', 'QQSocketApiMessageHandler');
}
// 启动各机器人
let enabledClients = [];
for (let type of allHandlers.keys()) {
if (config[type] && !config[type].disabled) {
enabledClients.push(type);
}
}
winston.info(`Enabled clients: ${enabledClients.join(', ')}`);
for (let client of enabledClients) {
winston.info(`Starting ${client} bot...`);
const options = config[client];
const Handler = require(`./lib/handlers/${allHandlers.get(client)}.js`);
const handler = new Handler(options);
handler.start();
pluginManager.handlers.set(client, handler);
pluginManager.handlerClasses.set(client, {
object: Handler,
options: options
});
winston.info(`${client} bot has started.`);
}
/**
* 載入擴充套件
*/
winston.info();
winston.info(`Loading plugins...`);
pluginManager.config = config;
for (let plugin of config.plugins) {
try {
winston.info(`Loading plugin: ${plugin}`);
let p = require(`./plugins/${plugin}.js`)(pluginManager, config[plugin] || {});
if (p) {
pluginManager.plugins[plugin] = p;
} else {
pluginManager.plugins[plugin] = true;
}
} catch (ex) {
winston.error(`Error while loading plugin ${plugin}: `, ex);
}
}
if (!config.plugins || config.plugins.length === 0) {
winston.info('No plugins loaded.');
}