!!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 {
/**
* 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.
* @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[] {
const regexPattern: RegExp = /(^|\s|\\r\\n|\\t|[".,;(){}\[\]!?@#])(\/?u\/[a-zA-Z0-9_]+)/g;
const foundUsernames: Set<string> = new Set();
public static parseCommand(comment: Comment): { command: string, args: string[] } {
const commandTrigger: RegExp = /!!Oregon\s+(\w+)(?:\s+(.*))?/i;
const match = comment.body.match(commandTrigger);
const matches = comment.body.match(regexPattern);
if (matches) {
matches.forEach(match => {
// Ensure the username is captured in a standardized format
const usernameMatch = match.trim().match(/\/?u\/([a-zA-Z0-9_]+)/);
if (usernameMatch) {
// Standardize to "username" format
const username = `${usernameMatch[1].toLowerCase()}`;
foundUsernames.add(username);
}
});
if (match) {
const command = match[1].toLowerCase();
const args = match[2] ? match[2].split(' ').map(arg => arg.trim()) : [];
return { command, args };
}
return Array.from(foundUsernames);
// No command found
return { command: '', args: [] };
}
}