Skip to content

Commit

Permalink
feat: add missing guild events
Browse files Browse the repository at this point in the history
  • Loading branch information
ijsKoud committed Apr 25, 2023
1 parent 27bf5b5 commit 3874315
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/database/DatabaseManager.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
}
}
12 changes: 12 additions & 0 deletions src/discord/bot/listeners/guild/GuildCreate.ts
Original file line number Diff line number Diff line change
@@ -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<EventListenerOptions>({ name: "guildCreate" })
export default class extends EventListener<GitCordClient> {
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}`);
}
}
12 changes: 12 additions & 0 deletions src/discord/bot/listeners/guild/GuildDelete.ts
Original file line number Diff line number Diff line change
@@ -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<EventListenerOptions>({ name: "guildDelete" })
export default class extends EventListener<GitCordClient> {
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}`);
}
}

0 comments on commit 3874315

Please sign in to comment.