Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
45323b2d3d | ||
|
|
58f5e13eb3 | ||
|
|
a6fda095d8 | ||
|
|
372f644995 |
4
package-lock.json
generated
4
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "manybot",
|
||||
"version": "2.4.0",
|
||||
"version": "2.4.2",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "manybot",
|
||||
"version": "2.4.0",
|
||||
"version": "2.4.2",
|
||||
"dependencies": {
|
||||
"node-addon-api": "^7",
|
||||
"node-gyp": "^12.2.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "manybot",
|
||||
"version": "2.4.0",
|
||||
"version": "2.4.2",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"node-addon-api": "^7",
|
||||
|
||||
@@ -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,32 +13,26 @@
|
||||
* 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);
|
||||
|
||||
// Filtra chats não autorizados
|
||||
if (!CHATS.includes(chatId)) return;
|
||||
// CHATS vazio = aceita todos os chats
|
||||
if (CHATS.length > 0 && !CHATS.includes(chatId)) return;
|
||||
|
||||
// Loga a mensagem recebida
|
||||
const ctx = await buildMessageContext(msg, chat);
|
||||
const ctx = await buildMessageContext(msg, chat, BOT_PREFIX);
|
||||
logger.msg(ctx);
|
||||
|
||||
// Monta a api que será passada para os plugins
|
||||
const api = buildApi({ msg, chat, pluginRegistry });
|
||||
|
||||
// Distribui para todos os plugins ativos
|
||||
const api = buildApi({ msg, chat, client, pluginRegistry });
|
||||
const context = { msg: api.msg, chat: api.chat, api };
|
||||
|
||||
for (const plugin of pluginRegistry.values()) {
|
||||
|
||||
@@ -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 {
|
||||
@@ -155,6 +154,61 @@ export function buildApi({ msg, chat, pluginRegistry }) {
|
||||
return currentChat.sendMessage(media, { sendMediaAsSticker: true });
|
||||
},
|
||||
|
||||
// ── Envio para chat específico ───────────────────────────
|
||||
|
||||
/**
|
||||
* Envia texto para um chat específico por ID.
|
||||
* @param {string} chatId
|
||||
* @param {string} text
|
||||
*/
|
||||
async sendTo(chatId, text) {
|
||||
return client.sendMessage(chatId, text);
|
||||
},
|
||||
|
||||
/**
|
||||
* Envia vídeo para um chat específico por ID.
|
||||
* @param {string} chatId
|
||||
* @param {string} filePath
|
||||
* @param {string} [caption]
|
||||
*/
|
||||
async sendVideoTo(chatId, filePath, caption = "") {
|
||||
const media = MessageMedia.fromFilePath(filePath);
|
||||
return client.sendMessage(chatId, media, { caption });
|
||||
},
|
||||
|
||||
/**
|
||||
* Envia áudio para um chat específico por ID.
|
||||
* @param {string} chatId
|
||||
* @param {string} filePath
|
||||
*/
|
||||
async sendAudioTo(chatId, filePath) {
|
||||
const media = MessageMedia.fromFilePath(filePath);
|
||||
return client.sendMessage(chatId, media, { sendAudioAsVoice: true });
|
||||
},
|
||||
|
||||
/**
|
||||
* Envia imagem para um chat específico por ID.
|
||||
* @param {string} chatId
|
||||
* @param {string} filePath
|
||||
* @param {string} [caption]
|
||||
*/
|
||||
async sendImageTo(chatId, filePath, caption = "") {
|
||||
const media = MessageMedia.fromFilePath(filePath);
|
||||
return client.sendMessage(chatId, media, { caption });
|
||||
},
|
||||
|
||||
/**
|
||||
* Envia figurinha para um chat específico por ID.
|
||||
* @param {string} chatId
|
||||
* @param {string | Buffer} source
|
||||
*/
|
||||
async sendStickerTo(chatId, source) {
|
||||
const media = typeof source === "string"
|
||||
? MessageMedia.fromFilePath(source)
|
||||
: new MessageMedia("image/webp", source.toString("base64"));
|
||||
return client.sendMessage(chatId, media, { sendMediaAsSticker: true });
|
||||
},
|
||||
|
||||
// ── Acesso a outros plugins ──────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user