Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Revert 92 eventmanager #93

Merged
merged 6 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions .vscode/emmet.code-snippets

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"editor.formatOnSave": true
"editor.formatOnSave": false
}
106 changes: 0 additions & 106 deletions src/classes/dynamicaChannel.ts

This file was deleted.

69 changes: 0 additions & 69 deletions src/classes/dynamicaEvent.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/classes/event.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ClientEvents } from 'discord.js';

export default class Event<K extends keyof ClientEvents> {
export default class Event {
public once: boolean;

public event: K;
public event: keyof ClientEvents;

public execute: (...args) => Promise<void>;

Expand All @@ -12,12 +12,12 @@ export default class Event<K extends keyof ClientEvents> {
return this;
}

setEvent(event: K) {
setEvent(event: keyof ClientEvents) {
this.event = event;
return this;
}

setResponse(response: (...args: ClientEvents[K]) => Promise<void>) {
setResponse(response: (...args) => Promise<void>) {
this.execute = response;
return this;
}
Expand Down
67 changes: 13 additions & 54 deletions src/classes/guild.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import db from '@db';
import { Alias, Guild as PrismaGuild } from '@prisma/client';
import logger from '@utils/logger';
import { Client, Guild as DiscordGuild, Guild } from 'discord.js';
import { Client, Guild as DiscordGuild } from 'discord.js';

export default class DynamicaGuild {
private client: Client<true>;
Expand Down Expand Up @@ -30,57 +30,23 @@ export default class DynamicaGuild {
if (!this.id) {
throw new Error('No Id defined.');
}

try {
const guild = await this.fetchPrisma();
const discordGuild = await this.fetchDiscord();
const guild = await db.guild.findUnique({
where: { id: this.id },
include: { aliases: true },
});
const discordGuild = await this.client.guilds.cache.get(this.id);
if (guild && discordGuild) {
this.prisma = guild;
this.discord = discordGuild;
}
return undefined;
} catch (error) {
logger.error("Couldn't fetch guild:", error);
}
return this;
}

async fetchPrisma(): Promise<
PrismaGuild & {
aliases: Alias[];
}
> {
const guild = await db.guild.findUnique({
where: { id: this.id },
include: { aliases: true },
});
if (!guild) {
logger.info('fetch prisma');
return db.guild.create({
data: { id: this.id },
include: { aliases: true },
});
} else {
return guild;
}
}

async fetchDiscord(): Promise<DiscordGuild> {
const discord = await this.client.guilds.cache.get(this.id);
if (!discord) {
this.deletePrisma();
return undefined;
}
return discord;
}

async deletePrisma(): Promise<PrismaGuild | undefined> {
const guild = await db.guild.findUnique({ where: { id: this.id } });
if (!guild) {
return undefined;
}
return db.guild.delete({ where: { id: this.id } });
}

/**
* Change the setting allowing people to request to join a locked channel.
* @param enabled the state to set the setting to
Expand All @@ -93,7 +59,7 @@ export default class DynamicaGuild {
if (!this.id) {
throw new Error('No Id defined');
}
this.prisma = await db.guild.update({
const updatedGuild = await db.guild.update({
where: { id: this.id },
include: {
aliases: true,
Expand All @@ -102,6 +68,8 @@ export default class DynamicaGuild {
allowJoinRequests: enabled,
},
});

this.prisma = updatedGuild;
return this;
}

Expand All @@ -117,24 +85,15 @@ export default class DynamicaGuild {
if (!this.id) {
throw new Error('No Id defined');
}
this.prisma = await db.guild.update({
const updatedGuild = await db.guild.update({
where: { id: this.id },
include: { aliases: true },
data: {
textChannelsEnabled: enabled,
},
});
return this;
}

/**
* Create a guild entry in prisma
*/
async create(id: Guild['id']) {
return db.guild.create({
data: {
id,
},
});
this.prisma = updatedGuild;
return this;
}
}
Loading