44 lines
1.7 KiB
TypeScript
44 lines
1.7 KiB
TypeScript
|
import fs from 'fs';
|
||
|
import path from 'path';
|
||
|
|
||
|
export class MessageService {
|
||
|
private redditMessages: string[] = [];
|
||
|
private rdramaMessages: string[] = [];
|
||
|
|
||
|
constructor() {
|
||
|
this.loadMessages();
|
||
|
}
|
||
|
|
||
|
private loadMessages() {
|
||
|
try {
|
||
|
const redditMessagesPath = path.join(__dirname, 'messages', 'reddit_messages.txt');
|
||
|
this.redditMessages = fs.readFileSync(redditMessagesPath, 'utf-8').split('---END---').filter(line => line.trim());
|
||
|
} catch (error) {
|
||
|
console.error('Failed to load Reddit messages:', error);
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
const rdramaMessagesPath = path.join(__dirname, 'messages', 'rdrama_messages.txt');
|
||
|
this.rdramaMessages = fs.readFileSync(rdramaMessagesPath, 'utf-8').split('---END---').filter(line => line.trim());
|
||
|
} catch (error) {
|
||
|
console.error('Failed to load rDrama messages:', error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public getRandomRedditMessage(placeholders: { [key: string]: string }): string {
|
||
|
const message = this.redditMessages[Math.floor(Math.random() * this.redditMessages.length)];
|
||
|
return this.replacePlaceholders(message, placeholders);
|
||
|
}
|
||
|
|
||
|
public getRandomRdramaMessage(placeholders: { [key: string]: string }): string {
|
||
|
const message = this.rdramaMessages[Math.floor(Math.random() * this.rdramaMessages.length)];
|
||
|
return this.replacePlaceholders(message, placeholders);
|
||
|
}
|
||
|
|
||
|
private replacePlaceholders(message: string, placeholders: { [key: string]: string }): string {
|
||
|
return Object.keys(placeholders).reduce((acc, key) => {
|
||
|
const regex = new RegExp(`{${key}}`, 'g');
|
||
|
return acc.replace(regex, placeholders[key]);
|
||
|
}, message);
|
||
|
}
|
||
|
}
|