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

Commit

Permalink
fixes and exploring more types
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasptsch committed Apr 23, 2022
1 parent d1dcb68 commit 9039def
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 64 deletions.
8 changes: 6 additions & 2 deletions src/classes/dynamicaChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Client, Guild, VoiceChannel } from 'discord.js';

interface DynamicaChannelTypes {
primary: {
prisma: Prisma.Primary;
prisma: Prisma.Primary & {
guild: Prisma.Guild;
secondaries: Prisma.Secondary[];
};
};
secondary: {
prisma: Prisma.Secondary & { guild: Prisma.Guild; primary: Prisma.Primary };
Expand Down Expand Up @@ -34,10 +37,11 @@ export default class DynamicaChannel<K extends keyof DynamicaChannelTypes> {
this.client = client;
if (channelId) {
this.id = channelId;
this.fetch();
}
}

async create(...args): Promise<DynamicaChannel<K>> {
async create(...args) {
return this;
}
async fetch(): Promise<DynamicaChannel<K> | undefined> {
Expand Down
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 {
export default class Event<K extends keyof ClientEvents> {
public once: boolean;

public event: keyof ClientEvents;
public event: K;

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

Expand All @@ -12,12 +12,12 @@ export default class Event {
return this;
}

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

setResponse(response: (...args) => Promise<void>) {
setResponse(response: (...args: ClientEvents[K]) => Promise<void>) {
this.execute = response;
return this;
}
Expand Down
70 changes: 37 additions & 33 deletions src/classes/secondary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import PrimaryClass from '@classes/primary';
import DynamicaPrimary from '@classes/primary';
import db from '@db';
import { Embed } from '@discordjs/builders';
import Prisma, { Primary } from '@prisma/client';
Expand All @@ -25,7 +25,7 @@ export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
* @param member guild member who created the channel
* @returns
*/
async create(primary: PrimaryClass, guild: Guild, member: GuildMember) {
async create(primary: DynamicaPrimary, guild: Guild, member: GuildMember) {
try {
await primary.fetch();

Expand Down Expand Up @@ -174,7 +174,7 @@ export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
const prisma = await this.fetchPrisma();
const discord = await this.fetchDiscord();
if (!discord) {
await this.deletePrisma();
await this.delete();
return undefined;
}
if (!prisma) {
Expand Down Expand Up @@ -207,8 +207,11 @@ export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
}

async fetchDiscordText(): Promise<TextChannel> {
const channel = await this.client.channels.cache.get(this.id);
if (!channel || channel.type !== 'GUILD_TEXT') {
const channel = await this.client.channels.cache.get(this.textChannel?.id);
if (!channel) {
return undefined;
}
if (channel.type !== 'GUILD_TEXT') {
return undefined;
}
return channel;
Expand Down Expand Up @@ -328,29 +331,21 @@ export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
try {
const discord = await this.fetchDiscord();
const prisma = await this.fetchPrisma();
const textChannel = await this.fetchDiscordText();
if (textChannel) {
await textChannel.delete();
}
if (discord) {
discord.delete();
await this.deleteDiscord();
}
if (prisma) {
db.secondary.delete({ where: { id: this.id } });
await this.deletePrisma();
}
// if (!this.discord && !this.prisma) {
// return;
// }
// if (!this.discord && !!this.prisma) {
// await this.deletePrisma();
// } else if (!this.prisma && !!this.discord) {
// await this.deleteDiscord();
// } else if (this.discord && this.prisma) {
// await this.deletePrisma();
// await this.deleteDiscord();
// }
console.trace();
logger.debug(`Secondary channel deleted ${this.id}.`);
} catch (error) {
logger.error(error);
}

await updateActivityCount(this.client);
}

async deletePrisma(): Promise<void> {
Expand All @@ -359,7 +354,9 @@ export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
}
const prisma = await this.fetchPrisma();
if (prisma) {
await db.secondary.delete({ where: { id: this.id } });
await db.secondary.delete({ where: { id: this.id } }).then(() => {
updateActivityCount(this.client);
});
}
}

Expand All @@ -371,22 +368,29 @@ export default class DynamicaSecondary extends DynamicaChannel<'secondary'> {
throw new Error('No Id defined.');
}
try {
if (!this.discord.deletable) {
throw new Error('The channel is not deletable.');
const discord = await this.fetchDiscord();
if (discord) {
await discord.delete();
}
} catch (error) {
logger.error('Failed to delete secondary:', error);
}
}

await updateActivityCount(this.client);

await this.discord.delete();

if (this.textChannel) {
const textChannel = this.discord.guild.channels.cache.get(
this.prisma.textChannelId
);
await textChannel.delete();
async deleteDiscordText(): Promise<void> {
if (!this.client) {
throw new Error('No client defined.');
}
if (!this.id) {
throw new Error('No Id defined.');
}
try {
const discordText = await this.fetchDiscordText();
if (discordText) {
await discordText.delete();
}
} catch (error) {
logger.error('Failed to delete secondary:', error);
logger.error('Failed to delete secondary text channel', error);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/events/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import autocompletes from '@autocompletes';
import Autocomplete from '@classes/autocomplete';
import Event from '@classes/event';
import logger from '@utils/logger';
import { Interaction } from 'discord.js';

export default new Event()
export default new Event<'interactionCreate'>()
.setOnce(false)
.setEvent('interactionCreate')
.setResponse(async (interaction: Interaction) => {
.setResponse(async (interaction) => {
if (!interaction.isAutocomplete()) return;
try {
const autocomplete: Autocomplete = autocompletes[interaction.commandName];
Expand Down
5 changes: 2 additions & 3 deletions src/events/channelDelete.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Event from '@classes/event';
import DynamicaPrimary from '@classes/primary';
import DynamicaSecondary from '@classes/secondary';
import { Channel, DMChannel } from 'discord.js';

export default new Event()
export default new Event<'channelDelete'>()
.setOnce(false)
.setEvent('channelDelete')
.setResponse(async (channel: Channel | DMChannel) => {
.setResponse(async (channel) => {
// logger.debug(channel);
const primary = await new DynamicaPrimary(
channel.client,
Expand Down
5 changes: 2 additions & 3 deletions src/events/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import commands from '@commands';
import checkGuild from '@preconditions/guild';
import { ErrorEmbed } from '@utils/discordEmbeds';
import logger from '@utils/logger';
import { CacheType, Interaction } from 'discord.js';

export default new Event()
export default new Event<'interactionCreate'>()
.setOnce(false)
.setEvent('interactionCreate')
.setResponse(async (interaction: Interaction<CacheType>) => {
.setResponse(async (interaction) => {
if (!interaction.isCommand()) return;
try {
const command = commands[interaction.commandName];
Expand Down
5 changes: 2 additions & 3 deletions src/events/guildCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Event from '@classes/event';
import db from '@db';
import { Embed, hyperlink } from '@discordjs/builders';
import logger from '@utils/logger';
import { Guild } from 'discord.js';
/**
* The list of basic commands to display.
*/
Expand Down Expand Up @@ -74,10 +73,10 @@ const botInfoEmbed = new Embed()
iconURL: 'https://dynamica.dev/img/dynamica.png',
});

export default new Event()
export default new Event<'guildCreate'>()
.setOnce(false)
.setEvent('guildCreate')
.setResponse(async (guild: Guild) => {
.setResponse(async (guild) => {
if (!guild.me.permissions.has('ADMINISTRATOR')) {
const owner = await guild.fetchOwner();
owner.send(
Expand Down
5 changes: 2 additions & 3 deletions src/events/guildDelete.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Event from '@classes/event';
import db from '@db';
import logger from '@utils/logger';
import { Guild } from 'discord.js';

export default new Event()
export default new Event<'guildDelete'>()
.setOnce(false)
.setEvent('guildDelete')
.setResponse(async (guild: Guild) => {
.setResponse(async (guild) => {
const manager = await guild.channels.cache.get('Dynamica Manager');
try {
await manager?.delete();
Expand Down
3 changes: 2 additions & 1 deletion src/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Event from '@/classes/event';
import { ClientEvents } from 'discord.js';
import autocomplete from './autocomplete';
import channelDelete from './channelDelete';
import command from './command';
Expand All @@ -9,7 +10,7 @@ import ready from './ready';
import voiceStateUpdate from './voiceStateUpdate';

interface EventsInterface {
[key: string]: Event;
[key: string]: Event<keyof ClientEvents>;
}

const exports: EventsInterface = {
Expand Down
2 changes: 1 addition & 1 deletion src/events/presenceUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Event from '@classes/event';
import DynamicaSecondary from '@classes/secondary';
import { Presence } from 'discord.js';

export default new Event()
export default new Event<'presenceUpdate'>()
.setOnce(false)
.setEvent('presenceUpdate')
.setResponse(async (oldPresence: Presence, newPresence: Presence) => {
Expand Down
7 changes: 3 additions & 4 deletions src/events/rateLimit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Event from '@classes/event';
import logger from '@utils/logger';
import { RateLimitData } from 'discord.js';

export default new Event()
export default new Event<'rateLimit'>()
.setOnce(false)
.setEvent('guildCreate')
.setResponse(async (data: RateLimitData) => {
.setEvent('rateLimit')
.setResponse(async (data) => {
logger.info(data);
});
5 changes: 2 additions & 3 deletions src/events/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import DynamicaSecondary from '@classes/secondary';
import db from '@db';
import updateActivityCount from '@utils';
import logger from '@utils/logger';
import { Client } from 'discord.js';

export default new Event()
export default new Event<'ready'>()
.setOnce(true)
.setEvent('ready')
.setResponse(async (client: Client<true>) => {
.setResponse(async (client) => {
try {
const secondaries = await db.secondary.findMany();
const primaries = await db.primary.findMany();
Expand Down
2 changes: 1 addition & 1 deletion src/events/voiceStateUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import DynamicaPrimary from '@classes/primary';
import DynamicaSecondary from '@classes/secondary';
import { VoiceState } from 'discord.js';

export default new Event()
export default new Event<'voiceStateUpdate'>()
.setOnce(false)
.setEvent('voiceStateUpdate')
.setResponse(async (oldVoiceState: VoiceState, newVoiceState: VoiceState) => {
Expand Down

0 comments on commit 9039def

Please sign in to comment.