Spaces:
Running
Running
Upload 11 files
Browse files
bot.js
CHANGED
|
@@ -115,32 +115,26 @@ function parsePatterns(text) {
|
|
| 115 |
}
|
| 116 |
|
| 117 |
/**
|
| 118 |
-
*
|
| 119 |
-
*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
*/
|
| 121 |
-
function
|
| 122 |
const upper = text.toUpperCase();
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 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 |
-
|
| 135 |
-
|
| 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 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
// ββ Download image to disk via MTProto βββββββββββββββββββββββββββββββββββββ
|
| 272 |
-
//
|
| 273 |
-
//
|
| 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:
|
| 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
|
| 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 |
-
|
| 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, {
|