fix: auth
This commit is contained in:
@@ -20,6 +20,43 @@ const { MessageMedia } = pkg;
|
||||
* @param {Map<string, any>} 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;
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
|
||||
10
src/main.js
10
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...");
|
||||
Reference in New Issue
Block a user