From 92e2ea2337017079d47c34dbad47dc824d576a2b Mon Sep 17 00:00:00 2001 From: synt-xerror <169557594+synt-xerror@users.noreply.github.com> Date: Mon, 16 Mar 2026 22:27:07 -0300 Subject: [PATCH] support for comments on config file --- package-lock.json | 4 +-- src/config.js | 72 +++++++++++++++++++++++++++++++++-------------- 2 files changed, 53 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index c4d6c5d..74b0cf1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "whatsapp-bot", - "version": "2.3.3", + "version": "2.3.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "whatsapp-bot", - "version": "2.3.3", + "version": "2.3.4", "dependencies": { "node-addon-api": "^7", "node-gyp": "^12.2.0", diff --git a/src/config.js b/src/config.js index 94f0a7c..f0b2407 100644 --- a/src/config.js +++ b/src/config.js @@ -1,32 +1,62 @@ import fs from "fs"; -function parseValue(v) { - v = v.trim(); +function parseConf(raw) { + // Remove comentários inline e de linha inteira, preservando estrutura + const lines = raw.split("\n"); - // lista: [a, b, c] - if (v.startsWith("[") && v.endsWith("]")) { - return v - .slice(1, -1) - .split(",") - .map(x => x.trim()) - .filter(Boolean); + const cleaned = []; + let insideList = false; + let buffer = ""; + + for (let line of lines) { + // Remove comentário inline (# ...) — mas só fora de strings + line = line.replace(/#.*$/, "").trim(); + if (!line) continue; + + if (!insideList) { + if (line.includes("=[") && !line.includes("]")) { + // Início de lista multilinha + insideList = true; + buffer = line; + } else { + cleaned.push(line); + } + } else { + buffer += line; + if (line.includes("]")) { + // Fim da lista + insideList = false; + cleaned.push(buffer); + buffer = ""; + } + } } - return v; + // Parseia cada linha chave=valor + const result = {}; + for (const line of cleaned) { + const eqIdx = line.indexOf("="); + if (eqIdx === -1) continue; + + const key = line.slice(0, eqIdx).trim(); + const raw = line.slice(eqIdx + 1).trim(); + + if (raw.startsWith("[") && raw.endsWith("]")) { + result[key] = raw + .slice(1, -1) + .split(",") + .map(x => x.trim()) + .filter(Boolean); + } else { + result[key] = raw; + } + } + + return result; } const raw = fs.readFileSync("manybot.conf", "utf8"); - -const config = Object.fromEntries( - raw - .split("\n") - .map(l => l.trim()) - .filter(l => l && !l.startsWith("#")) - .map(l => { - const [k, ...v] = l.split("="); - return [k.trim(), parseValue(v.join("="))]; - }) -); +const config = parseConf(raw); export const CLIENT_ID = config.CLIENT_ID ?? "bot_permanente"; export const BOT_PREFIX = config.BOT_PREFIX ?? "🤖 *ManyBot:* ";