[fix] corrige sendTo passando client explicitamente para a pluginApi
This commit is contained in:
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "manybot",
|
"name": "manybot",
|
||||||
"version": "2.4.1",
|
"version": "2.4.2",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "manybot",
|
"name": "manybot",
|
||||||
"version": "2.4.1",
|
"version": "2.4.2",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"node-addon-api": "^7",
|
"node-addon-api": "^7",
|
||||||
"node-gyp": "^12.2.0",
|
"node-gyp": "^12.2.0",
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
*
|
*
|
||||||
* Ordem:
|
* Ordem:
|
||||||
* 1. Filtra chats não permitidos (CHATS do .conf)
|
* 1. Filtra chats não permitidos (CHATS do .conf)
|
||||||
|
* — se CHATS estiver vazio, aceita todos os chats
|
||||||
* 2. Loga a mensagem
|
* 2. Loga a mensagem
|
||||||
* 3. Passa o contexto para todos os plugins ativos
|
* 3. Passa o contexto para todos os plugins ativos
|
||||||
*
|
*
|
||||||
@@ -12,33 +13,31 @@
|
|||||||
* Cada plugin decide por conta própria se age ou ignora.
|
* Cada plugin decide por conta própria se age ou ignora.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { CHATS } from "../config.js";
|
import { CHATS, BOT_PREFIX } from "../config.js";
|
||||||
import { getChatId } from "../utils/getChatId.js";
|
import { getChatId } from "../utils/getChatId.js";
|
||||||
import { buildApi } from "./pluginApi.js";
|
import { buildApi } from "./pluginApi.js";
|
||||||
import { pluginRegistry } from "./pluginLoader.js";
|
import { pluginRegistry } from "./pluginLoader.js";
|
||||||
import { runPlugin } from "./pluginGuard.js";
|
import { runPlugin } from "./pluginGuard.js";
|
||||||
import { buildMessageContext } from "../logger/messageContext.js";
|
import { buildMessageContext } from "../logger/messageContext.js";
|
||||||
import { logger } from "../logger/logger.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) {
|
export async function handleMessage(msg) {
|
||||||
const chat = await msg.getChat();
|
const chat = await msg.getChat();
|
||||||
const chatId = getChatId(chat);
|
const chatId = getChatId(chat);
|
||||||
|
|
||||||
// CHATS vazio = aceita todos os chats
|
// CHATS vazio = aceita todos os chats
|
||||||
if (CHATS.length > 0 && !CHATS.includes(chatId)) return;
|
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);
|
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 };
|
const context = { msg: api.msg, chat: api.chat, api };
|
||||||
|
|
||||||
for (const plugin of pluginRegistry.values()) {
|
for (const plugin of pluginRegistry.values()) {
|
||||||
await runPlugin(plugin, context);
|
await runPlugin(plugin, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.done("message_create", `de +${ctx.senderNumber}`);
|
logger.done("message_create", `de +${ctx.senderNumber}`);
|
||||||
}
|
}
|
||||||
@@ -20,9 +20,8 @@ const { MessageMedia } = pkg;
|
|||||||
* @param {Map<string, any>} params.pluginRegistry
|
* @param {Map<string, any>} params.pluginRegistry
|
||||||
* @returns {object} api
|
* @returns {object} api
|
||||||
*/
|
*/
|
||||||
export function buildApi({ msg, chat, pluginRegistry }) {
|
export function buildApi({ msg, chat, client, pluginRegistry }) {
|
||||||
|
|
||||||
// ── Helpers internos ──────────────────────────────────────
|
|
||||||
const currentChat = chat;
|
const currentChat = chat;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -163,7 +162,7 @@ export function buildApi({ msg, chat, pluginRegistry }) {
|
|||||||
* @param {string} text
|
* @param {string} text
|
||||||
*/
|
*/
|
||||||
async sendTo(chatId, 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 = "") {
|
async sendVideoTo(chatId, filePath, caption = "") {
|
||||||
const media = MessageMedia.fromFilePath(filePath);
|
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) {
|
async sendAudioTo(chatId, filePath) {
|
||||||
const media = MessageMedia.fromFilePath(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 = "") {
|
async sendImageTo(chatId, filePath, caption = "") {
|
||||||
const media = MessageMedia.fromFilePath(filePath);
|
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"
|
const media = typeof source === "string"
|
||||||
? MessageMedia.fromFilePath(source)
|
? MessageMedia.fromFilePath(source)
|
||||||
: new MessageMedia("image/webp", source.toString("base64"));
|
: 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 ──────────────────────────────
|
// ── Acesso a outros plugins ──────────────────────────────
|
||||||
|
|||||||
Reference in New Issue
Block a user