cache.threads

This commit is contained in:
Skillz4Killz
2021-06-13 13:34:18 +00:00
committed by GitHub
parent ee79d7da78
commit f2177e8ca6
6 changed files with 70 additions and 25 deletions
+39 -18
View File
@@ -6,6 +6,7 @@ import type { DiscordenoMember } from "./structures/member.ts";
import type { DiscordenoMessage } from "./structures/message.ts";
import type { PresenceUpdate } from "./types/activity/presence_update.ts";
import type { Emoji } from "./types/emojis/emoji.ts";
import { Thread } from "./util/channel_to_thread.ts";
import { Collection } from "./util/collection.ts";
export const cache = {
@@ -35,6 +36,7 @@ export const cache = {
activeGuildIds: new Set<bigint>(),
dispatchedGuildIds: new Set<bigint>(),
dispatchedChannelIds: new Set<bigint>(),
threads: new Collection<bigint, Thread>(),
};
function messageSweeper(message: DiscordenoMessage) {
@@ -107,28 +109,43 @@ export let cacheHandlers = {
filter,
};
export type TableName = "guilds" | "unavailableGuilds" | "channels" | "messages" | "members" | "presences";
export type TableName = "guilds" | "unavailableGuilds" | "channels" | "messages" | "members" | "presences" | "threads";
function set(table: "guilds", key: bigint, value: DiscordenoGuild): Promise<Collection<bigint, DiscordenoGuild>>;
function set(table: "channels", key: bigint, value: DiscordenoChannel): Promise<Collection<bigint, DiscordenoChannel>>;
function set(table: "messages", key: bigint, value: DiscordenoMessage): Promise<Collection<bigint, DiscordenoMessage>>;
function set(table: "members", key: bigint, value: DiscordenoMember): Promise<Collection<bigint, DiscordenoMember>>;
function set(table: "presences", key: bigint, value: PresenceUpdate): Promise<Collection<bigint, PresenceUpdate>>;
function set(table: "unavailableGuilds", key: bigint, value: number): Promise<Collection<bigint, number>>;
async function set(table: "threads", key: bigint, value: Thread): Promise<Collection<bigint, Thread>>;
async function set(table: "guilds", key: bigint, value: DiscordenoGuild): Promise<Collection<bigint, DiscordenoGuild>>;
async function set(
table: "channels",
key: bigint,
value: DiscordenoChannel
): Promise<Collection<bigint, DiscordenoChannel>>;
async function set(
table: "messages",
key: bigint,
value: DiscordenoMessage
): Promise<Collection<bigint, DiscordenoMessage>>;
async function set(
table: "members",
key: bigint,
value: DiscordenoMember
): Promise<Collection<bigint, DiscordenoMember>>;
async function set(table: "presences", key: bigint, value: PresenceUpdate): Promise<Collection<bigint, PresenceUpdate>>;
async function set(table: "unavailableGuilds", key: bigint, value: number): Promise<Collection<bigint, number>>;
async function set(table: TableName, key: bigint, value: any) {
return cache[table].set(key, value);
}
function get(table: "guilds", key: bigint): Promise<DiscordenoGuild | undefined>;
function get(table: "channels", key: bigint): Promise<DiscordenoChannel | undefined>;
function get(table: "messages", key: bigint): Promise<DiscordenoMessage | undefined>;
function get(table: "members", key: bigint): Promise<DiscordenoMember | undefined>;
function get(table: "presences", key: bigint): Promise<PresenceUpdate | undefined>;
function get(table: "unavailableGuilds", key: bigint): Promise<number | undefined>;
async function get(table: "threads", key: bigint): Promise<Thread | undefined>;
async function get(table: "guilds", key: bigint): Promise<DiscordenoGuild | undefined>;
async function get(table: "channels", key: bigint): Promise<DiscordenoChannel | undefined>;
async function get(table: "messages", key: bigint): Promise<DiscordenoMessage | undefined>;
async function get(table: "members", key: bigint): Promise<DiscordenoMember | undefined>;
async function get(table: "presences", key: bigint): Promise<PresenceUpdate | undefined>;
async function get(table: "unavailableGuilds", key: bigint): Promise<number | undefined>;
async function get(table: TableName, key: bigint) {
return cache[table].get(key);
}
function forEach(table: "threads", callback: (value: Thread, key: bigint, map: Map<bigint, Thread>) => unknown): void;
function forEach(
table: "guilds",
callback: (value: DiscordenoGuild, key: bigint, map: Map<bigint, DiscordenoGuild>) => unknown
@@ -153,23 +170,27 @@ function forEach(table: TableName, callback: (value: any, key: bigint, map: Map<
return cache[table].forEach(callback);
}
function filter(
async function filter(
table: "threads",
callback: (value: Thread, key: bigint) => boolean
): Promise<Collection<bigint, Thread>>;
async function filter(
table: "guilds",
callback: (value: DiscordenoGuild, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoGuild>>;
function filter(
async function filter(
table: "unavailableGuilds",
callback: (value: number, key: bigint) => boolean
): Promise<Collection<bigint, number>>;
function filter(
async function filter(
table: "channels",
callback: (value: DiscordenoChannel, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoChannel>>;
function filter(
async function filter(
table: "messages",
callback: (value: DiscordenoMessage, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoMessage>>;
function filter(
async function filter(
table: "members",
callback: (value: DiscordenoMember, key: bigint) => boolean
): Promise<Collection<bigint, DiscordenoMember>>;
+4 -4
View File
@@ -1,14 +1,14 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { channelToThread } from "../../util/channel_to_thread.ts";
export async function handleThreadCreate(data: DiscordGatewayPayload) {
const payload = data.d as Channel;
const discordenoChannel = await structures.createDiscordenoChannel(payload);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
const thread = channelToThread(payload);
await cacheHandlers.set("threads", thread.id, thread);
eventHandlers.threadCreate?.(discordenoChannel);
eventHandlers.threadCreate?.(thread);
}
+3 -3
View File
@@ -7,12 +7,12 @@ import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleThreadDelete(data: DiscordGatewayPayload) {
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", snowflakeToBigint(payload.id));
const cachedChannel = await cacheHandlers.get("threads", snowflakeToBigint(payload.id));
if (!cachedChannel) return;
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
await cacheHandlers.delete("threads", snowflakeToBigint(payload.id));
cacheHandlers.forEach("messages", (message) => {
eventHandlers.debug?.("loop", `Running forEach messages loop in CHANNEL_DELTE file.`);
eventHandlers.debug?.("loop", `Running forEach messages loop in THREAD_DELETE file.`);
if (message.channelId === snowflakeToBigint(payload.id)) {
cacheHandlers.delete("messages", message.id);
}
+2
View File
@@ -110,6 +110,8 @@ export async function createDiscordenoChannel(data: Channel, guildId?: bigint) {
(Object.keys(rest) as (keyof typeof rest)[]).forEach((key) => {
eventHandlers.debug?.("loop", `Running forEach loop in createDiscordenoChannel function.`);
if (key === "threadMetadata") return;
props[key] = createNewProp(
CHANNEL_SNOWFLAKES.includes(key) ? (rest[key] ? snowflakeToBigint(rest[key] as string) : undefined) : rest[key]
);
@@ -6,6 +6,8 @@ export interface ThreadMetadata {
// TODO(threads): channel struct should convert this to a unixx
/** Timestamp when the thread's archive status was last changed, used for calculating recent activity */
archiveTimestamp: string;
/** id of the user that last archived or unarchived the thread */
archiverId?: string;
/** When a thread is locked, only users with `MANAGE_THREADS` can unarchive it */
locked?: boolean;
}
+20
View File
@@ -0,0 +1,20 @@
import { Channel } from "../types/channels/channel.ts";
import { snowflakeToBigint } from "./bigint.ts";
export function channelToThread(channel: Channel) {
return {
id: snowflakeToBigint(channel.id),
channelId: snowflakeToBigint(channel.parentId!),
memberCount: channel.memberCount,
messageCount: channel.messageCount,
archived: channel.threadMetadata?.archived || false,
archiveTimestamp: channel.threadMetadata?.archiveTimestamp
? Date.parse(channel.threadMetadata.archiveTimestamp)
: undefined,
archiverId: channel.threadMetadata?.archiverId ? snowflakeToBigint(channel.threadMetadata.archiverId) : undefined,
autoArchiveDuration: channel.threadMetadata?.autoArchiveDuration || 0,
locked: channel.threadMetadata?.locked || false,
};
}
export type Thread = ReturnType<typeof channelToThread>