@manybot/manybot 5.0.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/i18n/index.js DELETED
@@ -1,235 +0,0 @@
1
- /**
2
- * i18n/index.js
3
- *
4
- * Internationalization system for ManyBot.
5
- * Loads translations based on LANGUAGE configuration.
6
- * Fallback is always English (en).
7
- *
8
- * Plugins can use createPluginT() to have isolated i18n.
9
- */
10
-
11
- import fs from "fs";
12
- import path from "path";
13
- import { fileURLToPath } from "url";
14
- import { CONFIG } from "#config";
15
-
16
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
17
- const LOCALES_DIR = path.join(__dirname, "..", "locales");
18
-
19
- // Default language (fallback)
20
- const DEFAULT_LANG = "en";
21
-
22
- // Cache of loaded translations
23
- const translations = new Map();
24
-
25
- /**
26
- * Loads a translation JSON file
27
- * @param {string} lang - language code (en, pt, es)
28
- * @returns {object|null}
29
- */
30
- function loadLocale(lang) {
31
- if (translations.has(lang)) {
32
- return translations.get(lang);
33
- }
34
-
35
- const filePath = path.join(LOCALES_DIR, `${lang}.json`);
36
-
37
- try {
38
- if (!fs.existsSync(filePath)) {
39
- return null;
40
- }
41
- const content = fs.readFileSync(filePath, "utf8");
42
- const data = JSON.parse(content);
43
- translations.set(lang, data);
44
- return data;
45
- } catch (err) {
46
- console.error(`[i18n] Failed to load locale ${lang}:`, err.message);
47
- return null;
48
- }
49
- }
50
-
51
- /**
52
- * Gets configured language or default
53
- * @returns {string}
54
- */
55
- function getConfiguredLang() {
56
- const lang = CONFIG.LANGUAGE?.trim().toLowerCase();
57
- if (!lang) return DEFAULT_LANG;
58
-
59
- // Check if file exists
60
- const filePath = path.join(LOCALES_DIR, `${lang}.json`);
61
- if (!fs.existsSync(filePath)) {
62
- console.warn(`[i18n] Language "${lang}" not found, falling back to "${DEFAULT_LANG}"`);
63
- return DEFAULT_LANG;
64
- }
65
-
66
- return lang;
67
- }
68
-
69
- // Load languages
70
- const currentLang = getConfiguredLang();
71
- const currentTranslations = loadLocale(currentLang) || {};
72
- const fallbackTranslations = loadLocale(DEFAULT_LANG) || {};
73
-
74
- /**
75
- * Gets a nested value from an object using dot path
76
- * @param {object} obj
77
- * @param {string} key - path like "system.connected"
78
- * @returns {string|undefined}
79
- */
80
- function getNestedValue(obj, key) {
81
- const parts = key.split(".");
82
- let current = obj;
83
-
84
- for (const part of parts) {
85
- if (current === null || current === undefined || typeof current !== "object") {
86
- return undefined;
87
- }
88
- current = current[part];
89
- }
90
-
91
- return current;
92
- }
93
-
94
- /**
95
- * Replaces placeholders {{key}} with values from context
96
- * @param {string} str
97
- * @param {object} context
98
- * @returns {string}
99
- */
100
- function interpolate(str, context = {}) {
101
- return str.replace(/\{\{(\w+)\}\}/g, (match, key) => {
102
- return context[key] !== undefined ? String(context[key]) : match;
103
- });
104
- }
105
-
106
- /**
107
- * Main translation function
108
- * @param {string} key - translation key (e.g., "system.connected")
109
- * @param {object} context - values to interpolate {{key}}
110
- * @returns {string}
111
- */
112
- export function t(key, context = {}) {
113
- // Try current language first
114
- let value = getNestedValue(currentTranslations, key);
115
-
116
- // Fallback to English if not found
117
- if (value === undefined) {
118
- value = getNestedValue(fallbackTranslations, key);
119
- }
120
-
121
- // If still not found, return the key
122
- if (value === undefined) {
123
- return key;
124
- }
125
-
126
- // If not string, convert
127
- if (typeof value !== "string") {
128
- return String(value);
129
- }
130
-
131
- // Interpolate values
132
- return interpolate(value, context);
133
- }
134
-
135
- /**
136
- * Creates an isolated translation function for a plugin.
137
- * Plugins should have their own locale/ folder with en.json, es.json, etc.
138
- *
139
- * Usage in plugin:
140
- * import { createPluginT } from "../../i18n/index.js";
141
- * const { t } = createPluginT(import.meta.url);
142
- *
143
- * Folder structure:
144
- * myPlugin/
145
- * index.js
146
- * locale/
147
- * en.json
148
- * es.json
149
- * pt.json
150
- *
151
- * @param {string} pluginMetaUrl - import.meta.url from the plugin
152
- * @returns {{ t: Function, lang: string }}
153
- */
154
- export function createPluginT(pluginMetaUrl) {
155
- const pluginDir = path.dirname(fileURLToPath(pluginMetaUrl));
156
- const pluginLocaleDir = path.join(pluginDir, "locale");
157
-
158
- // Get bot's configured language
159
- const targetLang = currentLang;
160
-
161
- // Load plugin translations
162
- let pluginTranslations = {};
163
- let pluginFallback = {};
164
-
165
- try {
166
- // Try to load the configured language
167
- const targetPath = path.join(pluginLocaleDir, `${targetLang}.json`);
168
- if (fs.existsSync(targetPath)) {
169
- pluginTranslations = JSON.parse(fs.readFileSync(targetPath, "utf8"));
170
- }
171
-
172
- // Always load English as fallback
173
- const fallbackPath = path.join(pluginLocaleDir, `${DEFAULT_LANG}.json`);
174
- if (fs.existsSync(fallbackPath)) {
175
- pluginFallback = JSON.parse(fs.readFileSync(fallbackPath, "utf8"));
176
- }
177
- } catch (err) {
178
- // Silent fail - plugin may not have translations
179
- }
180
-
181
- /**
182
- * Plugin-specific translation function
183
- * @param {string} key
184
- * @param {object} context
185
- * @returns {string}
186
- */
187
- function pluginT(key, context = {}) {
188
- // Try plugin's target language first
189
- let value = getNestedValue(pluginTranslations, key);
190
-
191
- // Fallback to plugin's English
192
- if (value === undefined) {
193
- value = getNestedValue(pluginFallback, key);
194
- }
195
-
196
- // If still not found, return the key
197
- if (value === undefined) {
198
- return key;
199
- }
200
-
201
- if (typeof value !== "string") {
202
- return String(value);
203
- }
204
-
205
- return interpolate(value, context);
206
- }
207
-
208
- return { t: pluginT, lang: targetLang };
209
- }
210
-
211
- /**
212
- * Reloads translations (useful for hot-reload)
213
- */
214
- export function reloadTranslations() {
215
- translations.clear();
216
- const lang = getConfiguredLang();
217
- const newTranslations = loadLocale(lang) || {};
218
- const newFallback = loadLocale(DEFAULT_LANG) || {};
219
-
220
- // Update references
221
- Object.assign(currentTranslations, newTranslations);
222
- Object.assign(fallbackTranslations, newFallback);
223
-
224
- console.log(`[i18n] Translations reloaded for language: ${lang}`);
225
- }
226
-
227
- /**
228
- * Returns current language
229
- * @returns {string}
230
- */
231
- export function getCurrentLang() {
232
- return currentLang;
233
- }
234
-
235
- export default { t, createPluginT, reloadTranslations, getCurrentLang };
@@ -1,35 +0,0 @@
1
- /**
2
- * messageHandler.js
3
- *
4
- * Central pipeline for received messages.
5
- *
6
- * Order:
7
- * 1. Filter allowed chats (CHATS from .conf)
8
- * — if CHATS is empty, accepts all chats
9
- * 2. Log the message
10
- * 3. Pass context to all active plugins
11
- *
12
- * Kernel knows no commands — only distributes.
13
- * Each plugin decides on its own whether to act or ignore.
14
- */
15
-
16
- import { CHATS } from "#config";
17
- import { getChatId } from "#utils/getChatId";
18
- import { buildApi } from "#manyapi";
19
- import { pluginRegistry } from "#kernel/pluginLoader";
20
- import { runPlugin } from "#kernel/pluginGuard";
21
- import client from "#client/whatsappClient";
22
-
23
- export async function handleMessage(msg) {
24
- const chat = await msg.getChat();
25
- const chatId = getChatId(chat);
26
-
27
- if (CHATS.length > 0 && !CHATS.includes(chatId)) return;
28
-
29
- const baseCtx = buildApi({ msg, chat, client, pluginRegistry });
30
-
31
- for (const plugin of pluginRegistry.values()) {
32
- const ctx = { ...baseCtx, storage: buildStorageApi(plugin.name) };
33
- await runPlugin(plugin, ctx);
34
- }
35
- }