diff --git a/src/kernel/pluginApi.js b/src/kernel/pluginApi.js index fdef36b..45c1981 100644 --- a/src/kernel/pluginApi.js +++ b/src/kernel/pluginApi.js @@ -20,6 +20,43 @@ const { MessageMedia } = pkg; * @param {Map} params.pluginRegistry * @returns {object} api */ +/** + * API de setup — sem contexto de mensagem. + * Passada para plugin.setup(api) na inicialização. + * Só tem sendTo e variantes, log e schedule. + */ +export function buildSetupApi(client) { + return { + async sendTo(chatId, text) { + return client.sendMessage(chatId, text); + }, + async sendVideoTo(chatId, filePath, caption = "") { + const media = MessageMedia.fromFilePath(filePath); + return client.sendMessage(chatId, media, { caption }); + }, + async sendAudioTo(chatId, filePath) { + const media = MessageMedia.fromFilePath(filePath); + return client.sendMessage(chatId, media, { sendAudioAsVoice: true }); + }, + async sendImageTo(chatId, filePath, caption = "") { + const media = MessageMedia.fromFilePath(filePath); + return client.sendMessage(chatId, media, { caption }); + }, + 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 }); + }, + log: { + info: (...a) => logger.info(...a), + warn: (...a) => logger.warn(...a), + error: (...a) => logger.error(...a), + success: (...a) => logger.success(...a), + }, + }; +} + export function buildApi({ msg, chat, client, pluginRegistry }) { const currentChat = chat; diff --git a/src/kernel/pluginLoader.js b/src/kernel/pluginLoader.js index 84e5a30..a2db207 100644 --- a/src/kernel/pluginLoader.js +++ b/src/kernel/pluginLoader.js @@ -12,7 +12,7 @@ import fs from "fs"; import path from "path"; import { logger } from "../logger/logger.js"; -const PLUGINS_DIR = path.resolve("src/plugins"); +const PLUGINS_DIR = path.resolve("plugins"); /** * Cada entrada no registry: @@ -51,6 +51,23 @@ export async function loadPlugins(activePlugins) { logger.success(`Plugins carregados: ${ativos} ativos${erros ? `, ${erros} com erro` : ""}`); } +/** + * Chama setup(api) em todos os plugins que o exportarem. + * Executado uma vez após o bot conectar ao WhatsApp. + * + * @param {object} api — api sem contexto de mensagem (só sendTo, log, schedule...) + */ +export async function setupPlugins(api) { + for (const plugin of pluginRegistry.values()) { + if (plugin.status !== "active" || !plugin.setup) continue; + try { + await plugin.setup(api); + } catch (err) { + logger.error(`Falha no setup do plugin "${plugin.name}": ${err.message}`); + } + } +} + /** * Carrega um único plugin pelo nome. * @param {string} name @@ -76,7 +93,8 @@ async function loadPlugin(name) { name, status: "active", run: mod.default, - exports: mod.api ?? null, // exports públicos opcionais (api de outros plugins) + setup: mod.setup ?? null, // opcional — chamado uma vez na inicialização + exports: mod.api ?? null, error: null, }); diff --git a/src/main.js b/src/main.js index 2c18b76..9f02cec 100644 --- a/src/main.js +++ b/src/main.js @@ -7,11 +7,12 @@ import client from "./client/whatsappClient.js"; import { handleMessage } from "./kernel/messageHandler.js"; -import { loadPlugins } from "./kernel/pluginLoader.js"; +import { loadPlugins, setupPlugins } from "./kernel/pluginLoader.js"; +import { buildSetupApi } from "./kernel/pluginApi.js"; import { logger } from "./logger/logger.js"; import { PLUGINS } from "./config.js"; -logger.info("Iniciando ManyBot...\n"); +logger.info("Iniciando ManyBot..."); // Rede de segurança global — nenhum erro deve derrubar o bot process.on("uncaughtException", (err) => { @@ -37,6 +38,7 @@ client.on("message_create", async (msg) => { } }); -client.initialize(); -console.log("\n"); +client.on("ready", async () => { + await setupPlugins(buildSetupApi(client)); +}); logger.info("Cliente inicializado. Aguardando conexão com WhatsApp..."); \ No newline at end of file