From 689df79ab0c877359212352b59ee2082bea6c126 Mon Sep 17 00:00:00 2001 From: j Date: Sat, 23 Mar 2024 01:02:51 -0400 Subject: [PATCH] retooled message service to be more generic added an enum for any new message banks --- src/utils/MessageService.ts | 86 ++++++++++--------------------------- 1 file changed, 23 insertions(+), 63 deletions(-) diff --git a/src/utils/MessageService.ts b/src/utils/MessageService.ts index fc8bb30..385306e 100644 --- a/src/utils/MessageService.ts +++ b/src/utils/MessageService.ts @@ -1,88 +1,48 @@ import fs from 'fs'; import path from 'path'; -/** - * Manages the retrieval and formatting of messages stored in text files. - * This class provides functionality to load messages for rDrama and Reddit, - * select a random message, and replace placeholders within that message - * with specified values. - */ +// Define an enum for message file names +export enum MessageFileName { + RdramaPreviousMessage = 'rdrama_PreviousMessage.txt', + RdramaShouldntNotify = 'rdrama_ShouldntNotify.txt', + RedditMessages = 'reddit_messages.txt', + RdramaMessages = 'rdrama_messages.txt', +} + export class MessageService { - /** - * Loads rDrama messages from a text file, splitting by a specific delimiter. + * Loads messages from a specified text file based on the enum, splitting by a specific delimiter. * Each message is separated by '---END---' in the text file. * - * @example - * const rdramaMessages = MessageService.loadRdramaMessages(); - * - * @returns {string[] | undefined} An array of rDrama messages, or undefined if there was an error loading the messages. + * @param {MessageFileName} fileName - The enum value representing the file containing the messages. + * @returns {string[] | undefined} An array of messages, or undefined if there was an error loading the messages. */ - private static loadRdramaMessages(): string[] | undefined { + private static loadMessages(fileName: MessageFileName): string[] | undefined { try { - const rdramaMessagesPath = path.join(__dirname, '..', 'messages', 'rdrama_messages.txt'); - return fs.readFileSync(rdramaMessagesPath, 'utf-8').split('---END---').filter(line => line.trim()); + const messagesPath = path.join(__dirname, '..', 'messages', fileName); + return fs.readFileSync(messagesPath, 'utf-8').split('---END---').filter(line => line.trim()); } catch (error) { - console.error('Failed to load rDrama messages:', error); + console.error(`Failed to load messages from ${fileName}:`, error); } } /** - * Loads Reddit messages from a text file, splitting by a specific delimiter. - * Each message is separated by '---END---' in the text file. - * - * @example - * const redditMessages = MessageService.loadRedditMessages(); - * - * @returns {string[] | undefined} An array of Reddit messages, or undefined if there was an error loading the messages. - */ - private static loadRedditMessages(): string[] | undefined { - try { - const redditMessagesPath = path.join(__dirname, '..', 'messages', 'reddit_messages.txt'); - return fs.readFileSync(redditMessagesPath, 'utf-8').split('---END---').filter(line => line.trim()); - } catch (error) { - console.error('Failed to load Reddit messages:', error); - } - } - - /** - * Selects a random Reddit message from the loaded messages and replaces placeholders within it. - * - * @example - * const message = MessageService.getRandomRedditMessage({ username: 'exampleUser' }); + * Selects a random message from the loaded messages and replaces placeholders within it. * + * @param {MessageFileName} fileName - The enum value representing the file containing the messages to load. * @param {Object} placeholders - A mapping of placeholder names to their replacement values. - * @returns {string | undefined} A formatted Reddit message with placeholders replaced, or undefined if messages couldn't be loaded. + * @returns {string | undefined} A formatted message with placeholders replaced, or undefined if messages couldn't be loaded. */ - public static getRandomRedditMessage(placeholders: { [key: string]: string }): string | undefined { - const redditMessages = this.loadRedditMessages() - if (!redditMessages) return - const message = redditMessages[Math.floor(Math.random() * redditMessages.length)]; - return this.replacePlaceholders(message, placeholders); - } - - /** - * Selects a random rDrama message from the loaded messages and replaces placeholders within it. - * - * @example - * const message = MessageService.getRandomRdramaMessage({ username: 'exampleUser' }); - * - * @param {Object} placeholders - A mapping of placeholder names to their replacement values. - * @returns {string | undefined} A formatted rDrama message with placeholders replaced, or undefined if messages couldn't be loaded. - */ - public static getRandomRdramaMessage(placeholders: { [key: string]: string }): string | undefined { - const rdramaMessages = this.loadRdramaMessages() - if (!rdramaMessages) return - const message = rdramaMessages[Math.floor(Math.random() * rdramaMessages.length)]; + public static getRandomMessage(fileName: MessageFileName, placeholders: { [key: string]: string }): string | undefined { + const messages = this.loadMessages(fileName); + if (!messages) return undefined; + const message = messages[Math.floor(Math.random() * messages.length)]; return this.replacePlaceholders(message, placeholders); } /** * Replaces placeholders in a message with values from a provided mapping. * - * @example - * const formattedMessage = MessageService.replacePlaceholders('Hello, {username}!', { username: 'exampleUser' }); - * * @param {string} message - The message containing placeholders. * @param {Object} placeholders - A mapping of placeholder names to their replacement values. * @returns {string} The message with placeholders replaced by actual values. @@ -93,4 +53,4 @@ export class MessageService { return acc.replace(regex, placeholders[key]); }, message); } -} \ No newline at end of file +}