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 | ThreadChannel
| StageChannel; | StageChannel;
export type ChannelInGuild =
| GuildTextChannel
| VoiceChannel
| StageChannel
| NewsChannel
| ThreadChannel;
export type ChannelWithMessages = export type ChannelWithMessages =
| GuildTextChannel | GuildTextChannel
| VoiceChannel | VoiceChannel
@ -693,6 +700,24 @@ export type ChannelWithMessages =
export type ChannelWithMessagesInGuild = Exclude<ChannelWithMessages, DMChannel>; export type ChannelWithMessagesInGuild = Exclude<ChannelWithMessages, DMChannel>;
export class ChannelFactory { 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 { static from(session: Session, channel: DiscordChannel): Channel {
switch (channel.type) { switch (channel.type) {
case ChannelTypes.GuildPublicThread: case ChannelTypes.GuildPublicThread:

27
packages/cache/mod.ts vendored
View File

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