Skip to content

Commit

Permalink
✨ feat: uhhhhhh i did sth
Browse files Browse the repository at this point in the history
  • Loading branch information
Helloyunho committed Sep 15, 2023
1 parent b09e6c5 commit 343df6d
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
scripts/api_docs
.DS_Store
.idea/
**/dummy.ts
7 changes: 5 additions & 2 deletions framework/src/client/events.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { EveryChannels } from "../../types/channel.ts";
import type { Guild } from "../structures/guilds/guild.ts";
import type { Message } from "../structures/mod.ts";

// ...this is gonna be very painful to fix
export type ClientEvents = {
messageCreate: [Message];
messageCreate: [message: Message];
ready: [];
guildCreate: [Guild];
guildCreate: [guild: Guild];
channelCreate: [channel: EveryChannels];
channelDelete: [channel: EveryChannels];
};
12 changes: 12 additions & 0 deletions framework/src/gateway/handlers/channelCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GatewayHandler } from "../../../types/mod.ts";

const channelCreate: GatewayHandler<"CHANNEL_CREATE"> = (
client,
[_, channel],
) => {
client.channels.set(channel.id, channel);
const channelObj = client.channels.get(channel.id)!;
client.emit("channelCreate", channelObj);
};

export default channelCreate;
12 changes: 12 additions & 0 deletions framework/src/gateway/handlers/channelDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GatewayHandler } from "../../../types/mod.ts";

const channelDelete: GatewayHandler<"CHANNEL_DELETE"> = (
client,
[_, channel],
) => {
const channelObj = client.channels.get(channel.id)!;
client.channels.delete(channel.id);
client.emit("channelDelete", channelObj);
};

export default channelDelete;
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { GatewayHandler } from "../../types/mod.ts";
import { GatewayHandler } from "../../../types/mod.ts";
import { Guild } from "../../structures/guilds/guild.ts";

const guildCreate: GatewayHandler<"GUILD_CREATE"> = async (
client,
[_, guild],
) => {
client.guilds.set(guild.id, guild);

const guildObj = client.guilds.get(guild.id)!;
const guildObj = new Guild(client, guild);
await guildObj.channels.fetchAll();

client.emit("guildCreate", guildObj);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MessagePayload } from "../../../types/mod.ts";
import type { GatewayHandler } from "../../types/gateway.ts";
import type { Client } from "../client/mod.ts";
import { Message } from "../structures/messages/mod.ts";
import type { MessagePayload } from "../../../../types/mod.ts";
import type { GatewayHandler } from "../../../types/gateway.ts";
import type { Client } from "../../client/mod.ts";
import { Message } from "../../structures/messages/mod.ts";

const messageCreate: GatewayHandler<"MESSAGE_CREATE"> = (
client: Client,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { GatewayReadyPayload } from "../../../types/mod.ts";
import { GatewayHandler } from "../../types/gateway.ts";
import type { Client } from "../client/mod.ts";
import { User } from "../structures/mod.ts";
import { GatewayReadyPayload } from "../../../../types/mod.ts";
import { GatewayHandler } from "../../../types/gateway.ts";
import type { Client } from "../../client/mod.ts";
import { User } from "../../structures/mod.ts";

const ready: GatewayHandler<"READY"> = (
client: Client,
Expand Down
10 changes: 7 additions & 3 deletions framework/src/gateway/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { GatewayHandler } from "../../types/gateway.ts";
import guildCreate from "./guildCreate.ts";
import messageCreate from "./messageCreate.ts";
import ready from "./ready.ts";
import channelCreate from "./handlers/channelCreate.ts";
import channelDelete from "./handlers/channelDelete.ts";
import guildCreate from "./handlers/guildCreate.ts";
import messageCreate from "./handlers/messageCreate.ts";
import ready from "./handlers/ready.ts";

// deno-lint-ignore no-explicit-any
export const GatewayHandlers: { [K: string]: GatewayHandler<any> } = {
MESSAGE_CREATE: messageCreate,
READY: ready,
GUILD_CREATE: guildCreate,
CHANNEL_CREATE: channelCreate,
CHANNEL_DELETE: channelDelete,
};
24 changes: 4 additions & 20 deletions framework/src/managers/channels.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
import {
type ChannelPayload,
ChannelType,
type GuildTextChannelPayload,
} from "../../../types/mod.ts";
import { type ChannelPayload } from "../../../types/mod.ts";
import { Channel } from "../structures/channels/channel.ts";
import { GuildTextChannel } from "../structures/channels/guildTextChannel.ts";
import { createChannel } from "../utils/channel.ts";
import { BaseManager } from "./base.ts";

export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
private _createChannel<P extends ChannelPayload>(payload: P) {
switch (payload.type) {
case ChannelType.GUILD_TEXT:
return new GuildTextChannel(
this.client,
payload as unknown as GuildTextChannelPayload,
);
default:
return new Channel(this.client, payload);
}
}

_get<P extends ChannelPayload>(id: string): P | undefined {
return this.cache.get(id) as P | undefined;
}
Expand All @@ -42,15 +26,15 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
) {
const cached = this._get(id);
if (!cached) return;
return this._createChannel(cached);
return createChannel(this.client, cached);
}
async fetch<P extends ChannelPayload>(
id: string,
) {
try {
const payload = await this._fetch<P>(id);
if (!payload) return;
return this._createChannel(payload);
return createChannel(this.client, payload);
} catch (_err) {
return;
}
Expand Down
80 changes: 80 additions & 0 deletions framework/src/utils/channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ChannelType } from "../../../types/mod.ts";
import type {
ChannelPayload,
DMChannelPayload,
GroupDMChannelPayload,
GuildAnnouncementChannelPayload,
GuildCategoryPayload,
GuildForumChannelPayload,
GuildStageChannelPayload,
GuildTextChannelPayload,
GuildVoiceChannelPayload,
} from "../../../types/mod.ts";
import type { Client } from "../client/mod.ts";
import { EveryChannels } from "../../types/channel.ts";
import {
DMChannel,
GroupDMChannel,
GuildAnnouncementChannel,
GuildCategory,
GuildForumChannel,
GuildStageChannel,
GuildTextChannel,
GuildVoiceChannel,
} from "../structures/channels/mod.ts";

export const createChannel = <P extends ChannelPayload>(
client: Client,
payload: P,
): EveryChannels => {
switch (payload.type) {
case ChannelType.GUILD_TEXT:
return new GuildTextChannel(
client,
payload as unknown as GuildTextChannelPayload,
);
case ChannelType.DM:
return new DMChannel(
client,
payload as unknown as DMChannelPayload,
);
case ChannelType.GUILD_VOICE:
return new GuildVoiceChannel(
client,
payload as unknown as GuildVoiceChannelPayload,
);
case ChannelType.GROUP_DM:
return new GroupDMChannel(
client,
payload as unknown as GroupDMChannelPayload,
);
case ChannelType.GUILD_CATEGORY:
return new GuildCategory(
client,
payload as unknown as GuildCategoryPayload,
);
case ChannelType.GUILD_ANNOUNCEMENT:
return new GuildAnnouncementChannel(
client,
payload as unknown 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,
);
// case ChannelType.GUILD_DIRECTORY:
case ChannelType.GUILD_FORUM:
return new GuildForumChannel(
client,
payload as unknown as GuildForumChannelPayload,
);
// case ChannelType.GUILD_MEDIA:
default:
// TODO: make a proper error type
throw new Error("Unknown channel type");
}
};
100 changes: 100 additions & 0 deletions framework/types/channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { ChannelType } from "../../mod.ts";
import type {
DMChannel,
GroupDMChannel,
GuildAnnouncementChannel,
GuildCategory,
GuildForumChannel,
GuildStageChannel,
GuildTextChannel,
GuildVoiceChannel,
} from "../src/structures/channels/mod.ts";

export type GuildTextBasedChannels =
| GuildAnnouncementChannel
| GuildForumChannel
| GuildTextChannel
| GuildVoiceChannel;

export type GuildThreadAvailableChannels =
| GuildAnnouncementChannel
| GuildForumChannel
| GuildTextChannel;

export type GuildVoiceBasedChannels =
| GuildStageChannel
| GuildVoiceChannel;

export type GuildChannels =
| GuildTextBasedChannels
| GuildVoiceBasedChannels
| GuildThreadAvailableChannels
| GuildCategory;

export type DMChannels = DMChannel | GroupDMChannel;

export type VoiceChannels = GuildVoiceBasedChannels | DMChannels;

export type TextChannels = GuildTextBasedChannels | DMChannels;

export type EveryChannels = GuildChannels | DMChannels;

export const isGuildChannel = (
channel: EveryChannels,
): channel is GuildChannels => {
return [
ChannelType.ANNOUNCEMENT_THREAD,
ChannelType.GUILD_ANNOUNCEMENT,
ChannelType.GUILD_CATEGORY,
ChannelType.GUILD_DIRECTORY,
ChannelType.GUILD_FORUM,
ChannelType.GUILD_PRIVATE_THREAD,
ChannelType.GUILD_PUBLIC_THREAD,
ChannelType.GUILD_STAGE_VOICE,
ChannelType.GUILD_TEXT,
ChannelType.GUILD_VOICE,
].includes(channel.type);
};

export const isDMChannel = (channel: EveryChannels): channel is DMChannels => {
return [ChannelType.DM, ChannelType.GROUP_DM].includes(channel.type);
};

export const isTextChannel = (
channel: EveryChannels,
): channel is TextChannels => {
return [
ChannelType.DM,
ChannelType.GROUP_DM,
ChannelType.ANNOUNCEMENT_THREAD,
ChannelType.GUILD_ANNOUNCEMENT,
ChannelType.GUILD_FORUM,
ChannelType.GUILD_PRIVATE_THREAD,
ChannelType.GUILD_PUBLIC_THREAD,
ChannelType.GUILD_TEXT,
ChannelType.GUILD_VOICE,
].includes(
channel.type,
);
};

export const isThreadAvailableChannel = (
channel: EveryChannels,
): channel is GuildThreadAvailableChannels => {
return [
ChannelType.GUILD_ANNOUNCEMENT,
ChannelType.GUILD_FORUM,
ChannelType.GUILD_TEXT,
].includes(channel.type);
};

export const isVoiceChannel = (
channel: EveryChannels,
): channel is VoiceChannels => {
return [
ChannelType.DM,
ChannelType.GROUP_DM,
ChannelType.GUILD_STAGE_VOICE,
ChannelType.GUILD_VOICE,
].includes(channel.type);
};
1 change: 1 addition & 0 deletions types/src/channels/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export enum ChannelType {
GUILD_STAGE_VOICE = 13,
GUILD_DIRECTORY = 14,
GUILD_FORUM = 15,
GUILD_MEDIA = 16,
}

/** @link https://discord.com/developers/docs/resources/channel#channel-object */
Expand Down

0 comments on commit 343df6d

Please sign in to comment.