autoconvert handler messages

This commit is contained in:
ITOH
2021-04-24 23:17:11 +02:00
parent 6d4c51a4eb
commit be2bbbdd99
54 changed files with 181 additions and 248 deletions
+2 -2
View File
@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
export async function handleChannelCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordChannel;
const payload = data.d as Channel;
const discordenoChannel = await structures.createDiscordenoChannel(payload);
await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel);
+4 -4
View File
@@ -1,19 +1,19 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
export async function handleChannelDelete(data: DiscordGatewayPayload) {
const payload = data.d as DiscordChannel;
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", payload.id);
if (!cachedChannel) return;
if (
cachedChannel.type === DiscordChannelTypes.GUILD_VOICE && payload.guild_id
cachedChannel.type === DiscordChannelTypes.GUILD_VOICE && payload.guildId
) {
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (guild) {
return Promise.all(guild.voiceStates.map(async (vs, key) => {
+6 -6
View File
@@ -1,17 +1,17 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordChannelPinsUpdate } from "../../types/channels/channel_pins_update.ts";
import { ChannelPinsUpdate } from "../../types/channels/channel_pins_update.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
export async function handleChannelPinsUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordChannelPinsUpdate;
const payload = data.d as ChannelPinsUpdate;
const channel = await cacheHandlers.get("channels", payload.channel_id);
const channel = await cacheHandlers.get("channels", payload.channelId);
if (!channel) return;
const guild = payload.guild_id
? await cacheHandlers.get("guilds", payload.guild_id)
const guild = payload.guildId
? await cacheHandlers.get("guilds", payload.guildId)
: undefined;
eventHandlers.channelPinsUpdate?.(channel, guild, payload.last_pin_timestamp);
eventHandlers.channelPinsUpdate?.(channel, guild, payload.lastPinTimestamp);
}
+2 -2
View File
@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Channel } from "../../types/channels/channel.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
export async function handleChannelUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordChannel;
const payload = data.d as Channel;
const cachedChannel = await cacheHandlers.get("channels", payload.id);
const discordenoChannel = await structures.createDiscordenoChannel(payload);
@@ -2,16 +2,12 @@ import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandCreate(
data: DiscordGatewayPayload,
) {
eventHandlers.applicationCommandCreate?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
data.d as ApplicationCommandCreateUpdateDelete,
);
}
@@ -2,14 +2,10 @@ import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandDelete(data: DiscordGatewayPayload) {
eventHandlers.applicationCommandDelete?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
data.d as ApplicationCommandCreateUpdateDelete,
);
}
@@ -2,14 +2,10 @@ import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandUpdate(data: DiscordGatewayPayload) {
eventHandlers.applicationCommandUpdate?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
data.d as ApplicationCommandCreateUpdateDelete,
);
}
+4 -4
View File
@@ -1,12 +1,12 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGuildEmojisUpdate } from "../../types/emojis/guild_emojis_update.ts";
import { GuildEmojisUpdate } from "../../types/emojis/guild_emojis_update.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { Collection } from "../../util/collection.ts";
export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildEmojisUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildEmojisUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const cachedEmojis = guild.emojis;
@@ -14,7 +14,7 @@ export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) {
payload.emojis.map((emoji) => [emoji.id!, emoji]),
);
await cacheHandlers.set("guilds", payload.guild_id, guild);
await cacheHandlers.set("guilds", payload.guildId, guild);
eventHandlers.guildEmojisUpdate?.(
guild,
+3 -3
View File
@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
export async function handleGuildBanAdd(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const member = await cacheHandlers.get("members", payload.user.id);
+3 -3
View File
@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
import { GuildBanAddRemove } from "../../types/guilds/guild_ban_add_remove.ts";
export async function handleGuildBanRemove(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildBanAddRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const member = await cacheHandlers.get("members", payload.user.id);
+2 -2
View File
@@ -2,14 +2,14 @@ import { eventHandlers } from "../../bot.ts";
import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuild } from "../../types/guilds/guild.ts";
import { Guild } from "../../types/guilds/guild.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildCreate(
data: DiscordGatewayPayload,
shardId: number,
) {
const payload = data.d as DiscordGuild;
const payload = data.d as Guild;
// When shards resume they emit GUILD_CREATE again.
if (await cacheHandlers.has("guilds", payload.id)) return;
+2 -2
View File
@@ -1,14 +1,14 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordUnavailableGuild } from "../../types/guilds/unavailable_guild.ts";
import { UnavailableGuild } from "../../types/guilds/unavailable_guild.ts";
import { ws } from "../../ws/ws.ts";
export async function handleGuildDelete(
data: DiscordGatewayPayload,
shardId: number,
) {
const payload = data.d as DiscordUnavailableGuild;
const payload = data.d as UnavailableGuild;
const guild = await cacheHandlers.get("guilds", payload.id);
if (!guild) return;
@@ -1,14 +1,14 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildIntegrationsUpdate } from "../../types/integration/guild_integrations_update.ts";
import { GuildIntegrationsUpdate } from "../../types/integration/guild_integrations_update.ts";
export async function handleGuildIntegrationsUpdate(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordGuildIntegrationsUpdate;
const payload = data.d as GuildIntegrationsUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
eventHandlers.guildIntegrationsUpdate?.(guild);
+2 -2
View File
@@ -2,10 +2,10 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { GuildUpdateChange } from "../../types/discordeno/guild_update_change.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuild } from "../../types/guilds/guild.ts";
import { Guild } from "../../types/guilds/guild.ts";
export async function handleGuildUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuild;
const payload = data.d as Guild;
const newGuild = await cacheHandlers.get("guilds", payload.id);
if (!newGuild) return;
@@ -1,17 +1,13 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordIntegrationCreateUpdate,
IntegrationCreateUpdate,
} from "../../types/integration/integration_create_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleIntegrationCreate(
data: DiscordGatewayPayload,
) {
eventHandlers.integrationCreate?.(
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationCreateUpdate,
),
data.d as IntegrationCreateUpdate,
);
}
@@ -1,15 +1,9 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { IntegrationCreateUpdate } from "../../types/integration/integration_create_update.ts";
import {
DiscordIntegrationDelete,
} from "../../types/integration/integration_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { IntegrationDelete } from "../../types/integration/integration_delete.ts";
export function handleIntegrationDelete(data: DiscordGatewayPayload) {
eventHandlers.integrationDelete?.(
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationDelete,
),
data.d as IntegrationDelete,
);
}
@@ -1,15 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordIntegrationCreateUpdate,
IntegrationCreateUpdate,
} from "../../types/integration/integration_create_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleIntegrationUpdate(data: DiscordGatewayPayload) {
eventHandlers.integrationUpdate?.(
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationCreateUpdate,
),
data.d as IntegrationCreateUpdate,
);
}
@@ -2,15 +2,15 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { DiscordInteraction } from "../../types/interactions/interaction.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Interaction } from "../../types/interactions/interaction.ts";
export async function handleInteractionCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordInteraction;
const discordenoMember = payload.guild_id
const payload = data.d as Interaction;
const discordenoMember = payload.guildId
? await structures.createDiscordenoMember(
payload.member as DiscordGuildMemberWithUser,
payload.guild_id,
payload.member as GuildMemberWithUser,
payload.guildId,
)
: undefined;
if (discordenoMember) {
+2 -6
View File
@@ -1,13 +1,9 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordInviteCreate,
InviteCreate,
} from "../../types/invites/invite_create.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { InviteCreate } from "../../types/invites/invite_create.ts";
export function handleInviteCreate(data: DiscordGatewayPayload) {
eventHandlers.inviteCreate?.(
snakeKeysToCamelCase<InviteCreate>(data.d as DiscordInviteCreate),
data.d as InviteCreate,
);
}
+2 -6
View File
@@ -1,13 +1,9 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordInviteDelete,
InviteDelete,
} from "../../types/invites/invite_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { InviteDelete } from "../../types/invites/invite_delete.ts";
export function handleInviteDelete(data: DiscordGatewayPayload) {
eventHandlers.inviteDelete?.(
snakeKeysToCamelCase<InviteDelete>(data.d as DiscordInviteDelete),
data.d as InviteDelete,
);
}
+7 -7
View File
@@ -1,17 +1,17 @@
import { cache, cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { Collection } from "../../util/collection.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMembersChunk } from "../../types/members/guild_members_chunk.ts";
import { GuildMembersChunk } from "../../types/members/guild_members_chunk.ts";
import { Collection } from "../../util/collection.ts";
export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildMembersChunk;
const payload = data.d as GuildMembersChunk;
const members = await Promise.all(
payload.members.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember(
member,
payload.guild_id,
payload.guildId,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
@@ -26,17 +26,17 @@ export async function handleGuildMembersChunk(data: DiscordGatewayPayload) {
const resolve = cache.fetchAllMembersProcessingRequests.get(payload.nonce);
if (!resolve) return;
if (payload.chunk_index + 1 === payload.chunk_count) {
if (payload.chunkIndex + 1 === payload.chunkCount) {
cache.fetchAllMembersProcessingRequests.delete(payload.nonce);
// Only 1 chunk most likely is all members or users only request a small amount of users
if (payload.chunk_count === 1) {
if (payload.chunkCount === 1) {
return resolve(new Collection(members.map((m) => [m.id, m])));
}
return resolve(
await cacheHandlers.filter(
"members",
(m) => m.guilds.has(payload.guild_id),
(m) => m.guilds.has(payload.guildId),
),
);
}
+4 -4
View File
@@ -2,17 +2,17 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberAdd } from "../../types/members/guild_member_add.ts";
import { GuildMemberAdd } from "../../types/members/guild_member_add.ts";
export async function handleGuildMemberAdd(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildMemberAdd;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildMemberAdd;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
guild.memberCount++;
const discordenoMember = await structures.createDiscordenoMember(
payload,
payload.guild_id,
payload.guildId,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
+3 -3
View File
@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberRemove } from "../../types/members/guild_member_remove.ts";
import { GuildMemberRemove } from "../../types/members/guild_member_remove.ts";
export async function handleGuildMemberRemove(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildMemberRemove;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildMemberRemove;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
guild.memberCount--;
+7 -9
View File
@@ -2,22 +2,20 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberUpdate } from "../../types/members/guild_member_update.ts";
import { GuildMemberUpdate } from "../../types/members/guild_member_update.ts";
export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildMemberUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildMemberUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const cachedMember = await cacheHandlers.get("members", payload.user.id);
const guildMember = cachedMember?.guilds.get(payload.guild_id);
const guildMember = cachedMember?.guilds.get(payload.guildId);
const newMemberData = {
...payload,
// deno-lint-ignore camelcase
premium_since: payload.premium_since || undefined,
// deno-lint-ignore camelcase
joined_at: new Date(guildMember?.joinedAt || Date.now())
premiumSince: payload.premiumSince || undefined,
joinedAt: new Date(guildMember?.joinedAt || Date.now())
.toISOString(),
deaf: guildMember?.deaf || false,
mute: guildMember?.mute || false,
@@ -25,7 +23,7 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
};
const discordenoMember = await structures.createDiscordenoMember(
newMemberData,
payload.guild_id,
payload.guildId,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
+6 -7
View File
@@ -2,12 +2,11 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { DiscordMessage, Message } from "../../types/messages/message.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { GuildMemberWithUser } from "../../types/guilds/guild_member.ts";
import { Message } from "../../types/messages/message.ts";
export async function handleMessageCreate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase(data.d as DiscordMessage) as Message;
const payload = data.d as Message;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (channel) channel.lastMessageId = payload.id;
@@ -18,7 +17,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
if (payload.member && guild) {
// If in a guild cache the author as a member
const discordenoMember = await structures.createDiscordenoMember(
{ ...payload.member, user: payload.author } as DiscordGuildMemberWithUser,
{ ...payload.member, user: payload.author } as GuildMemberWithUser,
guild.id,
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
@@ -29,7 +28,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
// Cache the member if its a valid member
if (mention.member && guild) {
const discordenoMember = await structures.createDiscordenoMember(
{ ...mention.member, user: mention } as DiscordGuildMemberWithUser,
{ ...mention.member, user: mention } as GuildMemberWithUser,
guild.id,
);
@@ -43,7 +42,7 @@ export async function handleMessageCreate(data: DiscordGatewayPayload) {
}
const message = await structures.createDiscordenoMessage(
data.d as DiscordMessage,
data.d as Message,
);
// Cache the message
await cacheHandlers.set("messages", payload.id, message);
+3 -3
View File
@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageDelete } from "../../types/messages/message_delete.ts";
import { MessageDelete } from "../../types/messages/message_delete.ts";
export async function handleMessageDelete(data: DiscordGatewayPayload) {
const payload = data.d as DiscordMessageDelete;
const channel = await cacheHandlers.get("channels", payload.channel_id);
const payload = data.d as MessageDelete;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (!channel) return;
eventHandlers.messageDelete?.(
+3 -3
View File
@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageDeleteBulk } from "../../types/messages/message_delete_bulk.ts";
import { MessageDeleteBulk } from "../../types/messages/message_delete_bulk.ts";
export async function handleMessageDeleteBulk(data: DiscordGatewayPayload) {
const payload = data.d as DiscordMessageDeleteBulk;
const channel = await cacheHandlers.get("channels", payload.channel_id);
const payload = data.d as MessageDeleteBulk;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (!channel) return;
return Promise.all(payload.ids.map(async (id) => {
@@ -3,14 +3,13 @@ import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordMessageReactionAdd,
MessageReactionAdd,
} from "../../types/messages/message_reaction_add.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
const payload = data.d as DiscordMessageReactionAdd;
const message = await cacheHandlers.get("messages", payload.message_id);
const payload = data.d as MessageReactionAdd;
const message = await cacheHandlers.get("messages", payload.messageId);
if (message) {
const reactionExisted = message.reactions?.find(
@@ -23,7 +22,7 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
else {
const newReaction = {
count: 1,
me: payload.user_id === botId,
me: payload.userId === botId,
emoji: { ...payload.emoji, id: payload.emoji.id || undefined },
};
message.reactions = message.reactions
@@ -31,11 +30,11 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
: [newReaction];
}
await cacheHandlers.set("messages", payload.message_id, message);
await cacheHandlers.set("messages", payload.messageId, message);
}
if (payload.member && payload.guild_id) {
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (payload.member && payload.guildId) {
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (guild) {
const discordenoMember = await structures.createDiscordenoMember(
payload.member,
@@ -2,16 +2,14 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordMessageReactionRemove,
MessageReactionRemove,
} from "../../types/messages/message_reaction_remove.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionRemove(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordMessageReactionRemove;
const message = await cacheHandlers.get("messages", payload.message_id);
const payload = data.d as MessageReactionRemove;
const message = await cacheHandlers.get("messages", payload.messageId);
if (message) {
const reaction = message.reactions?.find((reaction) =>
@@ -27,12 +25,12 @@ export async function handleMessageReactionRemove(
}
if (!message.reactions?.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
await cacheHandlers.set("messages", payload.messageId, message);
}
}
eventHandlers.reactionRemove?.(
snakeKeysToCamelCase<MessageReactionRemove>(payload),
payload,
message,
);
}
@@ -2,25 +2,23 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordMessageReactionRemoveAll,
MessageReactionRemoveAll,
} from "../../types/messages/message_reaction_remove_all.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionRemoveAll(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordMessageReactionRemoveAll;
const message = await cacheHandlers.get("messages", payload.message_id);
const payload = data.d as MessageReactionRemoveAll;
const message = await cacheHandlers.get("messages", payload.messageId);
if (message?.reactions) {
message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
await cacheHandlers.set("messages", payload.messageId, message);
}
eventHandlers.reactionRemoveAll?.(
snakeKeysToCamelCase<MessageReactionRemoveAll>(payload),
payload,
message,
);
}
@@ -1,13 +1,13 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageReactionRemoveEmoji } from "../../types/messages/message_reaction_remove_emoji.ts";
import { MessageReactionRemoveEmoji } from "../../types/messages/message_reaction_remove_emoji.ts";
export async function handleMessageReactionRemoveEmoji(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordMessageReactionRemoveEmoji;
const message = await cacheHandlers.get("messages", payload.message_id);
const payload = data.d as MessageReactionRemoveEmoji;
const message = await cacheHandlers.get("messages", payload.messageId);
if (message?.reactions) {
message.reactions = message.reactions.filter(
@@ -21,13 +21,13 @@ export async function handleMessageReactionRemoveEmoji(
if (!message.reactions.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
await cacheHandlers.set("messages", payload.messageId, message);
}
eventHandlers.reactionRemoveEmoji?.(
payload.emoji,
payload.message_id,
payload.channel_id,
payload.guild_id,
payload.messageId,
payload.channelId,
payload.guildId,
);
}
+4 -4
View File
@@ -2,11 +2,11 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessage } from "../../types/messages/message.ts";
import { Message } from "../../types/messages/message.ts";
export async function handleMessageUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordMessage;
const channel = await cacheHandlers.get("channels", payload.channel_id);
const payload = data.d as Message;
const channel = await cacheHandlers.get("channels", payload.channelId);
if (!channel) return;
const oldMessage = await cacheHandlers.get("messages", payload.id);
@@ -14,7 +14,7 @@ export async function handleMessageUpdate(data: DiscordGatewayPayload) {
// Messages with embeds can trigger update but they wont have edited_timestamp
if (
!payload.edited_timestamp ||
!payload.editedTimestamp ||
(oldMessage.content === payload.content)
) {
return;
+3 -8
View File
@@ -1,16 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordPresenceUpdate,
PresenceUpdate,
} from "../../types/misc/presence_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { PresenceUpdate } from "../../types/misc/presence_update.ts";
export async function handlePresenceUpdate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase<PresenceUpdate>(
data.d as DiscordPresenceUpdate,
);
const payload = data.d as PresenceUpdate;
const oldPresence = await cacheHandlers.get("presences", payload.user.id);
await cacheHandlers.set("presences", payload.user.id, payload);
+5 -6
View File
@@ -3,9 +3,8 @@ import { cache, cacheHandlers } from "../../cache.ts";
import { initialMemberLoadQueue } from "../../structures/guild.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordReady } from "../../types/gateway/ready.ts";
import { DiscordGuildMemberWithUser } from "../../types/mod.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
import { Ready } from "../../types/gateway/ready.ts";
import { GuildMemberWithUser } from "../../types/mod.ts";
import { ws } from "../../ws/ws.ts";
export function handleReady(
@@ -15,7 +14,7 @@ export function handleReady(
// The bot has already started, the last shard is resumed, however.
if (cache.isReady) return;
const payload = data.d as DiscordReady;
const payload = data.d as Ready;
setBotId(payload.user.id);
setApplicationId(payload.application.id);
@@ -44,7 +43,7 @@ export function handleReady(
// Don't pass the shard itself because unavailableGuilds won't be updated by the GUILD_CREATE event
/** This function checks if the shard is fully loaded */
async function checkReady(payload: DiscordReady, shardId: number, now: number) {
async function checkReady(payload: Ready, shardId: number, now: number) {
const shard = ws.shards.get(shardId);
if (!shard) return;
@@ -100,7 +99,7 @@ async function loaded(shardId: number) {
await Promise.allSettled(
members.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember(
camelKeysToSnakeCase<DiscordGuildMemberWithUser>(member),
member as GuildMemberWithUser,
guildId,
);
+2 -6
View File
@@ -1,13 +1,9 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordTypingStart,
TypingStart,
} from "../../types/misc/typing_start.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { TypingStart } from "../../types/misc/typing_start.ts";
export function handleTypingStart(data: DiscordGatewayPayload) {
eventHandlers.typingStart?.(
snakeKeysToCamelCase<TypingStart>(data.d as DiscordTypingStart),
data.d as TypingStart,
);
}
+2 -3
View File
@@ -1,11 +1,10 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordUser, User } from "../../types/users/user.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
import { User } from "../../types/users/user.ts";
export async function handleUserUpdate(data: DiscordGatewayPayload) {
const userData = camelKeysToSnakeCase(data.d as DiscordUser) as User;
const userData = data.d as User;
const member = await cacheHandlers.get("members", userData.id);
if (!member) return;
+4 -4
View File
@@ -2,16 +2,16 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildRoleCreate } from "../../types/mod.ts";
import { GuildRoleCreate } from "../../types/mod.ts";
export async function handleGuildRoleCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildRoleCreate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildRoleCreate;
const guild = await cacheHandlers.get("guilds", 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.guild_id, guild);
await cacheHandlers.set("guilds", payload.guildId, guild);
eventHandlers.roleCreate?.(guild, role);
}
+7 -7
View File
@@ -1,15 +1,15 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildRoleDelete } from "../../types/guilds/guild_role_delete.ts";
import { GuildRoleDelete } from "../../types/guilds/guild_role_delete.ts";
export async function handleGuildRoleDelete(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildRoleDelete;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildRoleDelete;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const cachedRole = guild.roles.get(payload.role_id)!;
guild.roles.delete(payload.role_id);
const cachedRole = guild.roles.get(payload.roleId)!;
guild.roles.delete(payload.roleId);
if (cachedRole) eventHandlers.roleDelete?.(guild, cachedRole);
@@ -28,9 +28,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.role_id)) return;
if (!g.roles.includes(payload.roleId)) return;
// Remove this role from the members cache
g.roles = g.roles.filter((id) => id !== payload.role_id);
g.roles = g.roles.filter((id) => id !== payload.roleId);
cacheHandlers.set("members", member.id, member);
});
});
+3 -3
View File
@@ -2,11 +2,11 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildRoleUpdate } from "../../types/mod.ts";
import { GuildRoleUpdate } from "../../types/mod.ts";
export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildRoleUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const payload = data.d as GuildRoleUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const cachedRole = guild.roles.get(payload.role.id);
+2 -8
View File
@@ -1,16 +1,10 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import {
DiscordVoiceServerUpdate,
VoiceServerUpdate,
} from "../../types/voice/voice_server_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
import { VoiceServerUpdate } from "../../types/voice/voice_server_update.ts";
export async function handleVoiceServerUpdate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase<VoiceServerUpdate>(
data.d as DiscordVoiceServerUpdate,
);
const payload = data.d as VoiceServerUpdate;
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
+2 -6
View File
@@ -2,15 +2,11 @@ import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildMemberWithUser } from "../../types/mod.ts";
import {
DiscordVoiceState,
VoiceState,
} from "../../types/voice/voice_state.ts";
import {
camelKeysToSnakeCase,
snakeKeysToCamelCase,
} from "../../util/utils.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase<VoiceState>(
@@ -23,7 +19,7 @@ export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
const member = payload.member
? await structures.createDiscordenoMember(
camelKeysToSnakeCase<DiscordGuildMemberWithUser>(payload),
payload.member,
guild.id,
)
: await cacheHandlers.get("members", payload.userId);
+4 -4
View File
@@ -1,11 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordWebhookUpdate } from "../../types/webhooks/webhooks_update.ts";
import { WebhookUpdate } from "../../types/webhooks/webhooks_update.ts";
export function handleWebhooksUpdate(data: DiscordGatewayPayload) {
const options = data.d as DiscordWebhookUpdate;
const options = data.d as WebhookUpdate;
eventHandlers.webhooksUpdate?.(
options.channel_id,
options.guild_id,
options.channelId,
options.guildId,
);
}