Implement persistance for limboMode boolean
This commit is contained in:
+4
-2
@@ -1,15 +1,17 @@
|
|||||||
import { Client, Events, GatewayIntentBits } from 'discord.js';
|
import { Client, Events, GatewayIntentBits } from 'discord.js';
|
||||||
import RoleService from './services/role';
|
import RoleService from './services/role';
|
||||||
import CommandService from './services/command';
|
import CommandService from './services/command';
|
||||||
|
import DataService from './services/data';
|
||||||
|
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
|
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
|
||||||
});
|
});
|
||||||
|
|
||||||
client.once(Events.ClientReady, async (readyClient) => {
|
client.once(Events.ClientReady, async (readyClient) => {
|
||||||
// Initialize & validate
|
// Initialize services
|
||||||
|
await DataService.init();
|
||||||
await CommandService.init();
|
await CommandService.init();
|
||||||
RoleService.validate(client.guilds.cache);
|
await RoleService.init(client.guilds.cache);
|
||||||
|
|
||||||
// Ready!
|
// Ready!
|
||||||
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export default class DataService {
|
|||||||
limboMode: false
|
limboMode: false
|
||||||
};
|
};
|
||||||
|
|
||||||
static async load() {
|
static async init() {
|
||||||
this.file = Bun.file("./data.json");
|
this.file = Bun.file("./data.json");
|
||||||
if (!(await this.file.exists()))
|
if (!(await this.file.exists()))
|
||||||
await Bun.write(this.file, JSON.stringify(this._data, null, 2));
|
await Bun.write(this.file, JSON.stringify(this._data, null, 2));
|
||||||
@@ -17,13 +17,13 @@ export default class DataService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static set data(data: Data) {
|
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;
|
this._data = data;
|
||||||
void Bun.write(this.file, JSON.stringify(this._data, null, 2));
|
void Bun.write(this.file, JSON.stringify(this._data, null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
static get data() {
|
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
|
return this._data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import type { Guild, GuildMember, Collection, RoleCreateOptions } from "discord.js";
|
import type { Guild, GuildMember, Collection, RoleCreateOptions } from "discord.js";
|
||||||
|
import DataService from "./data";
|
||||||
|
|
||||||
export default abstract class RoleService {
|
export default abstract class RoleService {
|
||||||
static readonly ROLES = {
|
static readonly ROLES = {
|
||||||
@@ -51,9 +52,11 @@ export default abstract class RoleService {
|
|||||||
|
|
||||||
static readonly STARTER_ROLES = [RoleService.ROLES["LURKER"], RoleService.ROLES["NEWFAG"]];
|
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<string, Guild>): void {
|
static async init(guilds: Collection<string, Guild>) {
|
||||||
|
// Check if any roles are missing
|
||||||
guilds.forEach(guild => {
|
guilds.forEach(guild => {
|
||||||
const missing = Object.values(RoleService.ROLES).filter(role => !this.getRoleByName(guild, role.name));
|
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(", ")}`);
|
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) {
|
static getRoleByName(guild: Guild, roleName?: string) {
|
||||||
return guild.roles.cache.find(role => role.name === roleName);
|
return guild.roles.cache.find(role => role.name === roleName);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user