Skip to content

Commit

Permalink
🏷️ feat(framework/types): add EveryChannelPayloads for easy life(seri…
Browse files Browse the repository at this point in the history
…ously)
  • Loading branch information
Helloyunho committed May 18, 2024
1 parent e0bf5f3 commit 4f67351
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 19 deletions.
3 changes: 2 additions & 1 deletion framework/src/gateway/handlers/channelCreate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { EveryChannelPayloads } from "../../../types/channel.ts";
import { GatewayHandler } from "../../../types/mod.ts";

const channelCreate: GatewayHandler<"CHANNEL_CREATE"> = (
client,
[_, channel],
) => {
client.channels.set(channel.id, channel);
client.channels.set(channel.id, channel as EveryChannelPayloads);
const channelObj = client.channels.get(channel.id)!;
client.emit("channelCreate", channelObj);
};
Expand Down
17 changes: 11 additions & 6 deletions framework/src/managers/channels.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { type ChannelPayload } from "../../../types/mod.ts";
import { Channel } from "../structures/channels/channel.ts";
import type {
EveryChannelPayloads,
EveryChannels,
} from "../../types/channel.ts";
import { createChannel } from "../utils/channel.ts";
import { BaseManager } from "./base.ts";

export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
_get<P extends ChannelPayload>(id: string): P | undefined {
export class ChannelsManager
extends BaseManager<EveryChannelPayloads, EveryChannels> {
_get<P extends EveryChannelPayloads>(id: string): P | undefined {
return this.cache.get(id) as P | undefined;
}

async _fetch<P extends ChannelPayload>(id: string): Promise<P | undefined> {
async _fetch<P extends EveryChannelPayloads>(
id: string,
): Promise<P | undefined> {
try {
const resp: P | undefined = await this.client.rest.get(
`/channels/${id}`,
Expand All @@ -28,7 +33,7 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
if (!cached) return;
return createChannel(this.client, cached);
}
async fetch<P extends ChannelPayload>(
async fetch<P extends EveryChannelPayloads>(
id: string,
) {
try {
Expand Down
21 changes: 10 additions & 11 deletions framework/src/utils/channel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ChannelType } from "../../../types/mod.ts";
import type {
ChannelPayload,
DMChannelPayload,
GroupDMChannelPayload,
GuildAnnouncementChannelPayload,
Expand All @@ -11,7 +10,7 @@ import type {
GuildVoiceChannelPayload,
} from "../../../types/mod.ts";
import type { Client } from "../client/mod.ts";
import { EveryChannels } from "../../types/channel.ts";
import { EveryChannelPayloads, EveryChannels } from "../../types/channel.ts";
import {
DMChannel,
GroupDMChannel,
Expand All @@ -23,54 +22,54 @@ import {
GuildVoiceChannel,
} from "../structures/channels/mod.ts";

export const createChannel = <P extends ChannelPayload>(
export const createChannel = <P extends EveryChannelPayloads>(
client: Client,
payload: P,
): EveryChannels => {
switch (payload.type) {
case ChannelType.GUILD_TEXT:
return new GuildTextChannel(
client,
payload as unknown as GuildTextChannelPayload,
payload as GuildTextChannelPayload,
);
case ChannelType.DM:
return new DMChannel(
client,
payload as unknown as DMChannelPayload,
payload as DMChannelPayload,
);
case ChannelType.GUILD_VOICE:
return new GuildVoiceChannel(
client,
payload as unknown as GuildVoiceChannelPayload,
payload as GuildVoiceChannelPayload,
);
case ChannelType.GROUP_DM:
return new GroupDMChannel(
client,
payload as unknown as GroupDMChannelPayload,
payload as GroupDMChannelPayload,
);
case ChannelType.GUILD_CATEGORY:
return new GuildCategory(
client,
payload as unknown as GuildCategoryPayload,
payload as GuildCategoryPayload,
);
case ChannelType.GUILD_ANNOUNCEMENT:
return new GuildAnnouncementChannel(
client,
payload as unknown as GuildAnnouncementChannelPayload,
payload as GuildAnnouncementChannelPayload,
);
// case ChannelType.ANNOUNCEMENT_THREAD:
// case ChannelType.GUILD_PUBLIC_THREAD:
// case ChannelType.GUILD_PRIVATE_THREAD:
case ChannelType.GUILD_STAGE_VOICE:
return new GuildStageChannel(
client,
payload as unknown as GuildStageChannelPayload,
payload as GuildStageChannelPayload,
);
// case ChannelType.GUILD_DIRECTORY:
case ChannelType.GUILD_FORUM:
return new GuildForumChannel(
client,
payload as unknown as GuildForumChannelPayload,
payload as GuildForumChannelPayload,
);
// case ChannelType.GUILD_MEDIA:
default:
Expand Down
45 changes: 44 additions & 1 deletion framework/types/channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { ChannelType } from "../../mod.ts";
import {
ChannelType,
DMChannelPayload,
GroupDMChannelPayload,
GuildAnnouncementChannelPayload,
GuildCategoryPayload,
GuildForumChannelPayload,
GuildStageChannelPayload,
GuildTextChannelPayload,
GuildVoiceChannelPayload,
} from "../../types/mod.ts";
import type {
DMChannel,
GroupDMChannel,
Expand All @@ -10,6 +20,39 @@ import type {
GuildVoiceChannel,
} from "../src/structures/channels/mod.ts";

export type GuildTextBasedChannelPayloads =
| GuildAnnouncementChannelPayload
| GuildForumChannelPayload
| GuildTextChannelPayload
| GuildVoiceChannelPayload;

export type GuildThreadAvailableChannelPayloads =
| GuildAnnouncementChannelPayload
| GuildForumChannelPayload
| GuildTextChannelPayload;

export type GuildVoiceBasedChannelPayloads =
| GuildStageChannelPayload
| GuildVoiceChannelPayload;

export type GuildChannelPayloads =
| GuildTextBasedChannelPayloads
| GuildVoiceBasedChannelPayloads
| GuildThreadAvailableChannelPayloads
| GuildCategoryPayload;

export type DMChannelPayloads = DMChannelPayload | GroupDMChannelPayload;

export type VoiceChannelPayloads =
| GuildVoiceBasedChannelPayloads
| DMChannelPayloads;

export type TextChannelPayloads =
| GuildTextBasedChannelPayloads
| DMChannelPayloads;

export type EveryChannelPayloads = GuildChannelPayloads | DMChannelPayloads;

export type GuildTextBasedChannels =
| GuildAnnouncementChannel
| GuildForumChannel
Expand Down

0 comments on commit 4f67351

Please sign in to comment.