Skip to content

Commit

Permalink
⚗️ chore: sorry but seriously i forgor what i was doing
Browse files Browse the repository at this point in the history
  • Loading branch information
Helloyunho committed Aug 27, 2024
1 parent 1b7bde4 commit 93b5b06
Show file tree
Hide file tree
Showing 58 changed files with 1,084 additions and 845 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![banner](https://cdn.discordapp.com/attachments/783319033730564098/783399012547035176/HarmonyBanner.png)
![banner](https://github.com/harmonyland/harmony/blob/main/assets/banner.png)

<p align="center"><i><b>An easy to use Discord API Library for Deno.</b></i></p>
<p align="center">
Expand Down
4 changes: 3 additions & 1 deletion core/src/rest/http_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ export class HTTPClient implements HTTPClientOptions {
}
}

const query = new URLSearchParams(options?.query).toString();

const abortController = new AbortController();
const timeoutID = setTimeout(() => {
abortController.abort();
}, this.timeout);

const res = await fetch(
`${this.baseURL}/v${this.version}${endpoint}`,
`${this.baseURL}/v${this.version}${endpoint}${query}`,
{
method,
headers,
Expand Down
8 changes: 8 additions & 0 deletions framework/src/cache/manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// deno-lint-ignore-file no-explicit-any
import { Collection } from "./collection.ts";

export class CacheManager extends Collection<string, Collection<string, any>> {
constructor() {
super();
}
}
2 changes: 1 addition & 1 deletion framework/src/managers/guildChannels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class GuildChannelsManager
constructor(client: Client, parent: ChannelsManager, guildID: string) {
super(
client,
parent as unknown as BaseManager<GuildChannelPayload, GuildChannel>,
parent as BaseManager<GuildChannelPayload, GuildChannel>,
);
this.guildID = guildID;
}
Expand Down
Empty file.
87 changes: 87 additions & 0 deletions framework/src/managers/messages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type {
GetChannelMessagesParams,
MessagePayload,
} from "../../../types/mod.ts";
import type { Client } from "../client/mod.ts";
import { Message } from "../structures/messages/mod.ts";
import { BaseManager } from "./base.ts";

export class MessagesManager extends BaseManager<MessagePayload, Message> {
constructor(client: Client) {
super(client);
}

_fill(messages: MessagePayload[]) {
for (const message of messages) {
this.set(message.id!, message);
}
}

async _fetch(
channelID: string,
id: string,
): Promise<MessagePayload | undefined> {
try {
const resp: MessagePayload | undefined = await this.client.rest.get(
`/channels/${channelID}/messages/${id}`,
);
if (!resp) return;
this.set(id, resp);
return resp;
} catch (_err) {
return;
}
}
async _fetchBulk(
channelID: string,
options: GetChannelMessagesParams,
): Promise<MessagePayload[] | undefined> {
try {
const query: Record<string, string> = {
...options,
limit: "",
};
if (options.limit) {
if (options.limit > 100 || options.limit < 1) {
throw new Error("Limit must be in range 1-100");
}
query.limit = options.limit.toString();
} else {
delete query.limit;
}
const resp: MessagePayload[] | undefined = await this.client.rest.get(
`/channels/${channelID}/messages`,
{
query,
},
);
if (!resp) return;
return resp.map((message) => {
this.set(message.id!, message);
return message;
});
} catch (_err) {
return;
}
}

get(
id: string,
) {
const cached = this._get(id);
if (!cached) return;
return new Message(this.client, cached);
}
async fetch(
channelID: string,
id: string,
) {
try {
const payload = await this._fetch(channelID, id);
if (!payload) return;
return new Message(this.client, payload);
} catch (_err) {
return;
}
}
}
4 changes: 0 additions & 4 deletions framework/src/structures/channels/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,4 @@ export class Channel {
get type(): ChannelType {
return this.payload.type;
}

get flags(): number {
return this.payload.flags;
}
}
3 changes: 3 additions & 0 deletions framework/src/structures/channels/groupDMChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class GroupDMChannel extends DMBasedChannel {
get icon(): string | null {
return this.payload.icon;
}
get ownerID(): string {
return this.payload.owner_id;
}
get owner(): User | undefined {
return this.client.users.get(this.payload.owner_id);
}
Expand Down
7 changes: 7 additions & 0 deletions framework/src/structures/channels/guildChannel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Channel } from "./channel.ts";
import type { GuildChannelPayload } from "../../../../types/mod.ts";
import type { Client } from "../../client/mod.ts";
import { Guild } from "../guilds/guild.ts";

export class GuildChannel extends Channel {
payload: GuildChannelPayload;
Expand All @@ -12,6 +13,9 @@ export class GuildChannel extends Channel {
get guildID(): string {
return this.payload.guild_id;
}
get guild(): Guild | undefined {
return this.client.guilds.get(this.payload.guild_id);
}
get name(): string {
return this.payload.name;
}
Expand All @@ -27,6 +31,9 @@ export class GuildChannel extends Channel {
get parentID(): string | null {
return this.payload.parent_id;
}
get parent(): GuildChannel | undefined {
return this.client.channels.get(this.payload.parent_id!) as GuildChannel;
}
get topic(): string | null {
return this.payload.topic;
}
Expand Down
3 changes: 3 additions & 0 deletions framework/src/structures/channels/guildForumChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ export class GuildForumChannel extends GuildForumChannelSuper {
};
});
}
get flags(): number {
return this.payload.flags!;
}
}
Empty file.
62 changes: 62 additions & 0 deletions framework/src/structures/guilds/member.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { GuildMemberPayload } from "../../../../types/mod.ts";
import type { Client } from "../../client/mod.ts";

export class Member {
client: Client;
payload: GuildMemberPayload;

constructor(client: Client, payload: GuildMemberPayload) {
this.client = client;
this.payload = payload;
if (payload.user) {
this.client.users.set(payload.user.id, payload.user);
}
}

get id() {
return this.payload.user?.id;
}
get user() {
return this.id ? this.client.users.get(this.id) : undefined;
}
get nick() {
return this.payload.nick ?? this.user?.name;
}
get avatar() {
// TODO: give default avatar
// (also) TODO: give the url instead of hash
return this.payload.avatar ?? this.user?.avatar;
}
get roles() {
return this.payload.roles.map((id) => this.client.roles.get(id));
}
get joinedAt() {
return Date.parse(this.payload.joined_at);
}
get premiumSince() {
return this.payload.premium_since ?? null;
}
get deaf() {
return this.payload.deaf;
}
get mute() {
return this.payload.mute;
}
get flags() {
// TODO: make flags class
return this.payload.flags;
}
get pending() {
return this.payload.pending ?? false;
}
get permissions() {
// TODO: make permissions class
// also TODO: use roles to calculate permissions - permissions in payload is not available in all cases
return this.payload.permissions;
}
get timeOutEnd() {
return this.payload.communication_disabled_until
? Date.parse(this.payload.communication_disabled_until)
: null;
}
}
33 changes: 33 additions & 0 deletions framework/test/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { GatewayIntent } from "../../mod.ts";
import { Client } from "../mod.ts";

const token = Deno.env.get("BOT_TOKEN");
if (!token) {
throw new Error("No token provided");
}

Deno.test("client", {
sanitizeOps: false,
}, async (t) => {
await t.step("ready event", async () => {
const client = new Client(token);
await client.connect();
for await (const _ of client.on("ready")) {
await client.gateway.destroyAll();
break;
}
});

await t.step("guild create event", async () => {
const client = new Client(token, {
intents: GatewayIntent.GUILDS,
});
await client.connect();
for await (const [guild] of client.on("guildCreate")) {
if (guild.id === "783319033205751809") {
await client.gateway.destroyAll();
break;
}
}
});
});
37 changes: 37 additions & 0 deletions framework/test/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { GatewayIntent } from "../../mod.ts";
import { Client, GuildChannel } from "../mod.ts";

const TOKEN = Deno.env.get("BOT_TOKEN");
if (!TOKEN) {
throw new Error("No token provided");
}

const client = new Client(TOKEN, {
intents: GatewayIntent.GUILDS | GatewayIntent.GUILD_MESSAGES,
});

client.on("guildCreate", (guild) => {
console.log(guild.name);
});

client.on("messageCreate", (msg) => {
if (msg.author.bot) return;
console.log(msg.guild!.id);
msg.channel?.send(`${msg.guild!.id}: ${msg.channel.id}`);
});

client.on("channelCreate", (channel) => {
if (channel instanceof GuildChannel) {
console.log(channel.name);
console.log(client.channels.cache.size);
}
});

client.on("channelDelete", (channel) => {
if (channel instanceof GuildChannel) {
console.log(channel.name);
console.log(client.channels.cache.size);
}
});

client.connect();
1 change: 1 addition & 0 deletions framework/test/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/assert/mod.ts";
Loading

0 comments on commit 93b5b06

Please sign in to comment.