!!Oregon Logic

master
j 2024-03-29 00:25:47 -04:00
parent 08851d70fc
commit a95240a1b0
1 changed files with 11 additions and 17 deletions

View File

@ -2,27 +2,21 @@ import { Comment } from '../models/Comment';
export class CommentParser { export class CommentParser {
/** /**
* Extracts Reddit usernames from the body of a single comment. * Parses commands from the body of a single comment triggered by !!Oregon.
* @param comment A single Comment object to be processed. * @param comment A single Comment object to be processed.
* @returns An array of unique Reddit usernames found in the comment. * @returns An object containing the command and its arguments, if any.
*/ */
public static extractUsernames(comment: Comment): string[] { public static parseCommand(comment: Comment): { command: string, args: string[] } {
const regexPattern: RegExp = /(^|\s|\\r\\n|\\t|[".,;(){}\[\]!?@#])(\/?u\/[a-zA-Z0-9_]+)/g; const commandTrigger: RegExp = /!!Oregon\s+(\w+)(?:\s+(.*))?/i;
const foundUsernames: Set<string> = new Set(); const match = comment.body.match(commandTrigger);
const matches = comment.body.match(regexPattern); if (match) {
if (matches) { const command = match[1].toLowerCase();
matches.forEach(match => { const args = match[2] ? match[2].split(' ').map(arg => arg.trim()) : [];
// Ensure the username is captured in a standardized format return { command, args };
const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_]+)/);
if (usernameMatch) {
// Standardize to "username" format
const username = `${usernameMatch[1].toLowerCase()}`;
foundUsernames.add(username);
}
});
} }
return Array.from(foundUsernames); // No command found
return { command: '', args: [] };
} }
} }