Files
mol-robot/app.js
mol 83964b678d
All checks were successful
continuous-integration/drone/push Build is passing
test
2023-12-14 10:07:44 +08:00

57 lines
1.6 KiB
JavaScript

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/index.js';
import plugins from '#root/plugins/index.js';
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 连接成功!!');
bot.mountPlugin(plugins);
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);