hansaka01 commited on
Commit
12ea892
Β·
verified Β·
1 Parent(s): 6b1778d

Upload 11 files

Browse files
Files changed (1) hide show
  1. bot.js +37 -34
bot.js CHANGED
@@ -115,32 +115,26 @@ function parsePatterns(text) {
115
  }
116
 
117
  /**
118
- * Detect grid size hint from caption text.
119
- * Returns a number (forced size) or null (let OCR auto-detect).
 
 
 
 
 
 
120
  */
121
- function detectGridSizeFromCaption(text) {
122
  const upper = text.toUpperCase();
123
-
124
- // Explicit NxN e.g. "10x10" or "8x8"
125
- const sizeMatch = text.match(/\b(\d+)\s*[xXΓ—]\s*\1\b/);
126
- if (sizeMatch) {
127
- const n = parseInt(sizeMatch[1], 10);
128
- if (n >= 4 && n <= 15) {
129
- console.log(`[Grid] Caption explicit size: ${n}Γ—${n}`);
130
- return n;
131
- }
132
  }
133
-
134
- if (
135
- upper.includes('HARD MODE') ||
136
- upper.includes('10X10') ||
137
- upper.includes('10 X 10')
138
- ) {
139
- console.log('[Grid] Caption phrase β†’ 10Γ—10');
140
- return 10;
141
  }
142
-
143
- return null; // let OCR auto-detect
144
  }
145
 
146
  // ─── Result formatter ─────────────────────────────────────────────────────────
@@ -258,7 +252,14 @@ async function startBot() {
258
  return;
259
  }
260
 
261
- // Only handle messages that carry a photo
 
 
 
 
 
 
 
262
  const hasPhoto = msg.media && (
263
  msg.media.className === 'MessageMediaPhoto' ||
264
  (msg.media.document &&
@@ -266,12 +267,17 @@ async function startBot() {
266
  msg.media.document.mimeType.startsWith('image/'))
267
  );
268
 
269
- if (!hasPhoto) return;
 
 
 
 
 
 
270
 
271
  // ── Download image to disk via MTProto ─────────────────────────────────────
272
- // GramJS downloadMedia accepts a STRING path as outputFile β†’ writes file,
273
- // returns the path. Do NOT pass Buffer constructor β€” that causes the
274
- // "writer.write is not a function" error.
275
  const imagePath = path.join(
276
  __dirname,
277
  `grid_${Date.now()}_${Math.random().toString(36).slice(2)}.jpg`
@@ -279,19 +285,17 @@ async function startBot() {
279
 
280
  try {
281
  await client.sendMessage(chatId, {
282
- message: 'πŸ” Downloading and processing your grid image...',
283
  });
284
 
285
- // Pass the destination path string β€” GramJS streams the file there via MTProto
286
  const result = await client.downloadMedia(msg, {
287
  outputFile: imagePath,
288
  });
289
 
290
- // result is the path string when outputFile is a string path
291
  if (!result) throw new Error('downloadMedia returned null/undefined');
292
  if (!fs.existsSync(imagePath)) throw new Error('File not written to disk');
293
 
294
- console.log(`[Download] Saved to ${imagePath} (${fs.statSync(imagePath).size} bytes)`);
295
 
296
  } catch (dlErr) {
297
  console.error('[Download] Failed:', dlErr.message);
@@ -306,9 +310,8 @@ async function startBot() {
306
  try {
307
  stats.imagesProcessed++;
308
 
309
- const forcedSize = detectGridSizeFromCaption(caption);
310
-
311
- const grid = await extractGrid(imagePath, forcedSize);
312
 
313
  if (!grid || grid.length === 0) {
314
  await client.sendMessage(chatId, {
 
115
  }
116
 
117
  /**
118
+ * Check whether this caption should trigger grid processing at all.
119
+ *
120
+ * Rules (case-insensitive):
121
+ * "WORD GRID CHALLENGE" β†’ process as 8Γ—8
122
+ * "HARD MODE CHALLENGE" β†’ process as 10Γ—10
123
+ * anything else β†’ ignore (return null)
124
+ *
125
+ * Returns: { gridSize: 8|10 } or null (skip this message)
126
  */
127
+ function getChallengeInfo(text) {
128
  const upper = text.toUpperCase();
129
+ if (upper.includes('WORD GRID CHALLENGE')) {
130
+ console.log('[Trigger] WORD GRID CHALLENGE detected β†’ 8Γ—8');
131
+ return { gridSize: 8 };
 
 
 
 
 
 
132
  }
133
+ if (upper.includes('HARD MODE CHALLENGE')) {
134
+ console.log('[Trigger] HARD MODE CHALLENGE detected β†’ 10Γ—10');
135
+ return { gridSize: 10 };
 
 
 
 
 
136
  }
137
+ return null; // not a recognised challenge caption β€” ignore
 
138
  }
139
 
140
  // ─── Result formatter ─────────────────────────────────────────────────────────
 
252
  return;
253
  }
254
 
255
+ // ── Gate: only act on recognised challenge captions ───────────────────────
256
+ // Caption MUST contain "WORD GRID CHALLENGE" (β†’ 8Γ—8)
257
+ // or "HARD MODE CHALLENGE" (β†’ 10Γ—10).
258
+ // All other messages (including plain photos) are silently ignored.
259
+ const challenge = getChallengeInfo(caption);
260
+ if (!challenge) return; // not a challenge message β€” do nothing
261
+
262
+ // ── Only handle messages that carry a photo ────────────────────────────────
263
  const hasPhoto = msg.media && (
264
  msg.media.className === 'MessageMediaPhoto' ||
265
  (msg.media.document &&
 
267
  msg.media.document.mimeType.startsWith('image/'))
268
  );
269
 
270
+ if (!hasPhoto) {
271
+ // It's a recognised challenge caption but has no image β€” let them know
272
+ await client.sendMessage(chatId, {
273
+ message: '⚠️ Please attach a grid image together with your challenge caption.',
274
+ });
275
+ return;
276
+ }
277
 
278
  // ── Download image to disk via MTProto ─────────────────────────────────────
279
+ // Pass a string path as outputFile β€” GramJS routes this to createWriteStream
280
+ // (NOT Buffer constructor, which would cause "writer.write is not a function")
 
281
  const imagePath = path.join(
282
  __dirname,
283
  `grid_${Date.now()}_${Math.random().toString(36).slice(2)}.jpg`
 
285
 
286
  try {
287
  await client.sendMessage(chatId, {
288
+ message: `πŸ” Processing your ${challenge.gridSize}Γ—${challenge.gridSize} grid...`,
289
  });
290
 
 
291
  const result = await client.downloadMedia(msg, {
292
  outputFile: imagePath,
293
  });
294
 
 
295
  if (!result) throw new Error('downloadMedia returned null/undefined');
296
  if (!fs.existsSync(imagePath)) throw new Error('File not written to disk');
297
 
298
+ console.log(`[Download] Saved ${imagePath} (${fs.statSync(imagePath).size} bytes)`);
299
 
300
  } catch (dlErr) {
301
  console.error('[Download] Failed:', dlErr.message);
 
310
  try {
311
  stats.imagesProcessed++;
312
 
313
+ // Grid size is already determined by the challenge keyword β€” no guessing
314
+ const grid = await extractGrid(imagePath, challenge.gridSize);
 
315
 
316
  if (!grid || grid.length === 0) {
317
  await client.sendMessage(chatId, {