Skip to content

Commit

Permalink
✨ feat: uhhhhhh it works
Browse files Browse the repository at this point in the history
  • Loading branch information
Helloyunho committed Sep 11, 2023
1 parent aa1de8d commit b09e6c5
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 12 deletions.
2 changes: 2 additions & 0 deletions framework/src/client/events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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];
ready: [];
guildCreate: [Guild];
};
4 changes: 2 additions & 2 deletions framework/src/client/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class Client extends EventEmitter<ClientEvents> {

waitFor<K extends keyof ClientEvents>(
event: K,
check: (...args: ClientEvents[K]) => boolean,
check?: (...args: ClientEvents[K]) => boolean,
timeout?: number,
): Promise<ClientEvents[K] | []> {
return new Promise((resolve) => {
Expand All @@ -60,7 +60,7 @@ export class Client extends EventEmitter<ClientEvents> {
}, timeout);
}
const eventFunc = (...args: ClientEvents[K]): void => {
if (check(...args)) {
if (check && check(...args) || !check) {
resolve(args);
this.off(event, eventFunc);
if (timeoutID !== undefined) clearTimeout(timeoutID);
Expand Down
15 changes: 15 additions & 0 deletions framework/src/gateway/guildCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { GatewayHandler } from "../../types/mod.ts";

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

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

client.emit("guildCreate", guildObj);
};

export default guildCreate;
2 changes: 2 additions & 0 deletions framework/src/gateway/mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { GatewayHandler } from "../../types/gateway.ts";
import guildCreate from "./guildCreate.ts";
import messageCreate from "./messageCreate.ts";
import ready from "./ready.ts";

// deno-lint-ignore no-explicit-any
export const GatewayHandlers: { [K: string]: GatewayHandler<any> } = {
MESSAGE_CREATE: messageCreate,
READY: ready,
GUILD_CREATE: guildCreate,
};
2 changes: 2 additions & 0 deletions framework/src/gateway/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const ready: GatewayHandler<"READY"> = (
client.guilds.set(g.id, g);
});

// TODO: implement application loading

client.emit("ready");
};

Expand Down
1 change: 0 additions & 1 deletion framework/src/managers/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class BaseManager<P, T> {
throw new Error("Not implemented");
}

// try _get first, if not found, fetch
get(_key: string): T | undefined {
throw new Error("Not implemented");
}
Expand Down
9 changes: 4 additions & 5 deletions framework/src/managers/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import { Channel } from "../structures/channels/channel.ts";
import { GuildTextChannel } from "../structures/channels/guildTextChannel.ts";
import { BaseManager } from "./base.ts";

export class ChannelsManager
extends BaseManager<ChannelPayload, Channel<ChannelPayload>> {
private createChannel<P extends ChannelPayload>(payload: P) {
export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
private _createChannel<P extends ChannelPayload>(payload: P) {
switch (payload.type) {
case ChannelType.GUILD_TEXT:
return new GuildTextChannel(
Expand Down Expand Up @@ -43,15 +42,15 @@ export class ChannelsManager
) {
const cached = this._get(id);
if (!cached) return;
return this.createChannel(cached);
return this._createChannel(cached);
}
async fetch<P extends ChannelPayload>(
id: string,
) {
try {
const payload = await this._fetch<P>(id);
if (!payload) return;
return this.createChannel(payload);
return this._createChannel(payload);
} catch (_err) {
return;
}
Expand Down
108 changes: 108 additions & 0 deletions framework/src/managers/guildChannels.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
ChannelType,
type GuildChannelPayload,
type GuildTextChannelPayload,
} from "../../../types/mod.ts";
import type { Client } from "../client/mod.ts";
import { GuildChannel } from "../structures/channels/guildChannel.ts";
import { GuildTextChannel } from "../structures/channels/guildTextChannel.ts";
import { BaseManager } from "./base.ts";
import { BaseChildManager } from "./baseChild.ts";
import type { ChannelsManager } from "./channels.ts";

export class GuildChannelsManager
extends BaseChildManager<GuildChannelPayload, GuildChannel> {
guildID: string;

constructor(client: Client, parent: ChannelsManager, guildID: string) {
super(
client,
parent as unknown as BaseManager<GuildChannelPayload, GuildChannel>,
);
this.guildID = guildID;
}

private _createChannel<P extends GuildChannelPayload>(payload: P) {
switch (payload.type) {
case ChannelType.GUILD_TEXT:
return new GuildTextChannel(
this.client,
payload as unknown as GuildTextChannelPayload,
);
default:
return new GuildChannel(this.client, payload);
}
}

_fill(payloads: GuildChannelPayload[]) {
for (const payload of payloads) {
this.set(payload.id!, payload);
}
}

_get(key: string): GuildChannelPayload | undefined {
const channel = this.parent._get(key);
if (!channel) return;
if (channel.guild_id !== this.guildID) return;
return channel;
}

async _fetchAll(): Promise<GuildChannelPayload[] | undefined> {
try {
const resp: GuildChannelPayload[] | undefined = await this.client.rest
.get(
`/guilds/${this.guildID}/channels`,
);
if (!resp) return;
const channels = resp;
this._fill(channels);
return channels;
} catch (_err) {
return;
}
}

async _fetch(id: string): Promise<GuildChannelPayload | undefined> {
try {
const resp: GuildChannelPayload | undefined = await this.client.rest
.get(
`/channels/${id}`,
);
if (!resp) return;
this.set(id, resp);
return resp;
} catch (_err) {
return;
}
}

get(
id: string,
) {
const cached = this._get(id);
if (!cached) return;
return this._createChannel(cached);
}

async fetchAll() {
try {
const channels = await this._fetchAll();
if (!channels) return;
return channels.map((r) => this._createChannel(r));
} catch (_err) {
return;
}
}

async fetch(
id: string,
) {
try {
const payload = await this._fetch(id);
if (!payload) return;
return this._createChannel(payload);
} catch (_err) {
return;
}
}
}
1 change: 1 addition & 0 deletions framework/src/managers/guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class GuildsManager extends BaseManager<LightGuildPayload, Guild> {
// TODO: remove more duplication
const lightGuild: LightGuildPayload = {
...value,
channels: undefined,
roles: undefined,
emojis: undefined,
};
Expand Down
13 changes: 10 additions & 3 deletions framework/src/structures/guilds/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import type {
} from "../../../../types/mod.ts";
import { LightGuildPayload } from "../../../types/guild.ts";
import { Client } from "../../client/mod.ts";
import { GuildChannelsManager } from "../../managers/guildChannels.ts";
import { GuildEmojisManager } from "../../managers/guildEmojis.ts";
import { GuildRolesManager } from "../../managers/guildRoles.ts";
import { UnavailableGuild } from "./unavaliable.ts";

export class Guild extends UnavailableGuild {
roles: GuildRolesManager;
emojis: GuildEmojisManager;
channels: GuildChannelsManager;
constructor(client: Client, payload: LightGuildPayload) {
// TODO: think about fill methods
super(client, payload);
Expand All @@ -29,6 +31,14 @@ export class Guild extends UnavailableGuild {
if (payload.emojis) {
this.emojis._fill(payload.emojis);
}
this.channels = new GuildChannelsManager(
client,
client.channels,
payload.id,
);
if (payload.channels) {
this.channels._fill(payload.channels);
}
}

get name(): string {
Expand Down Expand Up @@ -112,9 +122,6 @@ export class Guild extends UnavailableGuild {
// get members(): string[] {
// return this.payload.members;
// }
// get channels(): string[] {
// return this.payload.channels;
// }
// get threads(): string[] {
// return this.payload.threads;
// }
Expand Down
6 changes: 6 additions & 0 deletions framework/src/structures/messages/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@ export class Message {
// return this.client.channels.get(channel.id);
// })
// }

get guild() {
return this.payload.guild_id
? this.client.guilds.get(this.payload.guild_id)
: undefined;
}
}
2 changes: 1 addition & 1 deletion framework/types/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { SelectivePartial } from "./utils.ts";

export type LightGuildPayload = SelectivePartial<
GuildPayload,
"roles" | "emojis"
"roles" | "emojis" | "channels"
>;

0 comments on commit b09e6c5

Please sign in to comment.