56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
import fs from 'fs';
|
|
import { join } from 'path';
|
|
import CreateBot from '#root/bot/index.js';
|
|
import CreateWebhookServer from '#root/http/index.js';
|
|
import logger from '#root/utils/logger.js';
|
|
import handlers from './handlers';
|
|
|
|
let retryCount = 0;
|
|
|
|
(async function start(port) {
|
|
if (retryCount >= 5) {
|
|
logger.warning('重试次数已达上限,请联系管理员重新启动本服务');
|
|
return;
|
|
}
|
|
|
|
logger('start server!!');
|
|
const bot = new CreateBot();
|
|
|
|
logger('bot 连接中...');
|
|
await bot
|
|
.open({
|
|
baseUrl: process.env.MIRAI_HTTP_API_HOST,
|
|
verifyKey: process.env.MIRAI_HTTP_API_VERIFY_KEY,
|
|
qq: process.env.QQ,
|
|
})
|
|
.catch((e) => {
|
|
retryCount++;
|
|
logger.error(`bot 连接失败,${e}。正在重试, 重试次数 ${retryCount}`);
|
|
|
|
setTimeout(() => {
|
|
start();
|
|
}, 5000);
|
|
return Promise.reject();
|
|
});
|
|
logger('bot 连接成功!!');
|
|
|
|
logger('开启 webhook 服务器');
|
|
const webhook = new CreateWebhookServer();
|
|
|
|
logger('开始引入handler函数');
|
|
// const files = fs.readdirSync('./handlers');
|
|
// const handlers = files
|
|
// .filter((it) => /\.js$/.test(it))
|
|
// .map((it) => {
|
|
// let fPath = join(__dirname, './handlers', it);
|
|
// return {
|
|
// event: it.split('.').slice(0, -1).join('.'),
|
|
// handler: require(fPath).bind(this, bot),
|
|
// };
|
|
// });
|
|
webhook.registerHandler(handlers.map(i => ({ event: i.event, handler: i.handler.bind(this, bot) })));
|
|
|
|
webhook.startListen(port);
|
|
logger(`开始监听端口: ${port}`);
|
|
})(process.env.WEB_HOOK_PORT);
|