124 lines
3.2 KiB
TypeScript
124 lines
3.2 KiB
TypeScript
|
import fs from 'fs';
|
||
|
import path from 'path';
|
||
|
import { config } from '../config/config';
|
||
|
import { DEFAULT_THEME_ID, getTheme } from './themes';
|
||
|
|
||
|
// Define the structure of guild settings
|
||
|
interface GuildSettings {
|
||
|
id: string;
|
||
|
themeId: string;
|
||
|
pronoun: string;
|
||
|
responseChance: number;
|
||
|
}
|
||
|
|
||
|
// Class to manage guild settings
|
||
|
export class GuildSettingsManager {
|
||
|
private settings: Map<string, GuildSettings> = new Map();
|
||
|
private settingsFilePath: string;
|
||
|
|
||
|
constructor(storagePath: string = path.join(__dirname, '..', '..', 'data')) {
|
||
|
// Ensure storage directory exists
|
||
|
if (!fs.existsSync(storagePath)) {
|
||
|
fs.mkdirSync(storagePath, { recursive: true });
|
||
|
}
|
||
|
|
||
|
this.settingsFilePath = path.join(storagePath, 'guild-settings.json');
|
||
|
this.loadSettings();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Load settings from file
|
||
|
*/
|
||
|
private loadSettings(): void {
|
||
|
try {
|
||
|
if (fs.existsSync(this.settingsFilePath)) {
|
||
|
const data = fs.readFileSync(this.settingsFilePath, 'utf8');
|
||
|
const settingsArray: GuildSettings[] = JSON.parse(data);
|
||
|
|
||
|
for (const guild of settingsArray) {
|
||
|
this.settings.set(guild.id, guild);
|
||
|
}
|
||
|
|
||
|
console.log(`Loaded settings for ${this.settings.size} guilds`);
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('Error loading guild settings:', error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Save settings to file
|
||
|
*/
|
||
|
private saveSettings(): void {
|
||
|
try {
|
||
|
const settingsArray = Array.from(this.settings.values());
|
||
|
fs.writeFileSync(this.settingsFilePath, JSON.stringify(settingsArray, null, 2));
|
||
|
} catch (error) {
|
||
|
console.error('Error saving guild settings:', error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get settings for a guild, creating default settings if none exist
|
||
|
*/
|
||
|
getSettings(guildId: string): GuildSettings {
|
||
|
if (!this.settings.has(guildId)) {
|
||
|
const defaultSettings: GuildSettings = {
|
||
|
id: guildId,
|
||
|
themeId: config.defaultTheme,
|
||
|
pronoun: config.catPersonalization.pronoun,
|
||
|
responseChance: config.messageResponseChance,
|
||
|
};
|
||
|
|
||
|
this.settings.set(guildId, defaultSettings);
|
||
|
this.saveSettings();
|
||
|
}
|
||
|
|
||
|
return this.settings.get(guildId)!;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the theme for a guild
|
||
|
*/
|
||
|
setTheme(guildId: string, themeId: string): boolean {
|
||
|
const settings = this.getSettings(guildId);
|
||
|
|
||
|
// Validate theme ID
|
||
|
const theme = getTheme(themeId);
|
||
|
if (!theme) return false;
|
||
|
|
||
|
settings.themeId = themeId;
|
||
|
settings.pronoun = theme.defaultPronoun;
|
||
|
this.saveSettings();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the pronoun for a guild
|
||
|
*/
|
||
|
setPronoun(guildId: string, pronoun: string): void {
|
||
|
const settings = this.getSettings(guildId);
|
||
|
settings.pronoun = pronoun;
|
||
|
this.saveSettings();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the response chance for a guild
|
||
|
*/
|
||
|
setResponseChance(guildId: string, chance: number): void {
|
||
|
const settings = this.getSettings(guildId);
|
||
|
settings.responseChance = Math.max(0, Math.min(100, chance));
|
||
|
this.saveSettings();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Get an array of all guilds and their settings
|
||
|
*/
|
||
|
getAllSettings(): GuildSettings[] {
|
||
|
return Array.from(this.settings.values());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Create a singleton instance
|
||
|
export const guildSettings = new GuildSettingsManager();
|