forked from Xeio/IdleCodeRedeemer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodeScanner.ts
More file actions
50 lines (39 loc) · 1.68 KB
/
codeScanner.ts
File metadata and controls
50 lines (39 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { Message } from 'discord.js';
import { codeManager } from '../database/codeManager';
// Aligned with the Idle Champions Chrome extension regex
const CODE_REGEX = /(?:[A-Z0-9*!@#$%^&*]-?){12}(?:(?:[A-Z0-9*!@#$%^&*]-?){4})?/g;
// Strip Discord custom emoji tags (<:name:id> and <a:name:id>) before scanning
// to avoid false positives from emoji names and snowflake IDs.
function stripDiscordEmojis(text: string): string {
return text.replace(/<a?:[^:]+:\d+>/g, '');
}
// Strip URLs before scanning to avoid false positives from URL paths/usernames.
function stripUrls(text: string): string {
return text.replace(/https?:\/\/\S+/gi, '');
}
export async function scanMessageForCodes(message: Message): Promise<string[]> {
try {
// Strip URLs and emoji tags, then uppercase for matching
const messageText = stripUrls(stripDiscordEmojis(message.content)).toUpperCase();
const codeMatches = messageText.match(CODE_REGEX) || [];
const codes: string[] = [];
for (const match of codeMatches) {
// Remove dashes from the code
const cleanCode = match.replaceAll('-', '');
// Verify it hasn't already been redeemed
const isRedeemed = await codeManager.isCodeRedeemed(cleanCode);
if (!isRedeemed) {
codes.push(cleanCode);
console.log(`[CODE SCANNER] Found new code: ${cleanCode}`);
}
}
return codes;
} catch (error) {
console.error('[CODE SCANNER] Error scanning message:', error);
return [];
}
}
export function extractCodesFromText(text: string): string[] {
const codeMatches = stripUrls(stripDiscordEmojis(text)).toUpperCase().match(CODE_REGEX) || [];
return codeMatches.map((code) => code.replaceAll('-', ''));
}