diff --git a/src/index.ts b/src/index.ts index d70890c..4b486de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,17 @@ import { Client, Events, GatewayIntentBits } from 'discord.js'; import RoleService from './services/role'; import CommandService from './services/command'; +import DataService from './services/data'; const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }); client.once(Events.ClientReady, async (readyClient) => { - // Initialize & validate + // Initialize services + await DataService.init(); await CommandService.init(); - RoleService.validate(client.guilds.cache); + await RoleService.init(client.guilds.cache); // Ready! console.log(`Ready! Logged in as ${readyClient.user.tag}`); diff --git a/src/services/data.ts b/src/services/data.ts index 3c717cf..3d666ab 100644 --- a/src/services/data.ts +++ b/src/services/data.ts @@ -9,7 +9,7 @@ export default class DataService { limboMode: false }; - static async load() { + static async init() { this.file = Bun.file("./data.json"); if (!(await this.file.exists())) await Bun.write(this.file, JSON.stringify(this._data, null, 2)); @@ -17,13 +17,13 @@ export default class DataService { } static set data(data: Data) { - if (!this.file) throw new Error("DataService is not loaded. Please call DataService.load()"); + if (!this.file) throw new Error("DataService is not initialized. Please call DataService.init()"); this._data = data; void Bun.write(this.file, JSON.stringify(this._data, null, 2)); } static get data() { - if (!this.file) throw new Error("DataService is not loaded. Please call DataService.load()"); + if (!this.file) throw new Error("DataService is not initialized. Please call DataService.init()"); return this._data } } diff --git a/src/services/role.ts b/src/services/role.ts index 02b98b9..7b3ff22 100644 --- a/src/services/role.ts +++ b/src/services/role.ts @@ -1,4 +1,5 @@ import type { Guild, GuildMember, Collection, RoleCreateOptions } from "discord.js"; +import DataService from "./data"; export default abstract class RoleService { static readonly ROLES = { @@ -51,9 +52,11 @@ export default abstract class RoleService { static readonly STARTER_ROLES = [RoleService.ROLES["LURKER"], RoleService.ROLES["NEWFAG"]]; - public static limboMode = false; + public static get limboMode() { return DataService.data.limboMode; } + public static set limboMode(mode: boolean) { DataService.data = { ...DataService.data, limboMode: mode }; } - static validate(guilds: Collection): void { + static async init(guilds: Collection) { + // Check if any roles are missing guilds.forEach(guild => { const missing = Object.values(RoleService.ROLES).filter(role => !this.getRoleByName(guild, role.name)); if (missing.length) console.error(`${guild.name} is missing roles: ${missing.map(r => r.name).join(", ")}`); @@ -93,10 +96,6 @@ export default abstract class RoleService { } } - static async setLimboMode(mode: boolean) { - - } - static getRoleByName(guild: Guild, roleName?: string) { return guild.roles.cache.find(role => role.name === roleName); }