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

Commit

Permalink
fixes to text channels lol
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasptsch committed Apr 23, 2022
1 parent a713d9f commit d1dcb68
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/classes/dynamicaChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface DynamicaChannelTypes {
prisma: Prisma.Primary;
};
secondary: {
prisma: Prisma.Secondary;
prisma: Prisma.Secondary & { guild: Prisma.Guild; primary: Prisma.Primary };
};
}

Expand Down
67 changes: 54 additions & 13 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 } from 'discord.js';
import { Client, Guild as DiscordGuild, Guild } from 'discord.js';

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

try {
const guild = await db.guild.findUnique({
where: { id: this.id },
include: { aliases: true },
});
const discordGuild = await this.client.guilds.cache.get(this.id);
const guild = await this.fetchPrisma();
const discordGuild = await this.fetchDiscord();
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 @@ -59,7 +93,7 @@ export default class DynamicaGuild {
if (!this.id) {
throw new Error('No Id defined');
}
const updatedGuild = await db.guild.update({
this.prisma = await db.guild.update({
where: { id: this.id },
include: {
aliases: true,
Expand All @@ -68,8 +102,6 @@ export default class DynamicaGuild {
allowJoinRequests: enabled,
},
});

this.prisma = updatedGuild;
return this;
}

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

this.prisma = updatedGuild;
return this;
}

/**
* Create a guild entry in prisma
*/
async create(id: Guild['id']) {
return db.guild.create({
data: {
id,
},
});
}
}
10 changes: 7 additions & 3 deletions src/classes/secondary.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import PrimaryClass from '@classes/primary';
import db from '@db';
import { Embed } from '@discordjs/builders';
import Prisma from '@prisma/client';
import Prisma, { Primary } from '@prisma/client';
import updateActivityCount from '@utils';
import formatChannelName from '@utils/format';
import logger from '@utils/logger';
Expand All @@ -10,7 +10,7 @@ import DynamicaChannel from './dynamicaChannel';

export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
/** The secondary channel as defined by prisma */
declare prisma: Prisma.Secondary;
// declare prisma: Prisma.Secondary & { guild: Guild; primary: Primary };

/** The discord text channel */
textChannel?: TextChannel;
Expand Down Expand Up @@ -181,6 +181,8 @@ export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
return undefined;
}
this.prisma = prisma;
this.prismaGuild = prisma.guild;
this.prismaPrimary = prisma.primary;
this.discord = discord;
if (prisma.textChannelId) {
const textChannel = await this.fetchDiscordText();
Expand All @@ -194,7 +196,9 @@ export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
return this;
}

async fetchPrisma(): Promise<Prisma.Secondary> {
async fetchPrisma(): Promise<
Prisma.Secondary & { guild: Prisma.Guild; primary: Primary }
> {
const prisma = await db.secondary.findUnique({
where: { id: this.id },
include: { guild: true, primary: true },
Expand Down
2 changes: 1 addition & 1 deletion src/commands/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default new Command()
interaction.client,
interaction.guildId
).fetch();
guild.setAllowJoin(state);
await guild.setTextEnabled(state);

await interaction.reply(
`Temporary text channels ${
Expand Down

0 comments on commit d1dcb68

Please sign in to comment.