diff --git a/.gitignore b/.gitignore index 58b63b6e..62ab7337 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ scripts/api_docs .DS_Store .idea/ +**/dummy.ts \ No newline at end of file diff --git a/framework/src/client/events.ts b/framework/src/client/events.ts index 164b2899..6da05567 100644 --- a/framework/src/client/events.ts +++ b/framework/src/client/events.ts @@ -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]; }; diff --git a/framework/src/gateway/handlers/channelCreate.ts b/framework/src/gateway/handlers/channelCreate.ts new file mode 100644 index 00000000..f3ac9a97 --- /dev/null +++ b/framework/src/gateway/handlers/channelCreate.ts @@ -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; diff --git a/framework/src/gateway/handlers/channelDelete.ts b/framework/src/gateway/handlers/channelDelete.ts new file mode 100644 index 00000000..42f1390e --- /dev/null +++ b/framework/src/gateway/handlers/channelDelete.ts @@ -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; diff --git a/framework/src/gateway/guildCreate.ts b/framework/src/gateway/handlers/guildCreate.ts similarity index 60% rename from framework/src/gateway/guildCreate.ts rename to framework/src/gateway/handlers/guildCreate.ts index 6aa2e784..0257e241 100644 --- a/framework/src/gateway/guildCreate.ts +++ b/framework/src/gateway/handlers/guildCreate.ts @@ -1,4 +1,5 @@ -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, @@ -6,7 +7,7 @@ const guildCreate: GatewayHandler<"GUILD_CREATE"> = async ( ) => { 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); diff --git a/framework/src/gateway/messageCreate.ts b/framework/src/gateway/handlers/messageCreate.ts similarity index 50% rename from framework/src/gateway/messageCreate.ts rename to framework/src/gateway/handlers/messageCreate.ts index 9d9f1858..6f0be494 100644 --- a/framework/src/gateway/messageCreate.ts +++ b/framework/src/gateway/handlers/messageCreate.ts @@ -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, diff --git a/framework/src/gateway/ready.ts b/framework/src/gateway/handlers/ready.ts similarity index 62% rename from framework/src/gateway/ready.ts rename to framework/src/gateway/handlers/ready.ts index 3a5defa9..cae5bad1 100644 --- a/framework/src/gateway/ready.ts +++ b/framework/src/gateway/handlers/ready.ts @@ -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, diff --git a/framework/src/gateway/mod.ts b/framework/src/gateway/mod.ts index d0690c86..89b84439 100644 --- a/framework/src/gateway/mod.ts +++ b/framework/src/gateway/mod.ts @@ -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 } = { MESSAGE_CREATE: messageCreate, READY: ready, GUILD_CREATE: guildCreate, + CHANNEL_CREATE: channelCreate, + CHANNEL_DELETE: channelDelete, }; diff --git a/framework/src/managers/channels.ts b/framework/src/managers/channels.ts index b40b8467..02292b04 100644 --- a/framework/src/managers/channels.ts +++ b/framework/src/managers/channels.ts @@ -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 { - private _createChannel

(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

(id: string): P | undefined { return this.cache.get(id) as P | undefined; } @@ -42,7 +26,7 @@ export class ChannelsManager extends BaseManager { ) { const cached = this._get(id); if (!cached) return; - return this._createChannel(cached); + return createChannel(this.client, cached); } async fetch

( id: string, @@ -50,7 +34,7 @@ export class ChannelsManager extends BaseManager { try { const payload = await this._fetch

(id); if (!payload) return; - return this._createChannel(payload); + return createChannel(this.client, payload); } catch (_err) { return; } diff --git a/framework/src/utils/channel.ts b/framework/src/utils/channel.ts new file mode 100644 index 00000000..a7402ea1 --- /dev/null +++ b/framework/src/utils/channel.ts @@ -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 =

( + 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"); + } +}; diff --git a/framework/types/channel.ts b/framework/types/channel.ts index e69de29b..a5b1d130 100644 --- a/framework/types/channel.ts +++ b/framework/types/channel.ts @@ -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); +}; diff --git a/types/src/channels/base.ts b/types/src/channels/base.ts index 0a5124da..05a9d31e 100644 --- a/types/src/channels/base.ts +++ b/types/src/channels/base.ts @@ -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 */