feat(cache): improvements on channels

This commit is contained in:
Yuzu 2022-07-12 14:50:17 -05:00
parent a89f22c189
commit f5e3eaaafc
3 changed files with 46 additions and 3104 deletions

3094
biscuit.js

File diff suppressed because it is too large Load Diff

View File

@ -683,6 +683,13 @@ export type Channel =
| ThreadChannel
| StageChannel;
export type ChannelInGuild =
| GuildTextChannel
| VoiceChannel
| StageChannel
| NewsChannel
| ThreadChannel;
export type ChannelWithMessages =
| GuildTextChannel
| VoiceChannel
@ -693,6 +700,24 @@ export type ChannelWithMessages =
export type ChannelWithMessagesInGuild = Exclude<ChannelWithMessages, DMChannel>;
export class ChannelFactory {
static fromGuildChannel(session: Session, channel: DiscordChannel): ChannelInGuild {
switch (channel.type) {
case ChannelTypes.GuildPublicThread:
case ChannelTypes.GuildPrivateThread:
return new ThreadChannel(session, channel, channel.guild_id!);
case ChannelTypes.GuildText:
return new GuildTextChannel(session, channel, channel.guild_id!);
case ChannelTypes.GuildNews:
return new NewsChannel(session, channel, channel.guild_id!);
case ChannelTypes.GuildVoice:
return new VoiceChannel(session, channel, channel.guild_id!);
case ChannelTypes.GuildStageVoice:
return new StageChannel(session, channel, channel.guild_id!);
default:
throw new Error("Channel was not implemented");
}
}
static from(session: Session, channel: DiscordChannel): Channel {
switch (channel.type) {
case ChannelTypes.GuildPublicThread:

31
packages/cache/mod.ts vendored
View File

@ -1,5 +1,4 @@
import type {
ChannelTypes,
DiscordMessage,
DiscordChannel,
DiscordEmoji,
@ -16,12 +15,14 @@ import type {
} from "./deps.ts";
import {
ChannelTypes,
ChannelFactory,
DMChannel,
Emoji,
Guild,
GuildEmoji,
GuildTextChannel,
GuildChannel,
Member,
Message,
MessageReaction,
@ -29,6 +30,7 @@ import {
ThreadChannel,
User,
VoiceChannel,
textBasedChannels,
} from "./deps.ts";
export const cache_sym = Symbol("@cache");
@ -101,8 +103,9 @@ export default function (session: Session): SessionCache {
case "MESSAGE_UPDATE":
messageBootstrapper(cache, raw, !raw.edited_timestamp);
break;
case "CHANNEL_UPDATE":
case "CHANNEL_CREATE":
// DMChannelBootstrapper(cache, raw);
channelBootstrapper(cache, raw);
break;
case "GUILD_MEMBER_ADD":
memberBootstrapper(cache, raw, raw.guild_id);
@ -113,9 +116,6 @@ export default function (session: Session): SessionCache {
case "GUILD_DELETE":
cache.guilds.delete(raw.id);
break;
case "MESSAGE_DELETE":
// pass
break;
case "MESSAGE_REACTION_ADD":
reactionBootstrapper(cache, raw, false);
break;
@ -408,11 +408,22 @@ export function emojiBootstrapper(cache: SessionCache, emoji: DiscordEmoji, guil
cache.emojis.set(emoji.id, new GuildEmoji(cache.session, emoji, guildId));
}
export function DMChannelBootstrapper(cache: SessionCache, channel: DiscordChannel) {
cache.dms.set(channel.id, Object.assign(
new DMChannel(cache.session, channel),
{ messages: new StructCache<CachedMessage>(cache.session) }
));
export function channelBootstrapper(cache: SessionCache, channel: DiscordChannel) {
if (!channel.guild_id) return;
cache.guilds.retrieve(channel.guild_id, (guild) => {
if (textBasedChannels.includes(channel.type)) {
// deno-lint-ignore no-explicit-any
guild.channels.set(channel.id, <any>Object.assign(
ChannelFactory.fromGuildChannel(cache.session, channel),
{ messages: new StructCache<CachedMessage>(cache.session) }
));
}
else {
// deno-lint-ignore no-explicit-any
guild.channels.set(channel.id, <any>ChannelFactory.fromGuildChannel(cache.session, channel));
}
});
}
export function memberBootstrapper(cache: SessionCache, member: DiscordMemberWithUser, guildId: Snowflake) {