Files
mol-robot/app.js
mol ba119d85c9
Some checks are pending
continuous-integration/drone/push Build is running
test
2023-12-14 15:33:41 +08:00

59 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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}`);
bot.sendMessageToFriend(+process.env.MASTER, '模儿bot已就绪');
})(process.env.WEB_HOOK_PORT);