diff --git a/src/database/DatabaseManager.ts b/src/database/DatabaseManager.ts index d4e9ec2..3914e24 100644 --- a/src/database/DatabaseManager.ts +++ b/src/database/DatabaseManager.ts @@ -1,5 +1,5 @@ import type GitCordClient from "#discord/lib/GitCordClient.js"; -import { Collection } from "discord.js"; +import { Collection, Guild } from "discord.js"; import GitCordGuild from "./structures/Guild.js"; export default class DatabaseManager { @@ -33,4 +33,28 @@ export default class DatabaseManager { this.configs.set(guildConfig.guildId, guildConfig); } } + + /** + * Creates a config for a guild + * @param guild The guild which requires a config + */ + public async createGuild(guild: Guild) { + const prismaGuild = await this.client.prisma.guild.create({ data: { guildId: guild.id }, include: { guildWebhooks: true } }); + const gitcordGuild = new GitCordGuild(this.client); + gitcordGuild.init(prismaGuild); + + this.configs.set(guild.id, gitcordGuild); + } + + /** + * Deletes a guild config from the database and cache + * @param guildId The id of the guild to delete + */ + public async deleteGuild(guildId: string) { + if (!this.configs.has(guildId)) return; + + await this.client.prisma.guildWebhook.deleteMany({ where: { guildId } }); + await this.client.prisma.guild.delete({ where: { guildId } }); + this.configs.delete(guildId); + } } diff --git a/src/discord/bot/listeners/guild/GuildCreate.ts b/src/discord/bot/listeners/guild/GuildCreate.ts new file mode 100644 index 0000000..79b7d07 --- /dev/null +++ b/src/discord/bot/listeners/guild/GuildCreate.ts @@ -0,0 +1,12 @@ +import type GitCordClient from "#discord/lib/GitCordClient.js"; +import { ApplyOptions, EventListener, type EventListenerOptions } from "@snowcrystals/iglo"; +import { bold } from "colorette"; +import { Guild } from "discord.js"; + +@ApplyOptions({ name: "guildCreate" }) +export default class extends EventListener { + public override async run(guild: Guild) { + await this.client.databaseManager.createGuild(guild); + this.client.logger.debug(`(GuildCreate Event): Config created for guild ${bold(guild.name)}: ${guild.id}`); + } +} diff --git a/src/discord/bot/listeners/guild/GuildDelete.ts b/src/discord/bot/listeners/guild/GuildDelete.ts new file mode 100644 index 0000000..784e2a9 --- /dev/null +++ b/src/discord/bot/listeners/guild/GuildDelete.ts @@ -0,0 +1,12 @@ +import type GitCordClient from "#discord/lib/GitCordClient.js"; +import { ApplyOptions, EventListener, type EventListenerOptions } from "@snowcrystals/iglo"; +import { bold } from "colorette"; +import { Guild } from "discord.js"; + +@ApplyOptions({ name: "guildDelete" }) +export default class extends EventListener { + public override async run(guild: Guild) { + await this.client.databaseManager.deleteGuild(guild.id); + this.client.logger.debug(`(GuildDelete Event): Config deleted of guild ${bold(guild.name)}: ${guild.id}`); + } +}