levix-bot 4.1.0 → 4.2.1

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.
Files changed (82) hide show
  1. package/package.json +1 -1
  2. package/public/dashboard/assets/{index-DngAcp-o.js → index-4AuqfnH5.js} +80 -80
  3. package/public/dashboard/index.html +1 -1
  4. package/scheduler.cjs +6 -1
  5. package/src/commands/autoschedule.cjs +40 -10
  6. package/src/commands/blacklist.cjs +35 -10
  7. package/src/commands/block.cjs +18 -5
  8. package/src/commands/calc.cjs +90 -24
  9. package/src/commands/debt.cjs +59 -33
  10. package/src/commands/deleteschedule.cjs +21 -5
  11. package/src/commands/digit.cjs +34 -8
  12. package/src/commands/gemini.cjs +217 -87
  13. package/src/commands/group/add.cjs +46 -13
  14. package/src/commands/group/all.cjs +15 -5
  15. package/src/commands/group/antiSpam.cjs +11 -4
  16. package/src/commands/group/antilink.cjs +36 -18
  17. package/src/commands/group/approveall.cjs +22 -7
  18. package/src/commands/group/clearwarns.cjs +18 -5
  19. package/src/commands/group/deletenote.cjs +18 -5
  20. package/src/commands/group/demote.cjs +25 -7
  21. package/src/commands/group/kick.cjs +28 -12
  22. package/src/commands/group/media.cjs +31 -12
  23. package/src/commands/group/members.cjs +21 -6
  24. package/src/commands/group/note.cjs +14 -4
  25. package/src/commands/group/promote.cjs +22 -6
  26. package/src/commands/group/removeall.cjs +13 -3
  27. package/src/commands/group/rules.cjs +20 -5
  28. package/src/commands/group/save.cjs +20 -5
  29. package/src/commands/group/setname.cjs +19 -6
  30. package/src/commands/group/setpp.cjs +21 -6
  31. package/src/commands/group/setrules.cjs +17 -5
  32. package/src/commands/group/setwarn.cjs +23 -7
  33. package/src/commands/group/tagadmins.cjs +16 -6
  34. package/src/commands/group/warn.cjs +35 -15
  35. package/src/commands/group/warns.cjs +33 -13
  36. package/src/commands/group/welcome.cjs +34 -16
  37. package/src/commands/group.cjs +96 -70
  38. package/src/commands/help.cjs +270 -135
  39. package/src/commands/lang.cjs +46 -13
  40. package/src/commands/listschedules.cjs +28 -11
  41. package/src/commands/loop.cjs +43 -9
  42. package/src/commands/memory.cjs +90 -30
  43. package/src/commands/mod.cjs +69 -27
  44. package/src/commands/notes.cjs +15 -5
  45. package/src/commands/perm.cjs +52 -20
  46. package/src/commands/ping.cjs +15 -3
  47. package/src/commands/poll.cjs +40 -9
  48. package/src/commands/prayer.cjs +28 -12
  49. package/src/commands/qr.cjs +20 -5
  50. package/src/commands/rand.cjs +54 -12
  51. package/src/commands/restart.cjs +13 -3
  52. package/src/commands/schedule.cjs +32 -9
  53. package/src/commands/score.cjs +26 -10
  54. package/src/commands/setprefix.cjs +27 -5
  55. package/src/commands/shortlink.cjs +36 -10
  56. package/src/commands/shutdown.cjs +13 -3
  57. package/src/commands/status.cjs +32 -11
  58. package/src/commands/sticker.cjs +34 -7
  59. package/src/commands/stt.cjs +163 -108
  60. package/src/commands/todo.cjs +31 -9
  61. package/src/commands/tts.cjs +25 -7
  62. package/src/commands/unblock.cjs +17 -4
  63. package/src/commands/weather.cjs +55 -17
  64. package/src/config/settings.cjs +1 -1
  65. package/src/handlers/command.handler.js +50 -10
  66. package/src/handlers/group.handler.js +5 -1
  67. package/src/handlers/message.handler.js +9 -0
  68. package/src/middleware/antispam.middleware.js +9 -2
  69. package/src/services/aiAgent.cjs +40 -39
  70. package/src/services/aiProviders.cjs +46 -19
  71. package/src/services/aiRouter.cjs +2 -1
  72. package/src/services/aiTools.cjs +2 -1
  73. package/src/utils/commandDocs.cjs +58 -0
  74. package/src/utils/geminiMedia.cjs +168 -0
  75. package/src/utils/i18n.cjs +113 -0
  76. package/src/utils/markdownParser.cjs +4 -2
  77. package/src/utils/memory.cjs +2 -1
  78. package/src/utils/permissionLevel.cjs +34 -6
  79. package/src/utils/providerUrl.cjs +29 -2
  80. package/src/utils/sendBotMessage.cjs +7 -3
  81. package/src/utils/sendBotMessage.esm.js +13 -3
  82. package/src/utils/statusMessage.cjs +11 -4
@@ -46,12 +46,12 @@ const {
46
46
  } = require("../services/aiAgent.cjs");
47
47
  const { getProvider } = require("../services/aiRouter.cjs");
48
48
  const { downloadContentFromMessage } = require("@whiskeysockets/baileys");
49
- const fs = require("fs").promises;
50
- const path = require("path");
49
+ const { unwrapMessage, visibleText } = require("../utils/messageContent.cjs");
51
50
  const {
52
51
  assertProviderBaseUrl,
53
52
  assertProviderRequestUrl,
54
53
  } = require("../utils/providerUrl.cjs");
54
+ const { tr } = require("../utils/i18n.cjs");
55
55
 
56
56
  // Per-(chat, sender) explicit context buffer for the multi-message workflow.
57
57
  // Entries accumulate until `!gemini send` flushes them into a single call.
@@ -133,16 +133,29 @@ function geminiClients() {
133
133
  return geminiCache;
134
134
  }
135
135
 
136
- async function processIncomingMedia(parts, mediaMessage, mimeOverride = null) {
136
+ // The media in a message and the type WhatsApp filed it under. That type, not
137
+ // the MIME type, picks the decryption keys: a photo sent as a document has to
138
+ // be downloaded as a "document".
139
+ const MEDIA_TYPES = ["image", "video", "audio", "document"];
140
+ function pickMedia(message) {
141
+ const inner = unwrapMessage(message);
142
+ for (const type of MEDIA_TYPES) {
143
+ const media = inner[`${type}Message`];
144
+ if (media) return { media, type };
145
+ }
146
+ return null;
147
+ }
148
+
149
+ async function processIncomingMedia(parts, mediaMessage, downloadType) {
137
150
  const provider = getProvider();
138
151
  if (provider.id === "gemini") {
139
152
  await assertProviderRequestUrl(settings.get("gemini_base_url"), { allowLoopback: true });
140
153
  }
141
154
  const { genAI } = geminiClients();
142
- return provider.prepareMedia(parts, mediaMessage, mimeOverride, {
155
+ return provider.prepareMedia(parts, mediaMessage, null, {
143
156
  downloadContentFromMessage,
144
157
  genAI,
145
- tempDir: __dirname,
158
+ downloadType,
146
159
  });
147
160
  }
148
161
 
@@ -164,12 +177,12 @@ async function captureContextEntry(msg, args) {
164
177
  const cleanedText = args.slice(1).join(" ").trim();
165
178
  if (cleanedText) entry.text = cleanedText;
166
179
 
167
- const m = msg.message || {};
180
+ const m = unwrapMessage(msg.message);
168
181
 
169
- const directMedia = m.imageMessage || m.videoMessage || m.audioMessage || m.documentMessage;
182
+ const directMedia = pickMedia(m);
170
183
  if (directMedia) {
171
184
  try {
172
- await processIncomingMedia(entry.mediaParts, directMedia);
185
+ await processIncomingMedia(entry.mediaParts, directMedia.media, directMedia.type);
173
186
  } catch (err) {
174
187
  logger.warn(
175
188
  { err: err?.message },
@@ -180,18 +193,11 @@ async function captureContextEntry(msg, args) {
180
193
 
181
194
  const quoted = m.extendedTextMessage?.contextInfo?.quotedMessage;
182
195
  if (quoted) {
183
- entry.quotedText =
184
- quoted.conversation ||
185
- quoted.extendedTextMessage?.text ||
186
- quoted.imageMessage?.caption ||
187
- quoted.videoMessage?.caption ||
188
- quoted.documentMessage?.caption ||
189
- "";
190
- const quotedMedia =
191
- quoted.imageMessage || quoted.videoMessage || quoted.audioMessage || quoted.documentMessage;
196
+ entry.quotedText = visibleText(quoted);
197
+ const quotedMedia = pickMedia(quoted);
192
198
  if (quotedMedia) {
193
199
  try {
194
- await processIncomingMedia(entry.quotedMediaParts, quotedMedia);
200
+ await processIncomingMedia(entry.quotedMediaParts, quotedMedia.media, quotedMedia.type);
195
201
  } catch (err) {
196
202
  logger.warn(
197
203
  { err: err?.message },
@@ -211,7 +217,9 @@ function entryHasContent(e) {
211
217
  function buildPartsFromBuffer(entries) {
212
218
  const parts = [];
213
219
  for (const e of entries) {
214
- if (e.quotedText) parts.push({ text: `(مرجع: "${e.quotedText}")` });
220
+ if (e.quotedText) {
221
+ parts.push({ text: tr(`(Reference: "${e.quotedText}")`, `(مرجع: "${e.quotedText}")`) });
222
+ }
215
223
  if (e.quotedMediaParts?.length) parts.push(...e.quotedMediaParts);
216
224
  if (e.mediaParts?.length) parts.push(...e.mediaParts);
217
225
  if (e.text) parts.push({ text: e.text });
@@ -229,18 +237,30 @@ function summarizeEntry(e, idx) {
229
237
  const preview = e.quotedText.length > 40 ? e.quotedText.slice(0, 40) + "…" : e.quotedText;
230
238
  bits.push(`↩️ "${preview}"`);
231
239
  }
232
- if (e.mediaParts?.length) bits.push(`🖼️ ميديا (${e.mediaParts.length})`);
233
- if (e.quotedMediaParts?.length) bits.push(`🖼️↩️ ميديا مقتبسة (${e.quotedMediaParts.length})`);
234
- return `${idx + 1}. ${bits.join(" · ") || "(فارغة)"}`;
240
+ if (e.mediaParts?.length)
241
+ bits.push(tr(`🖼️ media (${e.mediaParts.length})`, `🖼️ ميديا (${e.mediaParts.length})`));
242
+ if (e.quotedMediaParts?.length) {
243
+ bits.push(
244
+ tr(
245
+ `🖼️↩️ quoted media (${e.quotedMediaParts.length})`,
246
+ `🖼️↩️ ميديا مقتبسة (${e.quotedMediaParts.length})`,
247
+ ),
248
+ );
249
+ }
250
+ return `${idx + 1}. ${bits.join(" · ") || tr("(empty)", "(فارغة)")}`;
235
251
  }
236
252
 
237
253
  module.exports = {
238
254
  name: "gemini",
239
255
  aliases: ["ask", "ai", "resetai", "del", "delall", "generate"],
240
- description:
241
- "مساعد ذكي بأدوات (بحث، فتح روابط، ذاكرة دائمة). sub-commands: add/send/clear/show للسياق، generate للصور، del/delall لمسح المحادثة.",
242
- usage:
243
- "gemini <النص>\ngemini add <النص>\ngemini send\ngemini show\ngemini clear\ngenerate <وصف الصورة>\ndel (مسح ذاكرة المحادثة)\ndelall (مسح كل المحادثات)",
256
+ description: {
257
+ en: "AI assistant with tools (web search, reading links, long-term memory). Sub-commands: add/send/clear/show for context, generate for images, del/delall to clear the conversation.",
258
+ ar: "مساعد ذكي بأدوات (بحث في الويب، قراءة الروابط، ذاكرة دائمة). أوامر فرعية: add/send/clear/show للسياق، وgenerate للصور، وdel/delall لمسح المحادثة.",
259
+ },
260
+ usage: {
261
+ en: "gemini <text>\ngemini add <text>\ngemini send\ngemini show\ngemini clear\ngenerate <image description>\ndel (clear this chat's conversation)\ndelall (clear every conversation)",
262
+ ar: "gemini <النص>\ngemini add <النص>\ngemini send\ngemini show\ngemini clear\ngenerate <وصف الصورة>\ndel (مسح سجل محادثة هذا الشات)\ndelall (مسح كل المحادثات)",
263
+ },
244
264
  chat: "all",
245
265
 
246
266
  async execute(sock, msg, args, body, groupMetadata, ctx = {}) {
@@ -278,7 +298,7 @@ module.exports = {
278
298
  return sendBotMessage(
279
299
  sock,
280
300
  chatId,
281
- { text: "🚫 هذا الأمر مخصص للمالك فقط." },
301
+ { text: tr("🚫 Only the bot owner can use this.", "🚫 هذا الأمر مخصص للمالك فقط.") },
282
302
  { replyTo: msg },
283
303
  );
284
304
  await deleteChatHistoryAsync(chatId);
@@ -286,9 +306,12 @@ module.exports = {
286
306
  sock,
287
307
  chatId,
288
308
  {
289
- text:
309
+ text: tr(
310
+ "✅ Conversation history cleared.\n" +
311
+ "_(Long-term memory isn't cleared this way — use `!memory clear` if that's what you meant.)_",
290
312
  "✅ تم مسح سجل المحادثة.\n" +
291
- "_(الذاكرة الدائمة مش بتتمسح كده — استخدم `!memory clear` لو ده اللي تقصده.)_",
313
+ "_(الذاكرة الدائمة مش بتتمسح كده — استخدم `!memory clear` لو ده اللي تقصده.)_",
314
+ ),
292
315
  },
293
316
  { replyTo: msg },
294
317
  );
@@ -298,14 +321,19 @@ module.exports = {
298
321
  return sendBotMessage(
299
322
  sock,
300
323
  chatId,
301
- { text: "🚫 هذا الأمر مخصص للمالك فقط." },
324
+ { text: tr("🚫 Only the bot owner can use this.", "🚫 هذا الأمر مخصص للمالك فقط.") },
302
325
  { replyTo: msg },
303
326
  );
304
327
  await deleteAllChatHistoriesAsync();
305
328
  return sendBotMessage(
306
329
  sock,
307
330
  chatId,
308
- { text: "✅ تم مسح كل سجلات المحادثات بنجاح." },
331
+ {
332
+ text: tr(
333
+ "✅ Every conversation history was cleared.",
334
+ "✅ تم مسح كل سجلات المحادثات بنجاح.",
335
+ ),
336
+ },
309
337
  { replyTo: msg },
310
338
  );
311
339
  }
@@ -320,20 +348,33 @@ module.exports = {
320
348
  sock,
321
349
  chatId,
322
350
  {
323
- text: "اكتب وصف للصورة اللي عايزها بعد الأمر. مثال: !generate قطة ترتدي قبعة",
351
+ text: tr(
352
+ "Describe the image you want after the command. Example: !generate a cat wearing a hat",
353
+ "اكتب وصف للصورة اللي عايزها بعد الأمر. مثال: !generate قطة ترتدي قبعة",
354
+ ),
324
355
  },
325
356
  { replyTo: msg },
326
357
  );
327
358
  }
328
359
 
329
- const status = await createStatus(sock, chatId, "🎨 بجهّز الصورة...", { replyTo: msg });
360
+ const status = await createStatus(
361
+ sock,
362
+ chatId,
363
+ tr("🎨 Making the image...", "🎨 بجهّز الصورة..."),
364
+ { replyTo: msg },
365
+ );
330
366
 
331
367
  try {
332
368
  // Image generation needs a model that returns image bytes, which only
333
369
  // the Gemini image models do — it stays on Gemini whatever the chat
334
370
  // provider is.
335
371
  if (!geminiClients().genAI) {
336
- throw new Error("مفتاح Gemini API غير معرف — توليد الصور يعمل على Gemini فقط");
372
+ throw new Error(
373
+ tr(
374
+ "No Gemini API key is set — image generation only runs on Gemini",
375
+ "مفتاح Gemini API غير معرف — توليد الصور يعمل على Gemini فقط",
376
+ ),
377
+ );
337
378
  }
338
379
  await assertProviderRequestUrl(settings.get("gemini_base_url"), {
339
380
  allowLoopback: true,
@@ -355,7 +396,9 @@ module.exports = {
355
396
  const image = (response.candidates?.[0]?.content?.parts || []).find(
356
397
  (part) => part?.inlineData?.data,
357
398
  );
358
- if (!image) throw new Error("الموديل رجّع رد من غير صورة");
399
+ if (!image) {
400
+ throw new Error(tr("The model answered without an image", "الموديل رجّع رد من غير صورة"));
401
+ }
359
402
  const imageBuffer = Buffer.from(image.inlineData.data, "base64");
360
403
 
361
404
  await status.remove();
@@ -364,13 +407,13 @@ module.exports = {
364
407
  chatId,
365
408
  {
366
409
  image: imageBuffer,
367
- caption: `🖼️ تفضل، صورة لـ: "${imagePrompt}"`,
410
+ caption: tr(`🖼️ Here you go: "${imagePrompt}"`, `🖼️ تفضل، صورة لـ: "${imagePrompt}"`),
368
411
  },
369
412
  { replyTo: msg },
370
413
  );
371
414
  } catch (error) {
372
415
  logger.error({ err: error }, `Error in !generate command`);
373
- await status.fail(error, "للأسف معرفتش أعمل الصورة");
416
+ await status.fail(error, tr("I couldn't make the image", "للأسف معرفتش أعمل الصورة"));
374
417
  }
375
418
  return;
376
419
  }
@@ -383,9 +426,12 @@ module.exports = {
383
426
  sock,
384
427
  chatId,
385
428
  {
386
- text:
429
+ text: tr(
430
+ "📭 Nothing to add to the context.\n\n" +
431
+ "Write text after the command, reply to a message, or send media with the caption `!gemini add`.",
387
432
  "📭 مفيش حاجة أضيفها للـ context.\n\n" +
388
- "اكتب نص بعد الأمر، أو رد على رسالة، أو ابعت ميديا مع كابشن `!gemini add`.",
433
+ "اكتب نص بعد الأمر، أو رد على رسالة، أو ابعت ميديا مع كابشن `!gemini add`.",
434
+ ),
389
435
  },
390
436
  { replyTo: msg },
391
437
  );
@@ -395,7 +441,12 @@ module.exports = {
395
441
  return sendBotMessage(
396
442
  sock,
397
443
  chatId,
398
- { text: `الـ context ممتلئ (حد أقصى ${MAX_BUFFER_ENTRIES} رسائل). ابعت \`!gemini send\` أو \`!gemini clear\`.` },
444
+ {
445
+ text: tr(
446
+ `The context is full (${MAX_BUFFER_ENTRIES} messages at most). Send \`!gemini send\` or \`!gemini clear\`.`,
447
+ `الـ context ممتلئ (حد أقصى ${MAX_BUFFER_ENTRIES} رسائل). ابعت \`!gemini send\` أو \`!gemini clear\`.`,
448
+ ),
449
+ },
399
450
  { replyTo: msg },
400
451
  );
401
452
  }
@@ -405,9 +456,12 @@ module.exports = {
405
456
  sock,
406
457
  chatId,
407
458
  {
408
- text:
459
+ text: tr(
460
+ "The context is too big, or too many contexts are open. " +
461
+ "Send `!gemini send` or `!gemini clear` and try again.",
409
462
  "الـ context كبير جدًا أو عدد الـ contexts المفتوحة وصل للحد. " +
410
- "ابعت `!gemini send` أو `!gemini clear` وحاول تاني.",
463
+ "ابعت `!gemini send` أو `!gemini clear` وحاول تاني.",
464
+ ),
411
465
  },
412
466
  { replyTo: msg },
413
467
  );
@@ -417,9 +471,12 @@ module.exports = {
417
471
  sock,
418
472
  chatId,
419
473
  {
420
- text:
474
+ text: tr(
475
+ `✅ Added to the context. (Total now: *${stored.length}*)\n\n` +
476
+ "Keep going with `!gemini add ...`, or send it all with `!gemini send`.",
421
477
  `✅ اتضافت للـ context. (إجمالي الآن: *${stored.length}*)\n\n` +
422
- "كمل بـ `!gemini add ...` ، أو ابعت كله بـ `!gemini send`.",
478
+ "كمل بـ `!gemini add ...` ، أو ابعت كله بـ `!gemini send`.",
479
+ ),
423
480
  },
424
481
  { replyTo: msg },
425
482
  );
@@ -432,7 +489,12 @@ module.exports = {
432
489
  sock,
433
490
  chatId,
434
491
  {
435
- text: had ? `🗑️ تم مسح *${had}* رسالة من الـ context.` : "📭 الـ context كان فاضي أصلاً.",
492
+ text: had
493
+ ? tr(
494
+ `🗑️ Cleared *${had}* messages from the context.`,
495
+ `🗑️ تم مسح *${had}* رسالة من الـ context.`,
496
+ )
497
+ : tr("📭 The context was already empty.", "📭 الـ context كان فاضي أصلاً."),
436
498
  },
437
499
  { replyTo: msg },
438
500
  );
@@ -444,7 +506,12 @@ module.exports = {
444
506
  return sendBotMessage(
445
507
  sock,
446
508
  chatId,
447
- { text: "📭 الـ context فاضي. ابدأ بـ `!gemini add ...`." },
509
+ {
510
+ text: tr(
511
+ "📭 The context is empty. Start with `!gemini add ...`.",
512
+ "📭 الـ context فاضي. ابدأ بـ `!gemini add ...`.",
513
+ ),
514
+ },
448
515
  { replyTo: msg },
449
516
  );
450
517
  }
@@ -453,9 +520,15 @@ module.exports = {
453
520
  chatId,
454
521
  {
455
522
  text:
456
- `📋 *الـ context الحالي:* (${buf.length} رسالة)\n\n` +
523
+ tr(
524
+ `📋 *Current context:* (${buf.length} messages)\n\n`,
525
+ `📋 *الـ context الحالي:* (${buf.length} رسالة)\n\n`,
526
+ ) +
457
527
  buf.map((e, i) => summarizeEntry(e, i)).join("\n") +
458
- "\n\nابعت كله بـ `!gemini send` أو امسحه بـ `!gemini clear`.",
528
+ tr(
529
+ "\n\nSend it all with `!gemini send` or clear it with `!gemini clear`.",
530
+ "\n\nابعت كله بـ `!gemini send` أو امسحه بـ `!gemini clear`.",
531
+ ),
459
532
  },
460
533
  { replyTo: msg },
461
534
  );
@@ -475,9 +548,12 @@ module.exports = {
475
548
  sock,
476
549
  chatId,
477
550
  {
478
- text:
551
+ text: tr(
552
+ "📭 The context is empty and `!gemini send` has no text.\n\n" +
553
+ "Start with `!gemini add ...`, or write text with `!gemini send ...`.",
479
554
  "📭 الـ context فاضي ومفيش نص في `!gemini send`.\n\n" +
480
- "ابدأ بـ `!gemini add ...` الأول، أو اكتب نص مع `!gemini send ...`.",
555
+ "ابدأ بـ `!gemini add ...` الأول، أو اكتب نص مع `!gemini send ...`.",
556
+ ),
481
557
  },
482
558
  { replyTo: msg },
483
559
  );
@@ -491,39 +567,66 @@ module.exports = {
491
567
  if (!parts.length && (msg.message?.imageMessage || msg.message?.videoMessage)) {
492
568
  const mediaMessage = msg.message.imageMessage || msg.message.videoMessage;
493
569
  try {
494
- await processIncomingMedia(parts, mediaMessage);
570
+ await processIncomingMedia(
571
+ parts,
572
+ mediaMessage,
573
+ msg.message.imageMessage ? "image" : "video",
574
+ );
495
575
  parts.push({
496
- text: prompt || mediaMessage.caption || "ماذا يوجد في هذه الصورة/الفيديو؟",
576
+ text:
577
+ prompt ||
578
+ mediaMessage.caption ||
579
+ tr("What is in this image/video?", "ماذا يوجد في هذه الصورة/الفيديو؟"),
497
580
  });
498
581
  } catch (error) {
499
582
  logger.error({ err: error }, "Failed to upload media to Gemini.");
500
- return sendBotError(sock, chatId, error, "حصلت مشكلة في رفع الصورة/الفيديو", {
501
- replyTo: msg,
502
- });
583
+ return sendBotError(
584
+ sock,
585
+ chatId,
586
+ error,
587
+ tr("Uploading the image/video failed", "حصلت مشكلة في رفع الصورة/الفيديو"),
588
+ {
589
+ replyTo: msg,
590
+ },
591
+ );
503
592
  }
504
593
  } else if (!parts.length && msg.message?.audioMessage) {
505
594
  try {
506
- await processIncomingMedia(parts, msg.message.audioMessage);
507
- parts.push({ text: prompt || "حلل لي هذا التسجيل الصوتي." });
595
+ await processIncomingMedia(parts, msg.message.audioMessage, "audio");
596
+ parts.push({ text: prompt || tr("Analyze this recording.", "حلل لي هذا التسجيل الصوتي.") });
508
597
  } catch (error) {
509
598
  logger.error({ err: error }, "Failed to upload audio to Gemini.");
510
- return sendBotError(sock, chatId, error, "حصلت مشكلة في رفع التسجيل الصوتي", {
511
- replyTo: msg,
512
- });
599
+ return sendBotError(
600
+ sock,
601
+ chatId,
602
+ error,
603
+ tr("Uploading the recording failed", "حصلت مشكلة في رفع التسجيل الصوتي"),
604
+ {
605
+ replyTo: msg,
606
+ },
607
+ );
513
608
  }
514
609
  } else if (!parts.length && msg.message?.documentMessage) {
515
610
  try {
516
- await processIncomingMedia(parts, msg.message.documentMessage);
611
+ await processIncomingMedia(parts, msg.message.documentMessage, "document");
517
612
  const fileName = msg.message.documentMessage.fileName || "document";
518
613
  const text = prompt || msg.message.documentMessage.caption || "";
519
614
  parts.push({
520
- text: text ? `${text}\n(الملف: ${fileName})` : `حلل/لخّص الملف: ${fileName}`,
615
+ text: text
616
+ ? tr(`${text}\n(File: ${fileName})`, `${text}\n(الملف: ${fileName})`)
617
+ : tr(`Analyze/summarize the file: ${fileName}`, `حلل/لخّص الملف: ${fileName}`),
521
618
  });
522
619
  } catch (error) {
523
620
  logger.error({ err: error }, "Failed to upload document to Gemini.");
524
- return sendBotError(sock, chatId, error, "حصلت مشكلة في رفع الملف", {
525
- replyTo: msg,
526
- });
621
+ return sendBotError(
622
+ sock,
623
+ chatId,
624
+ error,
625
+ tr("Uploading the file failed", "حصلت مشكلة في رفع الملف"),
626
+ {
627
+ replyTo: msg,
628
+ },
629
+ );
527
630
  }
528
631
  } else if (!parts.length && prompt) {
529
632
  parts.push({ text: prompt });
@@ -533,7 +636,12 @@ module.exports = {
533
636
  return sendBotMessage(
534
637
  sock,
535
638
  chatId,
536
- { text: "يرجى كتابة سؤال أو إرسال صورة/فيديو/تسجيل صوتي/ملف مع الأمر." },
639
+ {
640
+ text: tr(
641
+ "Write a question, or send an image/video/voice note/file with the command.",
642
+ "يرجى كتابة سؤال أو إرسال صورة/فيديو/تسجيل صوتي/ملف مع الأمر.",
643
+ ),
644
+ },
537
645
  { replyTo: msg },
538
646
  );
539
647
  }
@@ -545,27 +653,29 @@ module.exports = {
545
653
 
546
654
  if (quotedMsg) {
547
655
  let quotedMediaPart = null;
548
- const quotedText =
549
- quotedMsg.conversation ||
550
- quotedMsg.extendedTextMessage?.text ||
551
- quotedMsg.documentMessage?.caption;
552
-
553
- const mediaInQuote =
554
- quotedMsg.imageMessage ||
555
- quotedMsg.videoMessage ||
556
- quotedMsg.audioMessage ||
557
- quotedMsg.documentMessage;
656
+ // Captions count: "describe this" on a captioned photo should see both.
657
+ const quotedText = visibleText(quotedMsg);
658
+ const mediaInQuote = pickMedia(quotedMsg);
558
659
 
559
660
  if (mediaInQuote) {
560
661
  try {
561
662
  const probeParts = [];
562
- await processIncomingMedia(probeParts, mediaInQuote);
663
+ await processIncomingMedia(probeParts, mediaInQuote.media, mediaInQuote.type);
563
664
  quotedMediaPart = probeParts[0];
564
665
  } catch (mediaError) {
666
+ // Stop here. Carrying on without the media used to post this error
667
+ // AND a second reply from a model that had never seen the photo
668
+ // ("I can't see any image") — two messages, the second one wrong.
565
669
  logger.error({ err: mediaError }, "Failed to process quoted media");
566
- await sendBotError(sock, chatId, mediaError, "حصلت مشكلة في الرسالة المقتبسة", {
567
- replyTo: msg,
568
- });
670
+ return sendBotError(
671
+ sock,
672
+ chatId,
673
+ mediaError,
674
+ tr("There was a problem with the quoted message", "حصلت مشكلة في الرسالة المقتبسة"),
675
+ {
676
+ replyTo: msg,
677
+ },
678
+ );
569
679
  }
570
680
  }
571
681
 
@@ -573,12 +683,18 @@ module.exports = {
573
683
  parts.unshift(quotedMediaPart);
574
684
  if (quotedText) {
575
685
  parts.unshift({
576
- text: `بالاعتماد على هذه الرسالة كمرجع:\n"""\n${quotedText}\n"""\n\n`,
686
+ text: tr(
687
+ `Using this message as a reference:\n"""\n${quotedText}\n"""\n\n`,
688
+ `بالاعتماد على هذه الرسالة كمرجع:\n"""\n${quotedText}\n"""\n\n`,
689
+ ),
577
690
  });
578
691
  }
579
692
  } else if (quotedText) {
580
693
  parts.unshift({
581
- text: `بالاعتماد على هذه الرسالة كمرجع:\n"""\n${quotedText}\n"""\n\nأجب على التالي:`,
694
+ text: tr(
695
+ `Using this message as a reference:\n"""\n${quotedText}\n"""\n\nAnswer the following:`,
696
+ `بالاعتماد على هذه الرسالة كمرجع:\n"""\n${quotedText}\n"""\n\nأجب على التالي:`,
697
+ ),
582
698
  });
583
699
  }
584
700
  }
@@ -590,7 +706,10 @@ module.exports = {
590
706
  sock,
591
707
  chatId,
592
708
  {
593
- text: `خطأ في الإعدادات: مفتاح مزود الذكاء الاصطناعي الحالي (${activeProvider.label || activeProvider.id}) غير معرف — عدّله من لوحة التحكم.`,
709
+ text: tr(
710
+ `Settings problem: no API key is set for the current AI provider (${activeProvider.label || activeProvider.id}) — set it in the control panel.`,
711
+ `خطأ في الإعدادات: مفتاح مزود الذكاء الاصطناعي الحالي (${activeProvider.label || activeProvider.id}) غير معرف — عدّله من لوحة التحكم.`,
712
+ ),
594
713
  },
595
714
  { replyTo: msg },
596
715
  );
@@ -622,12 +741,15 @@ module.exports = {
622
741
  const targets = [...caller.mentionedJids, caller.quotedParticipant].filter(Boolean);
623
742
  if (targets.length) {
624
743
  parts.unshift({
625
- text: `(معرفات مذكورة في الرسالة: ${[...new Set(targets)].join(", ")})`,
744
+ text: tr(
745
+ `(IDs mentioned in the message: ${[...new Set(targets)].join(", ")})`,
746
+ `(معرفات مذكورة في الرسالة: ${[...new Set(targets)].join(", ")})`,
747
+ ),
626
748
  });
627
749
  }
628
750
 
629
751
  // The one message that will carry the whole run.
630
- const status = await createStatus(sock, chatId, "🤖 جاري التفكير...", {
752
+ const status = await createStatus(sock, chatId, tr("🤖 Thinking...", "🤖 جاري التفكير..."), {
631
753
  replyTo: msg,
632
754
  });
633
755
 
@@ -663,7 +785,12 @@ module.exports = {
663
785
  result = await attempt(cleaned);
664
786
  }
665
787
 
666
- const text = result.text?.trim() || "مفيش رد جه من الموديل. جرّب تصيغ السؤال بشكل تاني.";
788
+ const text =
789
+ result.text?.trim() ||
790
+ tr(
791
+ "The model sent no answer. Try asking another way.",
792
+ "مفيش رد جه من الموديل. جرّب تصيغ السؤال بشكل تاني.",
793
+ );
667
794
  // Appended only when Gemini really grounded the answer on a search —
668
795
  // formatSources() returns "" for an answer the model gave from its own
669
796
  // knowledge, so there is never a Sources block with nothing behind it.
@@ -671,7 +798,10 @@ module.exports = {
671
798
  await status.finish(responseText);
672
799
  } catch (error) {
673
800
  logger.error({ err: error }, "Error in !gemini command");
674
- await status.fail(error, "حصلت مشكلة وأنا بكلم الذكاء الاصطناعي");
801
+ await status.fail(
802
+ error,
803
+ tr("Something went wrong while talking to the AI", "حصلت مشكلة وأنا بكلم الذكاء الاصطناعي"),
804
+ );
675
805
  }
676
806
  },
677
807
  };