Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/bot/handlers/codeScanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ describe('extractCodesFromText', () => {
});
});

describe('URL stripping', () => {
test('does not extract Twitch username as code', () => {
const text = 'LATU-EGIS-TOCK\n\nhttps://www.twitch.tv/dungeonscrawlers\n1x Electrum Chest';
expect(extractCodesFromText(text)).toEqual(['LATUEGISTOCK']);
});

test('does not extract long URL path segments as codes', () => {
const text = 'GOEL-ARNA-VIDS\nhttps://www.twitch.tv/jasoncharlesmiller\n1x Electrum Chest';
expect(extractCodesFromText(text)).toEqual(['GOELARNAVIDS']);
});

test('strips http URLs as well as https', () => {
const text = 'ABCD1234EFGH http://example.com/SOMETHINGLONG123456';
expect(extractCodesFromText(text)).toEqual(['ABCD1234EFGH']);
});
});

describe('Discord emoji stripping', () => {
test('strips static emoji tags before matching', () => {
const text = 'Redeem <:gem:123456789012345678> this: ABCD1234EFGH';
Expand Down
11 changes: 8 additions & 3 deletions src/bot/handlers/codeScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ 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 emoji tags then uppercase for matching
const messageText = stripDiscordEmojis(message.content).toUpperCase();
// 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[] = [];
Expand All @@ -40,6 +45,6 @@ export async function scanMessageForCodes(message: Message): Promise<string[]> {
}

export function extractCodesFromText(text: string): string[] {
const codeMatches = stripDiscordEmojis(text).toUpperCase().match(CODE_REGEX) || [];
const codeMatches = stripUrls(stripDiscordEmojis(text)).toUpperCase().match(CODE_REGEX) || [];
return codeMatches.map((code) => code.replaceAll('-', ''));
}
Loading