mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-04 09:50:07 +00:00
custom caching complete
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
import { eventHandlers } from "../module/client.ts";
|
||||
import { DiscordPayload } from "../types/discord.ts";
|
||||
import { GuildBanPayload } from "../types/guild.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { cacheHandlers } from "./cache.ts";
|
||||
|
||||
export function handleInternalGuildBanAdd(data: DiscordPayload) {
|
||||
export async function handleInternalGuildBanAdd(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_BAN_ADD") return;
|
||||
|
||||
const payload = data.d as GuildBanPayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
const member = guild.members.get(payload.user.id);
|
||||
eventHandlers.guildBanAdd?.(guild, member || payload.user);
|
||||
}
|
||||
|
||||
export function handleInternalGuildBanRemove(data: DiscordPayload) {
|
||||
export async function handleInternalGuildBanRemove(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_BAN_ADD") return;
|
||||
|
||||
const payload = data.d as GuildBanPayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
const member = guild.members.get(payload.user.id);
|
||||
|
||||
@@ -1,44 +1,95 @@
|
||||
import { Channel } from "../structures/channel.ts";
|
||||
import { Guild } from "../structures/guild.ts";
|
||||
import { Message } from "../structures/message.ts";
|
||||
import { PresenceUpdatePayload } from "../types/discord.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { Collection } from "../utils/collection.ts";
|
||||
|
||||
export type TableName = "guilds" | "channels" | "messages";
|
||||
export type TableName =
|
||||
| "guilds"
|
||||
| "channels"
|
||||
| "messages"
|
||||
| "presences"
|
||||
| "unavailableGuilds"
|
||||
| "fetchAllMembersProcessingRequests";
|
||||
|
||||
export function test(
|
||||
test: "guilds",
|
||||
function set(
|
||||
table: "guilds",
|
||||
key: string,
|
||||
value: Guild,
|
||||
): Promise<Collection<string, Guild>>;
|
||||
export function test(
|
||||
test: "channels",
|
||||
function set(
|
||||
table: "channels",
|
||||
key: string,
|
||||
value: Channel,
|
||||
): Promise<Collection<string, Channel>>;
|
||||
export function test(
|
||||
test: "messages",
|
||||
function set(
|
||||
table: "messages",
|
||||
key: string,
|
||||
value: Message,
|
||||
): Promise<Collection<string, Message>>;
|
||||
export async function test(table: TableName, key: string, value: unknown) {
|
||||
function set(
|
||||
table: "presences",
|
||||
key: string,
|
||||
value: PresenceUpdatePayload,
|
||||
): Promise<Collection<string, PresenceUpdatePayload>>;
|
||||
function set(
|
||||
table: "unavailableGuilds",
|
||||
key: string,
|
||||
value: number,
|
||||
): Promise<Collection<string, number>>;
|
||||
function set(
|
||||
table: "fetchAllMembersProcessingRequests",
|
||||
key: string,
|
||||
value: number,
|
||||
): Promise<Collection<string, number>>;
|
||||
async function set(table: TableName, key: string, value: any) {
|
||||
return cache[table].set(key, value);
|
||||
}
|
||||
|
||||
function get(table: "guilds", key: string): Promise<Guild>;
|
||||
function get(table: "channels", key: string): Promise<Channel>;
|
||||
function get(table: "messages", key: string): Promise<Message>;
|
||||
function get(table: "presences", key: string): Promise<PresenceUpdatePayload>;
|
||||
function get(table: "unavailableGuilds", key: string): Promise<Guild>;
|
||||
async function get(table: TableName, key: string) {
|
||||
return cache[table].get(key);
|
||||
}
|
||||
|
||||
function forEach(
|
||||
table: "guilds",
|
||||
callback: (value: Guild, key: string, map: Map<string, Guild>) => unknown,
|
||||
): void;
|
||||
function forEach(
|
||||
table: "unavailableGuilds",
|
||||
callback: (value: Guild, key: string, map: Map<string, Guild>) => unknown,
|
||||
): void;
|
||||
function forEach(
|
||||
table: "channels",
|
||||
callback: (value: Channel, key: string, map: Map<string, Channel>) => unknown,
|
||||
): void;
|
||||
function forEach(
|
||||
table: "messages",
|
||||
callback: (value: Message, key: string, map: Map<string, Message>) => unknown,
|
||||
): void;
|
||||
function forEach(
|
||||
table: TableName,
|
||||
callback: (value: any, key: string, map: Map<string, any>) => unknown,
|
||||
) {
|
||||
return cache[table].forEach(callback);
|
||||
}
|
||||
|
||||
export let cacheHandlers = {
|
||||
get: async function (table: TableName, key: string) {
|
||||
return cache[table].get(key);
|
||||
},
|
||||
delete: async function (table: TableName, key: string) {
|
||||
return cache[table].delete(key);
|
||||
},
|
||||
set: async function (
|
||||
table: TableName,
|
||||
key: string,
|
||||
value: Channel | Guild | Message,
|
||||
) {
|
||||
return cache[table].set(key, value);
|
||||
has: async function (table: TableName, key: string) {
|
||||
return cache[table].has(key);
|
||||
},
|
||||
// Done differently to have overloads
|
||||
set,
|
||||
get,
|
||||
forEach,
|
||||
};
|
||||
|
||||
export type CacheHandlers = typeof cacheHandlers;
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { ChannelCreatePayload, ChannelTypes } from "../types/channel.ts";
|
||||
import { eventHandlers } from "../module/client.ts";
|
||||
import { structures } from "../structures/mod.ts";
|
||||
import { DiscordPayload } from "../types/discord.ts";
|
||||
import { cacheHandlers } from "./cache.ts";
|
||||
|
||||
export function handleInternalChannelCreate(data: DiscordPayload) {
|
||||
export async function handleInternalChannelCreate(data: DiscordPayload) {
|
||||
if (data.t !== "CHANNEL_CREATE") return;
|
||||
|
||||
const payload = data.d as ChannelCreatePayload;
|
||||
const channel = structures.createChannel(payload);
|
||||
cache.channels.set(channel.id, channel);
|
||||
await cacheHandlers.set("channels", channel.id, channel);
|
||||
|
||||
if (channel.guildID) {
|
||||
const guild = cache.guilds.get(channel.guildID);
|
||||
const guild = await cacheHandlers.get("guilds", channel.guildID);
|
||||
guild?.channels.set(channel.id, channel);
|
||||
}
|
||||
|
||||
eventHandlers.channelCreate?.(channel);
|
||||
}
|
||||
|
||||
export function handleInternalChannelDelete(data: DiscordPayload) {
|
||||
export async function handleInternalChannelDelete(data: DiscordPayload) {
|
||||
if (data.t !== "CHANNEL_DELETE") return;
|
||||
|
||||
const payload = data.d as ChannelCreatePayload;
|
||||
|
||||
const cachedChannel = cache.channels.get(payload.id);
|
||||
const cachedChannel = await cacheHandlers.get("channels", payload.id);
|
||||
if (!cachedChannel) return;
|
||||
|
||||
if (cachedChannel.type === ChannelTypes.GUILD_VOICE && payload.guild_id) {
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
|
||||
if (guild) {
|
||||
guild.voiceStates.forEach((vs, key) => {
|
||||
@@ -47,24 +47,27 @@ export function handleInternalChannelDelete(data: DiscordPayload) {
|
||||
guild?.channels.delete(payload.id);
|
||||
}
|
||||
|
||||
cache.channels.delete(payload.id);
|
||||
cache.messages.forEach((message) => {
|
||||
if (message.channelID === payload.id) cache.messages.delete(message.id);
|
||||
cacheHandlers.delete("channels", payload.id);
|
||||
cacheHandlers.forEach("messages", (message) => {
|
||||
if (message.channelID === payload.id) {
|
||||
cacheHandlers.delete("messages", message.id);
|
||||
}
|
||||
});
|
||||
eventHandlers.channelDelete?.(cachedChannel);
|
||||
}
|
||||
|
||||
export function handleInternalChannelUpdate(data: DiscordPayload) {
|
||||
export async function handleInternalChannelUpdate(data: DiscordPayload) {
|
||||
if (data.t !== "CHANNEL_UPDATE") return;
|
||||
|
||||
const payload = data.d as ChannelCreatePayload;
|
||||
const cachedChannel = cache.channels.get(payload.id);
|
||||
const cachedChannel = await cacheHandlers.get("channels", payload.id);
|
||||
const channel = structures.createChannel(payload);
|
||||
cache.channels.set(channel.id, channel);
|
||||
cacheHandlers.set("channels", channel.id, channel);
|
||||
|
||||
if (!cachedChannel) return;
|
||||
|
||||
if (channel.guildID) {
|
||||
const guild = cache.guilds.get(channel.guildID);
|
||||
const guild = await cacheHandlers.get("guilds", channel.guildID);
|
||||
guild?.channels.set(channel.id, channel);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,8 +9,9 @@ import {
|
||||
import { structures } from "../structures/mod.ts";
|
||||
import { eventHandlers } from "../module/client.ts";
|
||||
import { GuildUpdateChange } from "../types/options.ts";
|
||||
import { cacheHandlers } from "./cache.ts";
|
||||
|
||||
export function handleInternalGuildCreate(
|
||||
export async function handleInternalGuildCreate(
|
||||
data: DiscordPayload,
|
||||
shardID: number,
|
||||
) {
|
||||
@@ -18,49 +19,56 @@ export function handleInternalGuildCreate(
|
||||
|
||||
const payload = data.d as CreateGuildPayload;
|
||||
// When shards resume they emit GUILD_CREATE again.
|
||||
if (cache.guilds.has(payload.id)) return;
|
||||
if (await cacheHandlers.has("guilds", payload.id)) return;
|
||||
|
||||
const guild = structures.createGuild(
|
||||
data.d as CreateGuildPayload,
|
||||
shardID,
|
||||
);
|
||||
|
||||
cache.guilds.set(guild.id, guild);
|
||||
cacheHandlers.set("guilds", guild.id, guild);
|
||||
|
||||
if (cache.unavailableGuilds.get(payload.id)) {
|
||||
cache.unavailableGuilds.delete(payload.id);
|
||||
if (cacheHandlers.has("unavailableGuilds", payload.id)) {
|
||||
cacheHandlers.delete("unavailableGuilds", payload.id);
|
||||
}
|
||||
|
||||
if (!cache.isReady) return eventHandlers.guildLoaded?.(guild);
|
||||
return eventHandlers.guildCreate?.(guild);
|
||||
}
|
||||
|
||||
export function handleInternalGuildDelete(data: DiscordPayload) {
|
||||
export async function handleInternalGuildDelete(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_CREATE") return;
|
||||
|
||||
const payload = data.d as GuildDeletePayload;
|
||||
cache.messages.forEach((message) => {
|
||||
if (message.guildID === payload.id) cache.messages.delete(message.id);
|
||||
cacheHandlers.forEach("messages", (message) => {
|
||||
if (message.guildID === payload.id) {
|
||||
cacheHandlers.delete("messages", message.id);
|
||||
}
|
||||
});
|
||||
cache.channels.forEach((channel) => {
|
||||
if (channel.guildID === payload.id) cache.channels.delete(channel.id);
|
||||
|
||||
cacheHandlers.forEach("channels", (channel) => {
|
||||
if (channel.guildID === payload.id) {
|
||||
cacheHandlers.delete("channels", channel.id);
|
||||
}
|
||||
});
|
||||
cache.guilds.delete(payload.id);
|
||||
|
||||
cacheHandlers.delete("guilds", payload.id);
|
||||
|
||||
if (payload.unavailable) {
|
||||
return cache.unavailableGuilds.set(payload.id, Date.now());
|
||||
return cacheHandlers.set("unavailableGuilds", payload.id, Date.now());
|
||||
}
|
||||
|
||||
const guild = cache.guilds.get(payload.id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.id);
|
||||
if (!guild) return;
|
||||
|
||||
return eventHandlers.guildDelete?.(guild);
|
||||
}
|
||||
|
||||
export function handleInternalGuildUpdate(data: DiscordPayload) {
|
||||
export async function handleInternalGuildUpdate(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_CREATE") return;
|
||||
|
||||
const payload = data.d as UpdateGuildPayload;
|
||||
const cachedGuild = cache.guilds.get(payload.id);
|
||||
const cachedGuild = await cacheHandlers.get("guilds", payload.id);
|
||||
if (!cachedGuild) return;
|
||||
|
||||
const keysToSkip = [
|
||||
@@ -99,11 +107,11 @@ export function handleInternalGuildUpdate(data: DiscordPayload) {
|
||||
return eventHandlers.guildUpdate?.(cachedGuild, changes);
|
||||
}
|
||||
|
||||
export function handleInternalGuildEmojisUpdate(data: DiscordPayload) {
|
||||
export async function handleInternalGuildEmojisUpdate(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_EMOJIS_UPDATE") return;
|
||||
|
||||
const payload = data.d as GuildEmojisUpdatePayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
const cachedEmojis = guild.emojis;
|
||||
|
||||
@@ -8,12 +8,13 @@ import {
|
||||
GuildMemberUpdatePayload,
|
||||
} from "../types/guild.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { cacheHandlers } from "./cache.ts";
|
||||
|
||||
export function handleInternalGuildMemberAdd(data: DiscordPayload) {
|
||||
export async function handleInternalGuildMemberAdd(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_MEMBER_ADD") return;
|
||||
|
||||
const payload = data.d as GuildMemberAddPayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id)
|
||||
if (!guild) return;
|
||||
|
||||
guild.memberCount++;
|
||||
@@ -26,11 +27,11 @@ export function handleInternalGuildMemberAdd(data: DiscordPayload) {
|
||||
eventHandlers.guildMemberAdd?.(guild, member);
|
||||
}
|
||||
|
||||
export function handleInternalGuildMemberRemove(data: DiscordPayload) {
|
||||
export async function handleInternalGuildMemberRemove(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_MEMBER_REMOVE") return;
|
||||
|
||||
const payload = data.d as GuildBanPayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
guild.memberCount--;
|
||||
@@ -48,11 +49,11 @@ export function handleInternalGuildMemberRemove(data: DiscordPayload) {
|
||||
guild.members.delete(payload.user.id);
|
||||
}
|
||||
|
||||
export function handleInternalGuildMemberUpdate(data: DiscordPayload) {
|
||||
export async function handleInternalGuildMemberUpdate(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_MEMBER_UPDATE") return;
|
||||
|
||||
const payload = data.d as GuildMemberUpdatePayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
const cachedMember = guild.members.get(payload.user.id);
|
||||
@@ -96,11 +97,11 @@ export function handleInternalGuildMemberUpdate(data: DiscordPayload) {
|
||||
eventHandlers.guildMemberUpdate?.(guild, member, cachedMember);
|
||||
}
|
||||
|
||||
export function handleInternalGuildMembersChunk(data: DiscordPayload) {
|
||||
export async function handleInternalGuildMembersChunk(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_MEMBERS_CHUNK") return;
|
||||
|
||||
const payload = data.d as GuildMemberChunkPayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
payload.members.forEach((member) => {
|
||||
|
||||
@@ -6,20 +6,20 @@ import {
|
||||
MessageDeletePayload,
|
||||
MessageDeleteBulkPayload,
|
||||
} from "../types/message.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { cacheHandlers } from "./cache.ts";
|
||||
|
||||
export function handleInternalMessageCreate(data: DiscordPayload) {
|
||||
export async function handleInternalMessageCreate(data: DiscordPayload) {
|
||||
if (data.t !== "MESSAGE_CREATE") return;
|
||||
|
||||
const payload = data.d as MessageCreateOptions;
|
||||
const channel = cache.channels.get(payload.channel_id);
|
||||
const channel = await cacheHandlers.get("channels", payload.channel_id);
|
||||
if (channel) channel.lastMessageID = payload.id;
|
||||
|
||||
const message = structures.createMessage(payload);
|
||||
// Cache the message
|
||||
cache.messages.set(payload.id, message);
|
||||
cacheHandlers.set("messages", payload.id, message);
|
||||
const guild = payload.guild_id
|
||||
? cache.guilds.get(payload.guild_id)
|
||||
? await cacheHandlers.get("guilds", payload.guild_id)
|
||||
: undefined;
|
||||
|
||||
if (payload.member) {
|
||||
@@ -49,41 +49,44 @@ export function handleInternalMessageCreate(data: DiscordPayload) {
|
||||
eventHandlers.messageCreate?.(message);
|
||||
}
|
||||
|
||||
export function handleInternalMessageDelete(data: DiscordPayload) {
|
||||
export async function handleInternalMessageDelete(data: DiscordPayload) {
|
||||
if (data.t !== "MESSAGE_DELETE") return;
|
||||
|
||||
const payload = data.d as MessageDeletePayload;
|
||||
const channel = cache.channels.get(payload.channel_id);
|
||||
const channel = await cacheHandlers.get("channels", payload.channel_id);
|
||||
if (!channel) return;
|
||||
|
||||
eventHandlers.messageDelete?.(
|
||||
cache.messages.get(payload.id) || { id: payload.id, channel },
|
||||
await cacheHandlers.get("messages", payload.id) ||
|
||||
{ id: payload.id, channel },
|
||||
);
|
||||
|
||||
cache.messages.delete(payload.id);
|
||||
cacheHandlers.delete("messages", payload.id);
|
||||
}
|
||||
|
||||
export function handleInternalMessageDeleteBulk(data: DiscordPayload) {
|
||||
export async function handleInternalMessageDeleteBulk(data: DiscordPayload) {
|
||||
if (data.t !== "MESSAGE_DELETE_BULK") return;
|
||||
|
||||
const payload = data.d as MessageDeleteBulkPayload;
|
||||
const channel = cache.channels.get(payload.channel_id);
|
||||
const channel = await cacheHandlers.get("channels", payload.channel_id);
|
||||
if (!channel) return;
|
||||
|
||||
payload.ids.forEach((id) => {
|
||||
eventHandlers.messageDelete?.(cache.messages.get(id) || { id, channel });
|
||||
cache.messages.delete(id);
|
||||
payload.ids.forEach(async (id) => {
|
||||
eventHandlers.messageDelete?.(
|
||||
await cacheHandlers.get("messages", id) || { id, channel },
|
||||
);
|
||||
cacheHandlers.delete("messages", id);
|
||||
});
|
||||
}
|
||||
|
||||
export function handleInternalMessageUpdate(data: DiscordPayload) {
|
||||
export async function handleInternalMessageUpdate(data: DiscordPayload) {
|
||||
if (data.t !== "MESSAGE_UPDATE") return;
|
||||
|
||||
const payload = data.d as MessageCreateOptions;
|
||||
const channel = cache.channels.get(payload.channel_id);
|
||||
const channel = await cacheHandlers.get("channels", payload.channel_id);
|
||||
if (!channel) return;
|
||||
|
||||
const cachedMessage = cache.messages.get(payload.id);
|
||||
const cachedMessage = await cacheHandlers.get("messages", payload.id);
|
||||
if (!cachedMessage) return;
|
||||
|
||||
const oldMessage = {
|
||||
|
||||
@@ -12,6 +12,7 @@ import {
|
||||
} from "../types/discord.ts";
|
||||
import { UserPayload } from "../types/guild.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { cacheHandlers } from "./cache.ts";
|
||||
|
||||
export async function handleInternalReady(
|
||||
data: DiscordPayload,
|
||||
@@ -36,12 +37,13 @@ export async function handleInternalReady(
|
||||
allowNextShard();
|
||||
}
|
||||
|
||||
export function handleInternalPresenceUpdate(data: DiscordPayload) {
|
||||
export async function handleInternalPresenceUpdate(data: DiscordPayload) {
|
||||
if (data.t !== "PRESENCE_UPDATE") return;
|
||||
|
||||
const payload = data.d as PresenceUpdatePayload;
|
||||
const oldPresence = cache.presences.get(payload.user.id);
|
||||
cache.presences.set(payload.user.id, payload);
|
||||
const oldPresence = await cacheHandlers.get("presences", payload.user.id);
|
||||
cacheHandlers.set("presences", payload.user.id, payload);
|
||||
|
||||
return eventHandlers.presenceUpdate?.(payload, oldPresence);
|
||||
}
|
||||
|
||||
@@ -55,7 +57,7 @@ export function handleInternalUserUpdate(data: DiscordPayload) {
|
||||
|
||||
const userData = data.d as UserPayload;
|
||||
|
||||
cache.guilds.forEach((guild) => {
|
||||
cacheHandlers.forEach("guilds", (guild) => {
|
||||
const member = guild.members.get(userData.id);
|
||||
if (!member) return;
|
||||
// member.author = userData;
|
||||
@@ -69,13 +71,13 @@ export function handleInternalUserUpdate(data: DiscordPayload) {
|
||||
return eventHandlers.botUpdate?.(userData);
|
||||
}
|
||||
|
||||
export function handleInternalVoiceStateUpdate(data: DiscordPayload) {
|
||||
export async function handleInternalVoiceStateUpdate(data: DiscordPayload) {
|
||||
if (data.t !== "VOICE_STATE_UPDATE") return;
|
||||
|
||||
const payload = data.d as VoiceStateUpdatePayload;
|
||||
if (!payload.guild_id) return;
|
||||
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
const member = guild.members.get(payload.user_id) ||
|
||||
|
||||
@@ -4,15 +4,15 @@ import { DiscordPayload } from "../types/discord.ts";
|
||||
import {
|
||||
BaseMessageReactionPayload,
|
||||
MessageReactionPayload,
|
||||
MessageReactionRemoveEmojiPayload,
|
||||
MessageReactionRemoveEmojiPayload,
|
||||
} from "../types/message.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { cacheHandlers } from "./cache.ts";
|
||||
|
||||
export function handleInternalMessageReactionAdd(data: DiscordPayload) {
|
||||
export async function handleInternalMessageReactionAdd(data: DiscordPayload) {
|
||||
if (data.t !== "MESSAGE_REACTION_ADD") return;
|
||||
|
||||
const payload = data.d as MessageReactionPayload;
|
||||
const message = cache.messages.get(payload.message_id);
|
||||
const message = await cacheHandlers.get("messages", payload.message_id);
|
||||
|
||||
if (message) {
|
||||
const previousReactions = message.reactions;
|
||||
@@ -34,11 +34,11 @@ export function handleInternalMessageReactionAdd(data: DiscordPayload) {
|
||||
: [newReaction];
|
||||
}
|
||||
|
||||
cache.messages.set(payload.message_id, message);
|
||||
cacheHandlers.set("messages", payload.message_id, message);
|
||||
}
|
||||
|
||||
if (payload.member && payload.guild_id) {
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
guild?.members.set(
|
||||
payload.member.user.id,
|
||||
structures.createMember(
|
||||
@@ -62,11 +62,13 @@ export function handleInternalMessageReactionAdd(data: DiscordPayload) {
|
||||
);
|
||||
}
|
||||
|
||||
export function handleInternalMessageReactionRemove(data: DiscordPayload) {
|
||||
export async function handleInternalMessageReactionRemove(
|
||||
data: DiscordPayload,
|
||||
) {
|
||||
if (data.t !== "MESSAGE_REACTION_REMOVE") return;
|
||||
|
||||
const payload = data.d as MessageReactionPayload;
|
||||
const message = cache.messages.get(payload.message_id);
|
||||
const message = await cacheHandlers.get("messages", payload.message_id);
|
||||
|
||||
if (message) {
|
||||
const previousReactions = message.reactions;
|
||||
@@ -88,11 +90,11 @@ export function handleInternalMessageReactionRemove(data: DiscordPayload) {
|
||||
: [newReaction];
|
||||
}
|
||||
|
||||
cache.messages.set(payload.message_id, message);
|
||||
cacheHandlers.set("messages", payload.message_id, message);
|
||||
}
|
||||
|
||||
if (payload.member && payload.guild_id) {
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
guild?.members.set(
|
||||
payload.member.user.id,
|
||||
structures.createMember(
|
||||
@@ -125,5 +127,7 @@ export function handleInternalMessageReactionRemoveAll(data: DiscordPayload) {
|
||||
export function handleInternalMessageReactionRemoveEmoji(data: DiscordPayload) {
|
||||
if (data.t !== "MESSAGE_REACTION_REMOVE_EMOJI") return;
|
||||
|
||||
eventHandlers.reactionRemoveEmoji?.(data.d as MessageReactionRemoveEmojiPayload);
|
||||
eventHandlers.reactionRemoveEmoji?.(
|
||||
data.d as MessageReactionRemoveEmojiPayload,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,13 +2,13 @@ import { eventHandlers } from "../module/client.ts";
|
||||
import { structures } from "../structures/mod.ts";
|
||||
import { DiscordPayload } from "../types/discord.ts";
|
||||
import { GuildRoleDeletePayload, GuildRolePayload } from "../types/guild.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { cacheHandlers } from "./cache.ts";
|
||||
|
||||
export function handleInternalGuildRoleCreate(data: DiscordPayload) {
|
||||
export async function handleInternalGuildRoleCreate(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_ROLE_CREATE") return;
|
||||
|
||||
const payload = data.d as GuildRolePayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
const role = structures.createRole(payload.role);
|
||||
@@ -17,11 +17,11 @@ export function handleInternalGuildRoleCreate(data: DiscordPayload) {
|
||||
return eventHandlers.roleCreate?.(guild, role);
|
||||
}
|
||||
|
||||
export function handleInternalGuildRoleDelete(data: DiscordPayload) {
|
||||
export async function handleInternalGuildRoleDelete(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_ROLE_DELETE") return;
|
||||
|
||||
const payload = data.d as GuildRoleDeletePayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
const cachedRole = guild.roles.get(payload.role_id)!;
|
||||
@@ -29,11 +29,11 @@ export function handleInternalGuildRoleDelete(data: DiscordPayload) {
|
||||
eventHandlers.roleDelete?.(guild, cachedRole);
|
||||
}
|
||||
|
||||
export function handleInternalGuildRoleUpdate(data: DiscordPayload) {
|
||||
export async function handleInternalGuildRoleUpdate(data: DiscordPayload) {
|
||||
if (data.t !== "GUILD_ROLE_UPDATE") return;
|
||||
|
||||
const payload = data.d as GuildRolePayload;
|
||||
const guild = cache.guilds.get(payload.guild_id);
|
||||
const guild = await cacheHandlers.get("guilds", payload.guild_id);
|
||||
if (!guild) return;
|
||||
|
||||
const cachedRole = guild.roles.get(payload.role.id);
|
||||
|
||||
@@ -31,11 +31,11 @@ import { Intents } from "../types/options.ts";
|
||||
import { identifyPayload } from "../module/client.ts";
|
||||
import { requestAllMembers } from "../module/shardingManager.ts";
|
||||
import { MemberCreatePayload } from "../types/member.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { Member } from "../structures/member.ts";
|
||||
import { urlToBase64 } from "../utils/utils.ts";
|
||||
import { Collection } from "../utils/collection.ts";
|
||||
import { structures } from "../structures/mod.ts";
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
|
||||
/** Gets an array of all the channels ids that are the children of this category. */
|
||||
export function categoryChildrenIDs(guild: Guild, id: string) {
|
||||
@@ -144,7 +144,7 @@ export async function getChannels(guildID: string, addToCache = true) {
|
||||
return result.map((res) => {
|
||||
const channel = structures.createChannel(res, guildID);
|
||||
if (addToCache) {
|
||||
cache.channels.set(channel.id, channel);
|
||||
cacheHandlers.set("channels", channel.id, channel);
|
||||
}
|
||||
return channel;
|
||||
});
|
||||
@@ -159,7 +159,7 @@ export async function getChannel(channelID: string, addToCache = true) {
|
||||
endpoints.GUILD_CHANNEL(channelID),
|
||||
) as ChannelCreatePayload;
|
||||
const channel = structures.createChannel(result, result.guild_id);
|
||||
if (addToCache) cache.channels.set(channel.id, channel);
|
||||
if (addToCache) cacheHandlers.set("channels", channel.id, channel);
|
||||
return channel;
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ export function swapChannels(
|
||||
* ⚠️ **ADVANCED USE ONLY: Your members will be cached in your guild most likely. Only use this when you are absolutely sure the member is not cached.**
|
||||
*/
|
||||
export async function getMember(guildID: string, id: string) {
|
||||
const guild = cache.guilds.get(guildID);
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) return;
|
||||
|
||||
const data = await RequestManager.get(
|
||||
@@ -203,7 +203,7 @@ export async function getMembersByQuery(
|
||||
name: string,
|
||||
limit = 1,
|
||||
) {
|
||||
const guild = cache.guilds.get(guildID);
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) return;
|
||||
|
||||
return new Promise((resolve) => {
|
||||
@@ -296,7 +296,7 @@ export async function createGuildRole(
|
||||
|
||||
const roleData = result as RoleData;
|
||||
const role = structures.createRole(roleData);
|
||||
const guild = cache.guilds.get(guildID);
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
guild?.roles.set(role.id, role);
|
||||
return role;
|
||||
}
|
||||
|
||||
@@ -12,10 +12,10 @@ import { Permissions } from "../types/permission.ts";
|
||||
import { Errors } from "../types/errors.ts";
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
import { MessageContent, DMChannelCreatePayload } from "../types/channel.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { EditMemberOptions } from "../types/member.ts";
|
||||
import { sendMessage } from "./channel.ts";
|
||||
import { structures } from "../structures/mod.ts";
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
|
||||
/** The users custom avatar or the default avatar if you don't have a member object. */
|
||||
export function rawAvatarURL(
|
||||
@@ -46,13 +46,13 @@ export function avatarURL(
|
||||
}
|
||||
|
||||
/** Add a role to the member */
|
||||
export function addRole(
|
||||
export async function addRole(
|
||||
guildID: string,
|
||||
memberID: string,
|
||||
roleID: string,
|
||||
reason?: string,
|
||||
) {
|
||||
const botsHighestRole = highestRole(guildID, botID);
|
||||
const botsHighestRole = await highestRole(guildID, botID);
|
||||
if (
|
||||
botsHighestRole &&
|
||||
!higherRolePosition(guildID, botsHighestRole.id, roleID)
|
||||
@@ -71,13 +71,13 @@ export function addRole(
|
||||
}
|
||||
|
||||
/** Remove a role from the member */
|
||||
export function removeRole(
|
||||
export async function removeRole(
|
||||
guildID: string,
|
||||
memberID: string,
|
||||
roleID: string,
|
||||
reason?: string,
|
||||
) {
|
||||
const botsHighestRole = highestRole(guildID, botID);
|
||||
const botsHighestRole = await highestRole(guildID, botID);
|
||||
if (
|
||||
botsHighestRole &&
|
||||
!higherRolePosition(guildID, botsHighestRole.id, roleID)
|
||||
@@ -99,7 +99,7 @@ export async function sendDirectMessage(
|
||||
memberID: string,
|
||||
content: string | MessageContent,
|
||||
) {
|
||||
let dmChannel = cache.channels.get(memberID);
|
||||
let dmChannel = await cacheHandlers.get("channels", memberID);
|
||||
if (!dmChannel) {
|
||||
// If not available in cache create a new one.
|
||||
const dmChannelData = await RequestManager.post(
|
||||
@@ -107,10 +107,10 @@ export async function sendDirectMessage(
|
||||
{ recipient_id: memberID },
|
||||
) as DMChannelCreatePayload;
|
||||
// Channel create event will have added this channel to the cache
|
||||
cache.channels.delete(dmChannelData.id);
|
||||
cacheHandlers.delete("channels", dmChannelData.id);
|
||||
const channel = structures.createChannel(dmChannelData);
|
||||
// Recreate the channel and add it undert he users id
|
||||
cache.channels.set(memberID, channel);
|
||||
cacheHandlers.set("channels", memberID, channel);
|
||||
dmChannel = channel;
|
||||
}
|
||||
|
||||
@@ -119,9 +119,9 @@ export async function sendDirectMessage(
|
||||
}
|
||||
|
||||
/** Kick a member from the server */
|
||||
export function kick(guildID: string, memberID: string, reason?: string) {
|
||||
const botsHighestRole = highestRole(guildID, botID);
|
||||
const membersHighestRole = highestRole(guildID, memberID);
|
||||
export async function kick(guildID: string, memberID: string, reason?: string) {
|
||||
const botsHighestRole = await highestRole(guildID, botID);
|
||||
const membersHighestRole = await highestRole(guildID, memberID);
|
||||
if (
|
||||
botsHighestRole && membersHighestRole &&
|
||||
botsHighestRole.position <= membersHighestRole.position
|
||||
|
||||
@@ -10,6 +10,7 @@ import { MessageContent } from "../types/channel.ts";
|
||||
import { UserPayload } from "../types/guild.ts";
|
||||
import { MessageCreateOptions } from "../types/message.ts";
|
||||
import { structures } from "../structures/mod.ts";
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
|
||||
/** Delete a message */
|
||||
export async function deleteMessage(
|
||||
@@ -178,7 +179,7 @@ export async function getReactions(message: Message, reaction: string) {
|
||||
const result = (await RequestManager.get(
|
||||
endpoints.CHANNEL_MESSAGE_REACTION(message.channelID, message.id, reaction),
|
||||
)) as UserPayload[];
|
||||
const guild = message.guild();
|
||||
const guild = await cacheHandlers.get("guilds", message.guildID);
|
||||
|
||||
return result.map((res) => {
|
||||
return guild?.members.get(res.id) || res;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ChannelCreatePayload } from "../types/channel.ts";
|
||||
import { calculatePermissions } from "../utils/permissions.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
|
||||
export function createChannel(data: ChannelCreatePayload, guildID?: string) {
|
||||
const {
|
||||
@@ -41,7 +41,7 @@ export function createChannel(data: ChannelCreatePayload, guildID?: string) {
|
||||
mention: `<#${data.id}>`,
|
||||
};
|
||||
|
||||
cache.channels.set(data.id, channel);
|
||||
cacheHandlers.set("channels", data.id, channel);
|
||||
return channel;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { MessageCreateOptions } from "../types/message.ts";
|
||||
import { cache } from "../utils/cache.ts";
|
||||
|
||||
export function createMessage(data: MessageCreateOptions) {
|
||||
const {
|
||||
@@ -25,23 +24,8 @@ export function createMessage(data: MessageCreateOptions) {
|
||||
messageReference,
|
||||
timestamp: Date.parse(data.timestamp),
|
||||
editedTimestamp: editedTimestamp ? Date.parse(editedTimestamp) : undefined,
|
||||
channel: cache.channels.get(data.channel_id)!,
|
||||
guild: () => data.guild_id ? cache.guilds.get(data.guild_id) : undefined,
|
||||
member: () => message.guild()?.members.get(data.author.id),
|
||||
mentions: () =>
|
||||
data.mentions.map((mention) =>
|
||||
message.guild()?.members.get(mention.id)! ||
|
||||
cache.guilds.find((g) => g.members.has(mention.id))?.members.get(
|
||||
mention.id,
|
||||
)
|
||||
),
|
||||
};
|
||||
|
||||
// Add guildID when not sent by Discord.
|
||||
if (!message.guildID) {
|
||||
if (message.channel.guildID) message.guildID = message.channel.guildID;
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,18 +2,18 @@ import {
|
||||
Permission,
|
||||
Permissions,
|
||||
} from "../types/permission.ts";
|
||||
import { cache } from "./cache.ts";
|
||||
import { botID } from "../module/client.ts";
|
||||
import { Role } from "../structures/role.ts";
|
||||
import { Guild } from "../structures/guild.ts";
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
|
||||
/** Checks if the member has this permission. If the member is an owner or has admin perms it will always be true. */
|
||||
export function memberIDHasPermission(
|
||||
export async function memberIDHasPermission(
|
||||
memberID: string,
|
||||
guildID: string,
|
||||
permissions: Permission[],
|
||||
) {
|
||||
const guild = cache.guilds.get(guildID);
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) return false;
|
||||
|
||||
if (memberID === guild.ownerID) return true;
|
||||
@@ -48,8 +48,8 @@ export function memberHasPermission(
|
||||
);
|
||||
}
|
||||
|
||||
export function botHasPermission(guildID: string, permissions: Permissions[]) {
|
||||
const guild = cache.guilds.get(guildID);
|
||||
export async function botHasPermission(guildID: string, permissions: Permissions[]) {
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) return false;
|
||||
|
||||
const member = guild.members.get(botID);
|
||||
@@ -77,15 +77,15 @@ export function botHasChannelPermissions(
|
||||
}
|
||||
|
||||
/** Checks if a user has permissions in a channel. */
|
||||
export function hasChannelPermissions(
|
||||
export async function hasChannelPermissions(
|
||||
channelID: string,
|
||||
memberID: string,
|
||||
permissions: Permissions[],
|
||||
) {
|
||||
const channel = cache.channels.get(channelID);
|
||||
const channel = await cacheHandlers.get("channels", channelID);
|
||||
if (!channel?.guildID) return true;
|
||||
|
||||
const guild = cache.guilds.get(channel.guildID);
|
||||
const guild = await cacheHandlers.get("guilds", channel.guildID);
|
||||
if (!guild) return false;
|
||||
|
||||
if (guild.ownerID === memberID) return true;
|
||||
@@ -187,8 +187,8 @@ export function calculatePermissions(permissionBits: bigint) {
|
||||
}) as Permission[];
|
||||
}
|
||||
|
||||
export function highestRole(guildID: string, memberID: string) {
|
||||
const guild = cache.guilds.get(guildID);
|
||||
export async function highestRole(guildID: string, memberID: string) {
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) return;
|
||||
|
||||
const member = guild?.members.get(memberID);
|
||||
@@ -210,12 +210,12 @@ export function highestRole(guildID: string, memberID: string) {
|
||||
return memberHighestRole || (guild.roles.get(guild.id) as Role);
|
||||
}
|
||||
|
||||
export function higherRolePosition(
|
||||
export async function higherRolePosition(
|
||||
guildID: string,
|
||||
roleID: string,
|
||||
otherRoleID: string,
|
||||
) {
|
||||
const guild = cache.guilds.get(guildID);
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) return;
|
||||
|
||||
const role = guild.roles.get(roleID);
|
||||
|
||||
Reference in New Issue
Block a user