Merge branch 'cache'

This commit is contained in:
Yuzu 2022-07-10 19:23:19 -05:00
commit e211ec9b8c
7 changed files with 117 additions and 8 deletions

View File

@ -2,6 +2,13 @@ import type { ButtonBuilder, InputTextBuilder, SelectMenuBuilder } from "./mod.t
import type { Permissions } from "./structures/Permissions.ts"; import type { Permissions } from "./structures/Permissions.ts";
import type { Snowflake } from "./Snowflake.ts"; import type { Snowflake } from "./Snowflake.ts";
/*
* Represents a session's cache
* */
export interface SymCache {
readonly cache: symbol;
}
/* /*
* @link https://discord.com/developers/docs/resources/channel#message-object-message-flags * @link https://discord.com/developers/docs/resources/channel#message-object-message-flags
*/ */

View File

@ -1,4 +1,7 @@
// structures // structures
import { Session } from "./Session.ts";
export default Session;
export * from "./structures/Attachment.ts"; export * from "./structures/Attachment.ts";
export * from "./structures/Base.ts"; export * from "./structures/Base.ts";
export * from "./structures/Embed.ts"; export * from "./structures/Embed.ts";
@ -48,6 +51,9 @@ export * from "./structures/interactions/InteractionFactory.ts";
export * from "./structures/interactions/ModalSubmitInteraction.ts"; export * from "./structures/interactions/ModalSubmitInteraction.ts";
export * from "./structures/interactions/PingInteraction.ts"; export * from "./structures/interactions/PingInteraction.ts";
// etc
export * from "./Snowflake.ts";
// session // session
export * from "./Session.ts"; export * from "./Session.ts";

View File

@ -361,6 +361,18 @@ export class Message implements Model {
return new Message(this.session, message); return new Message(this.session, message);
} }
async fetch() {
const message = await this.session.rest.runMethod<DiscordMessage>(
this.session.rest,
"GET",
Routes.CHANNEL_MESSAGE(this.channelId, this.message),
);
if (!message?.id) return;
return new Message(this.session, message);
}
/* /*
* alias of Message.crosspost * alias of Message.crosspost
* */ * */

View File

@ -130,6 +130,8 @@ export class TextChannel {
if (data.last_pin_timestamp) { if (data.last_pin_timestamp) {
this.lastPinTimestamp = data.last_pin_timestamp; this.lastPinTimestamp = data.last_pin_timestamp;
} }
this.messages = new Map();
} }
readonly session: Session; readonly session: Session;
@ -141,6 +143,9 @@ export class TextChannel {
rateLimitPerUser: number; rateLimitPerUser: number;
nsfw: boolean; nsfw: boolean;
/** Meant to be used with cache */
messages: Map<Snowflake, Message>;
/** /**
* Mixin * Mixin
*/ */

View File

@ -22,7 +22,7 @@ import {
VerificationLevels, VerificationLevels,
} from "../../../discordeno/mod.ts"; } from "../../../discordeno/mod.ts";
import { encode as _encode, urlToBase64 } from "../../util/urlToBase64.ts"; import { encode as _encode, urlToBase64 } from "../../util/urlToBase64.ts";
import { ThreadChannel } from "../channels.ts"; import { GuildChannel, ThreadChannel } from "../channels.ts";
import Util from "../../Util.ts"; import Util from "../../Util.ts";
import Member from "../Member.ts"; import Member from "../Member.ts";
import BaseGuild from "./BaseGuild.ts"; import BaseGuild from "./BaseGuild.ts";
@ -208,10 +208,22 @@ export class Guild extends BaseGuild implements Model {
this.vefificationLevel = data.verification_level; this.vefificationLevel = data.verification_level;
this.defaultMessageNotificationLevel = data.default_message_notifications; this.defaultMessageNotificationLevel = data.default_message_notifications;
this.explicitContentFilterLevel = data.explicit_content_filter; this.explicitContentFilterLevel = data.explicit_content_filter;
this.members = data.members?.map((member) => new Member(session, { ...member, user: member.user! }, data.id)) ??
[]; this.members = new Map(
this.roles = data.roles.map((role) => new Role(session, role, data.id)); data.members?.map((member) => [data.id, new Member(session, { ...member, user: member.user! }, data.id)])
this.emojis = data.emojis.map((guildEmoji) => new GuildEmoji(session, guildEmoji, data.id)); );
this.roles = new Map(
data.roles.map((role) => [data.id, new Role(session, role, data.id)])
);
this.emojis = new Map(
data.emojis.map((guildEmoji) => [guildEmoji.id!, new GuildEmoji(session, guildEmoji, data.id)])
);
this.channels = new Map(
data.channels?.map((guildChannel) => [guildChannel.id, new GuildChannel(session, guildChannel, data.id)])
);
} }
splashHash?: bigint; splashHash?: bigint;
@ -222,9 +234,10 @@ export class Guild extends BaseGuild implements Model {
vefificationLevel: VerificationLevels; vefificationLevel: VerificationLevels;
defaultMessageNotificationLevel: DefaultMessageNotificationLevels; defaultMessageNotificationLevel: DefaultMessageNotificationLevels;
explicitContentFilterLevel: ExplicitContentFilterLevels; explicitContentFilterLevel: ExplicitContentFilterLevels;
members: Member[]; members: Map<Snowflake, Member>;
roles: Role[]; roles: Map<Snowflake, Role>;
emojis: GuildEmoji[]; emojis: Map<Snowflake, GuildEmoji>;
channels: Map<Snowflake, GuildChannel>;
/** /**
* 'null' would reset the nickname * 'null' would reset the nickname

2
packages/cache/deps.ts vendored Normal file
View File

@ -0,0 +1,2 @@
export * from "../biscuit/mod.ts";
export * from "../discordeno/mod.ts";

64
packages/cache/mod.ts vendored Normal file
View File

@ -0,0 +1,64 @@
import type {
SymCache,
Session,
Snowflake,
DiscordGuild,
DiscordEmoji,
DiscordUser,
DiscordChannel,
} from "./deps.ts";
import {
Guild,
User,
DMChannel,
GuildEmoji,
} from "./deps.ts";
export const cache_sym = Symbol("@cache");
export interface SessionCache extends SymCache {
guilds: StructCache<Guild>;
users: StructCache<User>;
dms: StructCache<DMChannel>;
emojis: StructCache<GuildEmoji>;
session: Session,
}
export default function (session: Session): SessionCache {
return {
guilds: new StructCache<Guild>(session),
users: new StructCache<User>(session),
dms: new StructCache<DMChannel>(session),
emojis: new StructCache<GuildEmoji>(session),
cache: cache_sym,
session,
};
}
export class StructCache<T> extends Map<Snowflake, T> {
constructor(session: Session, entries?: Iterable<readonly [Snowflake, T]>) {
super(entries);
this.session = session;
}
readonly session: Session;
}
export function userBootstrapper(cache: SessionCache, user: DiscordUser) {
cache.users.set(user.id, new User(cache.session, user));
}
export function emojiBootstrapper(cache: SessionCache, emoji: DiscordEmoji, guildId: Snowflake) {
if (!emoji.id) return;
cache.emojis.set(emoji.id, new GuildEmoji(cache.session, emoji, guildId));
}
export function DMChannelBootstrapper(cache: SessionCache, channel: DiscordChannel) {
cache.dms.set(channel.id, new DMChannel(cache.session, channel));
}
export function guildBootstrapper(guild: DiscordGuild, cache: SessionCache) {
// TODO: optimizee this garbage
cache.guilds.set(guild.id, new Guild(cache.session, guild));
}