diff --git a/package-lock.json b/package-lock.json index d40d0ad..1ee225a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "manybot", - "version": "2.4.1", + "version": "2.4.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "manybot", - "version": "2.4.1", + "version": "2.4.2", "dependencies": { "node-addon-api": "^7", "node-gyp": "^12.2.0", diff --git a/src/kernel/messageHandler.js b/src/kernel/messageHandler.js index 85d3e4d..8961711 100644 --- a/src/kernel/messageHandler.js +++ b/src/kernel/messageHandler.js @@ -5,6 +5,7 @@ * * Ordem: * 1. Filtra chats não permitidos (CHATS do .conf) + * — se CHATS estiver vazio, aceita todos os chats * 2. Loga a mensagem * 3. Passa o contexto para todos os plugins ativos * @@ -12,33 +13,31 @@ * Cada plugin decide por conta própria se age ou ignora. */ -import { CHATS } from "../config.js"; -import { getChatId } from "../utils/getChatId.js"; -import { buildApi } from "./pluginApi.js"; -import { pluginRegistry } from "./pluginLoader.js"; -import { runPlugin } from "./pluginGuard.js"; -import { buildMessageContext } from "../logger/messageContext.js"; -import { logger } from "../logger/logger.js"; +import { CHATS, BOT_PREFIX } from "../config.js"; +import { getChatId } from "../utils/getChatId.js"; +import { buildApi } from "./pluginApi.js"; +import { pluginRegistry } from "./pluginLoader.js"; +import { runPlugin } from "./pluginGuard.js"; +import { buildMessageContext } from "../logger/messageContext.js"; +import { logger } from "../logger/logger.js"; +import client from "../client/whatsappClient.js"; -/** - * @param {import("whatsapp-web.js").Message} msg - */ export async function handleMessage(msg) { const chat = await msg.getChat(); const chatId = getChatId(chat); - + // CHATS vazio = aceita todos os chats if (CHATS.length > 0 && !CHATS.includes(chatId)) return; - - const ctx = await buildMessageContext(msg, chat); + + const ctx = await buildMessageContext(msg, chat, BOT_PREFIX); logger.msg(ctx); - - const api = buildApi({ msg, chat, pluginRegistry }); + + const api = buildApi({ msg, chat, client, pluginRegistry }); const context = { msg: api.msg, chat: api.chat, api }; - + for (const plugin of pluginRegistry.values()) { await runPlugin(plugin, context); } - + logger.done("message_create", `de +${ctx.senderNumber}`); } \ No newline at end of file diff --git a/src/kernel/pluginApi.js b/src/kernel/pluginApi.js index a853e19..fdef36b 100644 --- a/src/kernel/pluginApi.js +++ b/src/kernel/pluginApi.js @@ -20,9 +20,8 @@ const { MessageMedia } = pkg; * @param {Map} params.pluginRegistry * @returns {object} api */ -export function buildApi({ msg, chat, pluginRegistry }) { +export function buildApi({ msg, chat, client, pluginRegistry }) { - // ── Helpers internos ────────────────────────────────────── const currentChat = chat; return { @@ -163,7 +162,7 @@ export function buildApi({ msg, chat, pluginRegistry }) { * @param {string} text */ async sendTo(chatId, text) { - return currentChat._client.sendMessage(chatId, text); + return client.sendMessage(chatId, text); }, /** @@ -174,7 +173,7 @@ export function buildApi({ msg, chat, pluginRegistry }) { */ async sendVideoTo(chatId, filePath, caption = "") { const media = MessageMedia.fromFilePath(filePath); - return currentChat._client.sendMessage(chatId, media, { caption }); + return client.sendMessage(chatId, media, { caption }); }, /** @@ -184,7 +183,7 @@ export function buildApi({ msg, chat, pluginRegistry }) { */ async sendAudioTo(chatId, filePath) { const media = MessageMedia.fromFilePath(filePath); - return currentChat._client.sendMessage(chatId, media, { sendAudioAsVoice: true }); + return client.sendMessage(chatId, media, { sendAudioAsVoice: true }); }, /** @@ -195,7 +194,7 @@ export function buildApi({ msg, chat, pluginRegistry }) { */ async sendImageTo(chatId, filePath, caption = "") { const media = MessageMedia.fromFilePath(filePath); - return currentChat._client.sendMessage(chatId, media, { caption }); + return client.sendMessage(chatId, media, { caption }); }, /** @@ -207,7 +206,7 @@ export function buildApi({ msg, chat, pluginRegistry }) { const media = typeof source === "string" ? MessageMedia.fromFilePath(source) : new MessageMedia("image/webp", source.toString("base64")); - return currentChat._client.sendMessage(chatId, media, { sendMediaAsSticker: true }); + return client.sendMessage(chatId, media, { sendMediaAsSticker: true }); }, // ── Acesso a outros plugins ──────────────────────────────