82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import fs from 'fs';
|
|
|
|
interface Role {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
interface RolesConfig {
|
|
helperroles: Role[];
|
|
educationroles: Role[];
|
|
languageroles: Role[];
|
|
locationroles: Role[];
|
|
pingroles: Role[];
|
|
}
|
|
|
|
interface Birthday {
|
|
day: number;
|
|
month: number;
|
|
timezone?: string;
|
|
}
|
|
|
|
interface BirthdaysConfig {
|
|
[userId: string]: Birthday;
|
|
}
|
|
|
|
interface ReplyTextConfig {
|
|
[key: string]: string;
|
|
}
|
|
|
|
export default class Config {
|
|
private roles: RolesConfig | null;
|
|
private replyText: ReplyTextConfig | null;
|
|
private bdays: BirthdaysConfig | null;
|
|
|
|
constructor() {
|
|
this.roles = null;
|
|
this.replyText = null;
|
|
this.bdays = null;
|
|
|
|
this.loadConfig();
|
|
}
|
|
|
|
async loadConfig(): Promise<void> {
|
|
let rawdata: Buffer;
|
|
|
|
// Load the roles.json file
|
|
rawdata = fs.readFileSync('./config/roles.json');
|
|
this.roles = JSON.parse(rawdata.toString());
|
|
|
|
rawdata = fs.readFileSync('./config/replytext.json');
|
|
this.replyText = JSON.parse(rawdata.toString());
|
|
|
|
rawdata = fs.readFileSync('./storage/bdays.json');
|
|
this.bdays = JSON.parse(rawdata.toString());
|
|
}
|
|
|
|
getRoles(): RolesConfig | null { return this.roles; }
|
|
getHelperRoles(): Role[] { return this.roles?.helperroles || []; }
|
|
getEducationRoles(): Role[] { return this.roles?.educationroles || []; }
|
|
getLanguageRoles(): Role[] { return this.roles?.languageroles || []; }
|
|
getLocationRoles(): Role[] { return this.roles?.locationroles || []; }
|
|
getPingRoles(): Role[] { return this.roles?.pingroles || []; }
|
|
|
|
getReplyText(): ReplyTextConfig | null { return this.replyText; }
|
|
|
|
getBdays(): BirthdaysConfig | null { return this.bdays; }
|
|
getBday(userId: string): Birthday | null {
|
|
return this.bdays?.[userId] || null;
|
|
}
|
|
setBday(userId: string, bday: Birthday): void {
|
|
if (!this.bdays) this.bdays = {};
|
|
this.bdays[userId] = bday;
|
|
fs.writeFileSync('./storage/bdays.json', JSON.stringify(this.bdays, null, 2));
|
|
}
|
|
removeBday(userId: string): void {
|
|
if (this.bdays?.[userId]) {
|
|
delete this.bdays[userId];
|
|
fs.writeFileSync('./storage/bdays.json', JSON.stringify(this.bdays, null, 2));
|
|
}
|
|
}
|
|
}
|