[fix] corrige sendTo passando client explicitamente para a pluginApi

This commit is contained in:
synt-xerror
2026-03-24 01:26:46 -03:00
parent 58f5e13eb3
commit 45323b2d3d
3 changed files with 24 additions and 26 deletions

View File

@@ -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}`);
}

View File

@@ -20,9 +20,8 @@ const { MessageMedia } = pkg;
* @param {Map<string, any>} 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 ──────────────────────────────