diff --git a/lib/core.js b/lib/core.js index 2706c3b..eb791dd 100644 --- a/lib/core.js +++ b/lib/core.js @@ -33,17 +33,16 @@ const default_config = { ws_reverse_url: [], ws_reverse_reconnect_interval: 3000, } -const config = {}, logger = process.OICQ.logger; +const config = {}; let bot, account, dir, server, wss, online = false, websockets = new Set(), wsrCreated = false; function startup(arg) { account = arg; - dir = path.join(process.mainModule.path, "data", account.toString()); + dir = path.join(process.mainModule.path, "data", String(account)); if (!fs.existsSync(dir)) fs.mkdirSync(dir, {recursive: true, mode: 0o755}); Object.assign(config, default_config, global_config.general, global_config[account]); - logger.info("已加载配置文件:"); - console.log(config); + console.log("已加载配置文件:", config); if (config.debug && !config.log_level) config.log_level = "debug"; config.device_path = dir; @@ -72,7 +71,7 @@ function startup(arg) { } function inputPassword() { - logger.info("请输入密码:"); + console.log("请输入密码:"); process.stdin.once("data", (input)=>{ input = input.toString().trim(); const password = crypto.createHash("md5").update(input).digest(); @@ -159,7 +158,7 @@ function dipatch(event) { timeout: config.post_timeout, headers: { 'Content-Type': 'application/json', - "X-Self-ID": account.toString(), + "X-Self-ID": String(account), "User-Agent": "OneBot" } } @@ -225,10 +224,10 @@ function createServer() { }); } server.listen(config.port, config.host, ()=>{ - logger.info(`开启http服务器成功,监听${server.address().address}:${server.address().port}`); + console.log(`开启http服务器成功,监听${server.address().address}:${server.address().port}`); }).on("error", (e)=>{ - logger.error(e.message); - logger.error("开启http服务器失败。"); + console.log(e.message); + console.log("开启http服务器失败,进程退出。"); process.exit(0); }) } @@ -258,7 +257,7 @@ function onWSOpen(ws) { function createReverseWS() { wsrCreated = true; const headers = { - "X-Self-ID": account.toString(), + "X-Self-ID": String(account), "X-Client-Role": "Universal", "User-Agent": "OneBot" }; @@ -274,19 +273,19 @@ function createWSClient(url, headers) { const ws = new WebSocket(url, {headers}); ws.on("error", ()=>{}); ws.on("open", ()=>{ - logger.info(`反向ws连接(${url})连接成功。`); + bot.logger.info(`反向ws连接(${url})连接成功。`); websockets.add(ws); onWSOpen(ws); }); ws.on("close", ()=>{ - logger.error(`反向ws连接(${url})被关闭,将在${config.ws_reverse_reconnect_interval}毫秒后尝试连接。`); + bot.logger.error(`反向ws连接(${url})被关闭,将在${config.ws_reverse_reconnect_interval}毫秒后尝试连接。`); websockets.delete(ws); setTimeout(()=>{ createWSClient(url, headers); }, config.ws_reverse_reconnect_interval); }); } catch (e) { - logger.error(e.message); + bot.logger.error(e.message); } } @@ -296,7 +295,7 @@ function onHttpRes(event, res) { res.on("end", ()=>{ if (!online) return; data = Buffer.concat(data).toString(); - logger.debug(`收到HTTP响应:${res.statusCode} ` + data); + debug(`收到HTTP响应:${res.statusCode} ` + data); try { data = JSON.parse(data); api.quickOperate(event, data); @@ -313,7 +312,7 @@ async function onHttpReq(req, res) { const qop = url.parse(req.url); const action = qop.pathname.replace("/", ""); if (req.method === "GET") { - logger.debug(`收到GET请求: ` + req.url); + debug(`收到GET请求: ` + req.url); const params = querystring.parse(qop.query); try { const ret = await api.apply({action, params}); @@ -327,7 +326,7 @@ async function onHttpReq(req, res) { req.on("end", async()=>{ try { data = Buffer.concat(data).toString(); - logger.debug(`收到POST请求: ` + data); + debug(`收到POST请求: ` + data); let params; if (req.headers["content-type"] === "application/x-www-form-urlencoded") params = querystring.parse(data); @@ -347,7 +346,7 @@ async function onHttpReq(req, res) { } } async function onWSMessage(ws, data) { - logger.debug(`收到WS消息: ` + data); + debug(`收到WS消息: ` + data); if (!online) { return ws.send(JSON.stringify({ retcode: 104, status: "failed" @@ -380,4 +379,11 @@ async function onWSMessage(ws, data) { } } +function debug(msg) { + if (bot && bot.logger) + bot.logger.debug(msg); + else + console.log(msg); +} + module.exports = startup; diff --git a/main.js b/main.js index e5b6f92..bc095fc 100644 --- a/main.js +++ b/main.js @@ -15,7 +15,6 @@ try { } require("oicq"); -const logger = process.OICQ.logger; const data_dir = path.join(process.mainModule.path, "data"); try { if (!fs.existsSync(data_dir)) @@ -24,8 +23,8 @@ try { fs.writeFileSync(testfile, ""); fs.unlinkSync(testfile); } catch(e) { - logger.error("数据文件夹不可写,进程退出。"); - logger.error(e.message); + console.log("数据文件夹不可写,进程退出。"); + console.log(e.message); process.exit(0); } @@ -36,7 +35,7 @@ function inputAccount() { if (account > 10000 && account < 0xffffffff) { return require("./lib/core")(account); } - logger.info("请输入账号:"); + console.log("请输入账号:"); process.stdin.once("data", (input)=>{ account = parseInt(input.toString().trim()); inputAccount();