Merge branch 'main' into threads

This commit is contained in:
ITOH
2021-05-03 20:50:53 +02:00
192 changed files with 1364 additions and 623 deletions
+10 -5
View File
@@ -3,21 +3,25 @@ import { cacheHandlers } from "../../cache.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelDelete(data: DiscordGatewayPayload) {
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", payload.id);
const cachedChannel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id),
);
if (!cachedChannel) return;
if (
cachedChannel.type === DiscordChannelTypes.GuildVoice && payload.guildId
) {
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get("guilds", cachedChannel.guildId);
if (guild) {
return Promise.all(guild.voiceStates.map(async (vs, key) => {
if (vs.channelId !== payload.id) return;
if (vs.channelId !== cachedChannel.id) return;
// Since this channel was deleted all voice states for this channel should be deleted
guild.voiceStates.delete(key);
@@ -38,18 +42,19 @@ export async function handleChannelDelete(data: DiscordGatewayPayload) {
DiscordChannelTypes.GuildNews,
].includes(payload.type)
) {
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
cacheHandlers.forEach("messages", (message) => {
eventHandlers.debug?.(
"loop",
`Running forEach messages loop in CHANNEL_DELTE file.`,
);
if (message.channelId === payload.id) {
if (message.channelId === snowflakeToBigint(payload.id)) {
cacheHandlers.delete("messages", message.id);
}
});
}
await cacheHandlers.delete("channels", payload.id);
await cacheHandlers.delete("channels", snowflakeToBigint(payload.id));
eventHandlers.channelDelete?.(cachedChannel);
}
+6 -2
View File
@@ -2,15 +2,19 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { ChannelPinsUpdate } from "../../types/channels/channel_pins_update.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelPinsUpdate(data: DiscordGatewayPayload) {
const payload = data.d as ChannelPinsUpdate;
const channel = await cacheHandlers.get("channels", payload.channelId);
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (!channel) return;
const guild = payload.guildId
? await cacheHandlers.get("guilds", payload.guildId)
? await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId))
: undefined;
eventHandlers.channelPinsUpdate?.(channel, guild, payload.lastPinTimestamp);
+5 -1
View File
@@ -3,10 +3,14 @@ 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 { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleChannelUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", payload.id);
const cachedChannel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.id),
);
if (!cachedChannel) return;
const discordenoChannel = await structures.createDiscordenoChannel(payload);
+7 -3
View File
@@ -2,19 +2,23 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { GuildEmojisUpdate } from "../../types/emojis/guild_emojis_update.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { Collection } from "../../util/collection.ts";
export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildEmojisUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
const cachedEmojis = guild.emojis;
guild.emojis = new Collection(
payload.emojis.map((emoji) => [emoji.id!, emoji]),
payload.emojis.map((emoji) => [snowflakeToBigint(emoji.id!), emoji]),
);
await cacheHandlers.set("guilds", payload.guildId, guild);
await cacheHandlers.set("guilds", guild.id, guild);
eventHandlers.guildEmojisUpdate?.(
guild,
+9 -2
View File
@@ -2,12 +2,19 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildBanAdd(data: DiscordGatewayPayload) {
const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
const member = await cacheHandlers.get("members", payload.user.id);
const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id),
);
eventHandlers.guildBanAdd?.(guild, payload.user, member);
}
+9 -2
View File
@@ -2,12 +2,19 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildBanRemove(data: DiscordGatewayPayload) {
const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
const member = await cacheHandlers.get("members", payload.user.id);
const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id),
);
eventHandlers.guildBanRemove?.(guild, payload.user, member);
}
+10 -9
View File
@@ -3,6 +3,7 @@ import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Guild } from "../../types/guilds/guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildCreate(
@@ -11,24 +12,24 @@ export async function handleGuildCreate(
) {
const payload = data.d as Guild;
// When shards resume they emit GUILD_CREATE again.
if (await cacheHandlers.has("guilds", payload.id)) return;
if (await cacheHandlers.has("guilds", snowflakeToBigint(payload.id))) return;
const discordenoGuild = await structures.createDiscordenoGuild(
const guild = await structures.createDiscordenoGuild(
payload,
shardId,
);
await cacheHandlers.set("guilds", discordenoGuild.id, discordenoGuild);
await cacheHandlers.set("guilds", guild.id, guild);
const shard = ws.shards.get(shardId);
if (shard?.unavailableGuildIds.has(payload.id)) {
await cacheHandlers.delete("unavailableGuilds", payload.id);
if (shard?.unavailableGuildIds.has(guild.id)) {
await cacheHandlers.delete("unavailableGuilds", guild.id);
shard.unavailableGuildIds.delete(payload.id);
shard.unavailableGuildIds.delete(guild.id);
return eventHandlers.guildAvailable?.(discordenoGuild);
return eventHandlers.guildAvailable?.(guild);
}
if (!cache.isReady) return eventHandlers.guildLoaded?.(discordenoGuild);
eventHandlers.guildCreate?.(discordenoGuild);
if (!cache.isReady) return eventHandlers.guildLoaded?.(guild);
eventHandlers.guildCreate?.(guild);
}
+12 -8
View File
@@ -2,6 +2,7 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { UnavailableGuild } from "../../types/guilds/unavailable_guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildDelete(
@@ -10,16 +11,19 @@ export async function handleGuildDelete(
) {
const payload = data.d as UnavailableGuild;
const guild = await cacheHandlers.get("guilds", payload.id);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.id),
);
if (!guild) return;
await cacheHandlers.delete("guilds", payload.id);
await cacheHandlers.delete("guilds", guild.id);
if (payload.unavailable) {
const shard = ws.shards.get(shardId);
if (shard) shard.unavailableGuildIds.add(payload.id);
if (shard) shard.unavailableGuildIds.add(guild.id);
await cacheHandlers.set("unavailableGuilds", payload.id, Date.now());
await cacheHandlers.set("unavailableGuilds", guild.id, Date.now());
eventHandlers.guildUnavailable?.(guild);
} else {
@@ -31,7 +35,7 @@ export async function handleGuildDelete(
"loop",
`1. Running forEach messages loop in CHANNEL_DELTE file.`,
);
if (message.guildId === payload.id) {
if (message.guildId === guild.id) {
cacheHandlers.delete("messages", message.id);
}
});
@@ -41,7 +45,7 @@ export async function handleGuildDelete(
"loop",
`2. Running forEach channels loop in CHANNEL_DELTE file.`,
);
if (channel.guildId === payload.id) {
if (channel.guildId === guild.id) {
cacheHandlers.delete("channels", channel.id);
}
});
@@ -51,9 +55,9 @@ export async function handleGuildDelete(
"loop",
`3. Running forEach members loop in CHANNEL_DELTE file.`,
);
if (!member.guilds.has(payload.id)) return;
if (!member.guilds.has(guild.id)) return;
member.guilds.delete(payload.id);
member.guilds.delete(guild.id);
if (!member.guilds.size) {
return cacheHandlers.delete("members", member.id);
@@ -2,13 +2,17 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildIntegrationsUpdate } from "../../types/integration/guild_integrations_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildIntegrationsUpdate(
data: DiscordGatewayPayload,
) {
const payload = data.d as GuildIntegrationsUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
eventHandlers.guildIntegrationsUpdate?.(guild);
+7 -2
View File
@@ -3,13 +3,18 @@ import { cacheHandlers } from "../../cache.ts";
import { GuildUpdateChange } from "../../types/discordeno/guild_update_change.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Guild } from "../../types/guilds/guild.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Guild;
const newGuild = await cacheHandlers.get("guilds", payload.id);
const newGuild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.id),
);
if (!newGuild) return;
const keysToSkip = [
"id",
"roles",
"guildHashes",
"guildId",
@@ -40,7 +45,7 @@ export async function handleGuildUpdate(data: DiscordGatewayPayload) {
}
}).filter((change) => change) as GuildUpdateChange[];
await cacheHandlers.set("guilds", payload.id, newGuild);
await cacheHandlers.set("guilds", newGuild.id, newGuild);
eventHandlers.guildUpdate?.(newGuild, changes);
}
@@ -4,13 +4,14 @@ import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Interaction } from "../../types/interactions/interaction.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleInteractionCreate(data: DiscordGatewayPayload) {
const payload = data.d as Interaction;
const discordenoMember = payload.guildId
? await structures.createDiscordenoMember(
payload.member as GuildMemberWithUser,
payload.guildId,
snowflakeToBigint(payload.guildId),
)
: undefined;
if (discordenoMember) {
+5 -2
View File
@@ -2,16 +2,19 @@ import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMembersChunk } from "../../types/members/guild_members_chunk.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { Collection } from "../../util/collection.ts";
export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
const payload = data.d as GuildMembersChunk;
const guildId = snowflakeToBigint(payload.guildId);
const members = await Promise.all(
payload.members.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember(
member,
payload.guildId,
guildId,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
@@ -36,7 +39,7 @@ export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
return resolve(
await cacheHandlers.filter(
"members",
(m) => m.guilds.has(payload.guildId),
(m) => m.guilds.has(guildId),
),
);
}
+6 -2
View File
@@ -3,16 +3,20 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberAdd } from "../../types/members/guild_member_add.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberAdd(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberAdd;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
guild.memberCount++;
const discordenoMember = await structures.createDiscordenoMember(
payload,
payload.guildId,
guild.id,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
+9 -2
View File
@@ -2,14 +2,21 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberRemove } from "../../types/members/guild_member_remove.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberRemove(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
guild.memberCount--;
const member = await cacheHandlers.get("members", payload.user.id);
const member = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id),
);
eventHandlers.guildMemberRemove?.(guild, payload.user, member);
member?.guilds.delete(guild.id);
+18 -7
View File
@@ -3,14 +3,21 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberUpdate } from "../../types/members/guild_member_update.ts";
import { bigintToSnowflake, snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildMemberUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
const cachedMember = await cacheHandlers.get("members", payload.user.id);
const guildMember = cachedMember?.guilds.get(payload.guildId);
const cachedMember = await cacheHandlers.get(
"members",
snowflakeToBigint(payload.user.id),
);
const guildMember = cachedMember?.guilds.get(guild.id);
const newMemberData = {
...payload,
@@ -23,7 +30,7 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
};
const discordenoMember = await structures.createDiscordenoMember(
newMemberData,
payload.guildId,
guild.id,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
@@ -48,7 +55,7 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
"loop",
`1. Running forEach loop in GUILD_MEMBER_UPDATE file.`,
);
if (!payload.roles.includes(id)) {
if (!payload.roles.includes(bigintToSnowflake(id))) {
eventHandlers.roleLost?.(guild, discordenoMember, id);
}
});
@@ -58,8 +65,12 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
"loop",
`2. Running forEach loop in GUILD_MEMBER_UPDATE file.`,
);
if (!roleIds.includes(id)) {
eventHandlers.roleGained?.(guild, discordenoMember, id);
if (!roleIds.includes(snowflakeToBigint(id))) {
eventHandlers.roleGained?.(
guild,
discordenoMember,
snowflakeToBigint(id),
);
}
});
}
+9 -5
View File
@@ -4,14 +4,18 @@ import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Message } from "../../types/messages/message.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageCreate(data: DiscordGatewayPayload) {
const payload = data.d as Message;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (channel) channel.lastMessageId = payload.id;
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (channel) channel.lastMessageId = snowflakeToBigint(payload.id);
const guild = payload.guildId
? await cacheHandlers.get("guilds", payload.guildId)
? await cacheHandlers.get("guilds", snowflakeToBigint(payload.guildId))
: undefined;
if (payload.member && guild) {
@@ -34,7 +38,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
return cacheHandlers.set(
"members",
mention.id,
snowflakeToBigint(mention.id),
discordenoMember,
);
}
@@ -45,7 +49,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
data.d as Message,
);
// Cache the message
await cacheHandlers.set("messages", payload.id, message);
await cacheHandlers.set("messages", snowflakeToBigint(payload.id), message);
eventHandlers.messageCreate?.(message);
}
+7 -3
View File
@@ -2,16 +2,20 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { MessageDelete } from "../../types/messages/message_delete.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageDelete(data: DiscordGatewayPayload) {
const payload = data.d as MessageDelete;
const channel = await cacheHandlers.get("channels", payload.channelId);
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (!channel) return;
eventHandlers.messageDelete?.(
{ id: payload.id, channel },
await cacheHandlers.get("messages", payload.id),
await cacheHandlers.get("messages", snowflakeToBigint(payload.id)),
);
await cacheHandlers.delete("messages", payload.id);
await cacheHandlers.delete("messages", snowflakeToBigint(payload.id));
}
+7 -3
View File
@@ -2,17 +2,21 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { MessageDeleteBulk } from "../../types/messages/message_delete_bulk.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageDeleteBulk(data: DiscordGatewayPayload) {
const payload = data.d as MessageDeleteBulk;
const channel = await cacheHandlers.get("channels", payload.channelId);
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (!channel) return;
return Promise.all(payload.ids.map(async (id) => {
eventHandlers.messageDelete?.(
{ id, channel },
await cacheHandlers.get("messages", id),
await cacheHandlers.get("messages", snowflakeToBigint(id)),
);
await cacheHandlers.delete("messages", id);
await cacheHandlers.delete("messages", snowflakeToBigint(id));
}));
}
+15 -4
View File
@@ -5,11 +5,15 @@ import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
MessageReactionAdd,
} from "../../types/messages/message_reaction_add.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
const payload = data.d as MessageReactionAdd;
const message = await cacheHandlers.get("messages", payload.messageId);
const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId),
);
if (message) {
const reactionExisted = message.reactions?.find(
@@ -22,7 +26,7 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
else {
const newReaction = {
count: 1,
me: payload.userId === botId,
me: snowflakeToBigint(payload.userId) === botId,
emoji: { ...payload.emoji, id: payload.emoji.id || undefined },
};
message.reactions = message.reactions
@@ -30,11 +34,18 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
: [newReaction];
}
await cacheHandlers.set("messages", payload.messageId, message);
await cacheHandlers.set(
"messages",
snowflakeToBigint(payload.messageId),
message,
);
}
if (payload.member && payload.guildId) {
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (guild) {
const discordenoMember = await structures.createDiscordenoMember(
payload.member,
@@ -4,12 +4,16 @@ import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
MessageReactionRemove,
} from "../../types/messages/message_reaction_remove.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemove(
data: DiscordGatewayPayload,
) {
const payload = data.d as MessageReactionRemove;
const message = await cacheHandlers.get("messages", payload.messageId);
const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId),
);
if (message) {
const reaction = message.reactions?.find((reaction) =>
@@ -25,7 +29,7 @@ export async function handleMessageReactionRemove(
}
if (!message.reactions?.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.messageId, message);
await cacheHandlers.set("messages", message.id, message);
}
}
@@ -4,17 +4,25 @@ import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
MessageReactionRemoveAll,
} from "../../types/messages/message_reaction_remove_all.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemoveAll(
data: DiscordGatewayPayload,
) {
const payload = data.d as MessageReactionRemoveAll;
const message = await cacheHandlers.get("messages", payload.messageId);
const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId),
);
if (message?.reactions) {
message.reactions = undefined;
await cacheHandlers.set("messages", payload.messageId, message);
await cacheHandlers.set(
"messages",
snowflakeToBigint(payload.messageId),
message,
);
}
eventHandlers.reactionRemoveAll?.(
@@ -2,12 +2,16 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { MessageReactionRemoveEmoji } from "../../types/messages/message_reaction_remove_emoji.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageReactionRemoveEmoji(
data: DiscordGatewayPayload,
) {
const payload = data.d as MessageReactionRemoveEmoji;
const message = await cacheHandlers.get("messages", payload.messageId);
const message = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.messageId),
);
if (message?.reactions) {
message.reactions = message.reactions.filter(
@@ -21,13 +25,13 @@ export async function handleMessageReactionRemoveEmoji(
if (!message.reactions.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.messageId, message);
await cacheHandlers.set("messages", message.id, message);
}
eventHandlers.reactionRemoveEmoji?.(
payload.emoji,
payload.messageId,
payload.channelId,
payload.guildId,
snowflakeToBigint(payload.messageId),
snowflakeToBigint(payload.channelId),
payload.guildId ? snowflakeToBigint(payload.guildId) : undefined,
);
}
+10 -3
View File
@@ -3,13 +3,20 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Message } from "../../types/messages/message.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleMessageUpdate(data: DiscordGatewayPayload) {
const payload = data.d as Message;
const channel = await cacheHandlers.get("channels", payload.channelId);
const channel = await cacheHandlers.get(
"channels",
snowflakeToBigint(payload.channelId),
);
if (!channel) return;
const oldMessage = await cacheHandlers.get("messages", payload.id);
const oldMessage = await cacheHandlers.get(
"messages",
snowflakeToBigint(payload.id),
);
if (!oldMessage) return;
// Messages with embeds can trigger update but they wont have edited_timestamp
@@ -22,7 +29,7 @@ export async function handleMessageUpdate(data: DiscordGatewayPayload) {
const message = await structures.createDiscordenoMessage(payload);
await cacheHandlers.set("messages", payload.id, message);
await cacheHandlers.set("messages", snowflakeToBigint(payload.id), message);
eventHandlers.messageUpdate?.(message, oldMessage);
}
+10 -2
View File
@@ -2,12 +2,20 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { PresenceUpdate } from "../../types/misc/presence_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handlePresenceUpdate(data: DiscordGatewayPayload) {
const payload = data.d as PresenceUpdate;
const oldPresence = await cacheHandlers.get("presences", payload.user.id);
await cacheHandlers.set("presences", payload.user.id, payload);
const oldPresence = await cacheHandlers.get(
"presences",
snowflakeToBigint(payload.user.id),
);
await cacheHandlers.set(
"presences",
snowflakeToBigint(payload.user.id),
payload,
);
eventHandlers.presenceUpdate?.(payload, oldPresence);
}
+4 -1
View File
@@ -5,6 +5,7 @@ import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Ready } from "../../types/gateway/ready.ts";
import { GuildMemberWithUser } from "../../types/mod.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
import { ws } from "../../ws/ws.ts";
export function handleReady(
@@ -29,7 +30,9 @@ export function handleReady(
// Set ready to false just to go sure
shard.ready = false;
// All guilds are unavailable at first
shard.unavailableGuildIds = new Set(payload.guilds.map((g) => g.id));
shard.unavailableGuildIds = new Set(
payload.guilds.map((g) => snowflakeToBigint(g.id)),
);
// Start ready check in 2 seconds
setTimeout(async () => {
+6 -2
View File
@@ -2,11 +2,15 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { User } from "../../types/users/user.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleUserUpdate(data: DiscordGatewayPayload) {
const userData = data.d as User;
const member = await cacheHandlers.get("members", userData.id);
const member = await cacheHandlers.get(
"members",
snowflakeToBigint(userData.id),
);
if (!member) return;
Object.entries(userData).forEach(([key, value]) => {
@@ -18,7 +22,7 @@ export async function handleUserUpdate(data: DiscordGatewayPayload) {
if (member[key] !== value) return member[key] = value;
});
await cacheHandlers.set("members", userData.id, member);
await cacheHandlers.set("members", snowflakeToBigint(userData.id), member);
eventHandlers.botUpdate?.(userData);
}
+11 -4
View File
@@ -3,15 +3,22 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildRoleCreate } from "../../types/mod.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleCreate(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleCreate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
const role = await structures.createDiscordenoRole(payload);
guild.roles = guild.roles.set(payload.role.id, role);
await cacheHandlers.set("guilds", payload.guildId, guild);
const role = await structures.createDiscordenoRole({
...payload,
guildId: guild.id,
});
guild.roles = guild.roles.set(snowflakeToBigint(payload.role.id), role);
await cacheHandlers.set("guilds", guild.id, guild);
eventHandlers.roleCreate?.(guild, role);
}
+11 -5
View File
@@ -2,14 +2,20 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildRoleDelete } from "../../types/guilds/guild_role_delete.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleDelete;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
const cachedRole = guild.roles.get(payload.roleId)!;
guild.roles.delete(payload.roleId);
const roleId = snowflakeToBigint(payload.roleId);
const cachedRole = guild.roles.get(roleId)!;
guild.roles.delete(roleId);
if (cachedRole) eventHandlers.roleDelete?.(guild, cachedRole);
@@ -28,9 +34,9 @@ export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
`2. Running forEach loop in CHANNEL_DELTE file.`,
);
// Member does not have this role
if (!g.roles.includes(payload.roleId)) return;
if (!g.roles.includes(roleId)) return;
// Remove this role from the members cache
g.roles = g.roles.filter((id) => id !== payload.roleId);
g.roles = g.roles.filter((id) => id !== roleId);
cacheHandlers.set("members", member.id, member);
});
});
+11 -4
View File
@@ -3,17 +3,24 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { GuildRoleUpdate } from "../../types/mod.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) {
const payload = data.d as GuildRoleUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
const cachedRole = guild.roles.get(payload.role.id);
const cachedRole = guild.roles.get(snowflakeToBigint(payload.role.id));
if (!cachedRole) return;
const role = await structures.createDiscordenoRole(payload);
guild.roles.set(payload.role.id, role);
const role = await structures.createDiscordenoRole({
...payload,
guildId: guild.id,
});
guild.roles.set(snowflakeToBigint(payload.role.id), role);
await cacheHandlers.set("guilds", guild.id, guild);
eventHandlers.roleUpdate?.(guild, role, cachedRole);
+5 -1
View File
@@ -2,11 +2,15 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { VoiceServerUpdate } from "../../types/voice/voice_server_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleVoiceServerUpdate(data: DiscordGatewayPayload) {
const payload = data.d as VoiceServerUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
eventHandlers.voiceServerUpdate?.(payload, guild);
+20 -11
View File
@@ -3,13 +3,16 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { VoiceState } from "../../types/voice/voice_state.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
const payload = data.d as VoiceState;
if (!payload.guildId) return;
const guild = await cacheHandlers.get("guilds", payload.guildId);
const guild = await cacheHandlers.get(
"guilds",
snowflakeToBigint(payload.guildId),
);
if (!guild) return;
const member = payload.member
@@ -17,34 +20,40 @@ export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
payload.member,
guild.id,
)
: await cacheHandlers.get("members", payload.userId);
: await cacheHandlers.get("members", snowflakeToBigint(payload.userId));
if (!member) return;
// No cached state before so lets make one for em
const cachedState = guild.voiceStates.get(payload.userId);
const cachedState = guild.voiceStates.get(snowflakeToBigint(payload.userId));
guild.voiceStates.set(
payload.userId,
payload,
snowflakeToBigint(payload.userId),
await structures.createDiscordenoVoiceState(guild.id, payload),
);
await cacheHandlers.set("guilds", payload.guildId, guild);
await cacheHandlers.set("guilds", guild.id, guild);
if (cachedState?.channelId !== payload.channelId) {
if (
cachedState?.channelId !==
(payload.channelId ? snowflakeToBigint(payload.channelId) : null)
) {
// Either joined or moved channels
if (payload.channelId) {
if (cachedState?.channelId) { // Was in a channel before
eventHandlers.voiceChannelSwitch?.(
member,
payload.channelId,
snowflakeToBigint(payload.channelId),
cachedState.channelId,
);
} else { // Was not in a channel before so user just joined
eventHandlers.voiceChannelJoin?.(member, payload.channelId);
eventHandlers.voiceChannelJoin?.(
member,
snowflakeToBigint(payload.channelId),
);
}
} // Left the channel
else if (cachedState?.channelId) {
guild.voiceStates.delete(payload.userId);
guild.voiceStates.delete(snowflakeToBigint(payload.userId));
eventHandlers.voiceChannelLeave?.(member, cachedState.channelId);
}
}
+3 -2
View File
@@ -1,11 +1,12 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { WebhookUpdate } from "../../types/webhooks/webhooks_update.ts";
import { snowflakeToBigint } from "../../util/bigint.ts";
export function handleWebhooksUpdate(data: DiscordGatewayPayload) {
const options = data.d as WebhookUpdate;
eventHandlers.webhooksUpdate?.(
options.channelId,
options.guildId,
snowflakeToBigint(options.channelId),
snowflakeToBigint(options.guildId),
);
}