retooled message service to be more generic added an enum for any new message banks

pull/2/head
j 2024-03-23 01:02:51 -04:00
parent de4da43242
commit 689df79ab0
1 changed files with 23 additions and 63 deletions

View File

@ -1,88 +1,48 @@
import fs from 'fs'; import fs from 'fs';
import path from 'path'; import path from 'path';
/** // Define an enum for message file names
* Manages the retrieval and formatting of messages stored in text files. export enum MessageFileName {
* This class provides functionality to load messages for rDrama and Reddit, RdramaPreviousMessage = 'rdrama_PreviousMessage.txt',
* select a random message, and replace placeholders within that message RdramaShouldntNotify = 'rdrama_ShouldntNotify.txt',
* with specified values. RedditMessages = 'reddit_messages.txt',
*/ RdramaMessages = 'rdrama_messages.txt',
}
export class MessageService { 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. * Each message is separated by '---END---' in the text file.
* *
* @example * @param {MessageFileName} fileName - The enum value representing the file containing the messages.
* const rdramaMessages = MessageService.loadRdramaMessages(); * @returns {string[] | undefined} An array of messages, or undefined if there was an error loading the messages.
*
* @returns {string[] | undefined} An array of rDrama messages, or undefined if there was an error loading the messages.
*/ */
private static loadRdramaMessages(): string[] | undefined { private static loadMessages(fileName: MessageFileName): string[] | undefined {
try { try {
const rdramaMessagesPath = path.join(__dirname, '..', 'messages', 'rdrama_messages.txt'); const messagesPath = path.join(__dirname, '..', 'messages', fileName);
return fs.readFileSync(rdramaMessagesPath, 'utf-8').split('---END---').filter(line => line.trim()); return fs.readFileSync(messagesPath, 'utf-8').split('---END---').filter(line => line.trim());
} catch (error) { } 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. * Selects a random message from the loaded messages and replaces placeholders within it.
* 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' });
* *
* @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. * @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 { public static getRandomMessage(fileName: MessageFileName, placeholders: { [key: string]: string }): string | undefined {
const redditMessages = this.loadRedditMessages() const messages = this.loadMessages(fileName);
if (!redditMessages) return if (!messages) return undefined;
const message = redditMessages[Math.floor(Math.random() * redditMessages.length)]; const message = messages[Math.floor(Math.random() * messages.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)];
return this.replacePlaceholders(message, placeholders); return this.replacePlaceholders(message, placeholders);
} }
/** /**
* Replaces placeholders in a message with values from a provided mapping. * 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 {string} message - The message containing placeholders.
* @param {Object} placeholders - A mapping of placeholder names to their replacement values. * @param {Object} placeholders - A mapping of placeholder names to their replacement values.
* @returns {string} The message with placeholders replaced by actual values. * @returns {string} The message with placeholders replaced by actual values.