This commit is contained in:
ITOH
2021-04-12 09:30:45 +02:00
parent 51e27d8f17
commit b9bd29ebf8
44 changed files with 268 additions and 221 deletions
@@ -1,19 +1,17 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordApplicationCommandCreateUpdateDelete } from "../../types/interactions/application_command_create_update_delete.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandCreate(
data: DiscordGatewayPayload,
) {
const {
guild_id: guildId,
application_id: applicationId,
...rest
} = data.d as DiscordApplicationCommandCreateUpdateDelete;
eventHandlers.applicationCommandCreate?.({
...rest,
guildId,
applicationId,
});
eventHandlers.applicationCommandCreate?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
);
}
@@ -1,17 +1,15 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordApplicationCommandCreateUpdateDelete } from "../../types/interactions/application_command_create_update_delete.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandDelete(data: DiscordGatewayPayload) {
const {
application_id: applicationId,
guild_id: guildId,
...rest
} = data.d as DiscordApplicationCommandCreateUpdateDelete;
eventHandlers.applicationCommandDelete?.({
...rest,
guildId,
applicationId,
});
eventHandlers.applicationCommandDelete?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
);
}
@@ -1,6 +1,10 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordApplicationCommandCreateUpdateDelete } from "../../types/interactions/application_command_create_update_delete.ts";
import {
ApplicationCommandCreateUpdateDelete,
DiscordApplicationCommandCreateUpdateDelete,
} from "../../types/interactions/application_command_create_update_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleApplicationCommandUpdate(data: DiscordGatewayPayload) {
const {
@@ -9,9 +13,9 @@ export function handleApplicationCommandUpdate(data: DiscordGatewayPayload) {
...rest
} = data.d as DiscordApplicationCommandCreateUpdateDelete;
eventHandlers.applicationCommandUpdate?.({
...rest,
guildId,
applicationId,
});
eventHandlers.applicationCommandUpdate?.(
snakeKeysToCamelCase<ApplicationCommandCreateUpdateDelete>(
data.d as DiscordApplicationCommandCreateUpdateDelete,
),
);
}
+3 -3
View File
@@ -1,8 +1,8 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { Collection } from "../../util/collection.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordGuildEmojisUpdate } 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;
@@ -11,7 +11,7 @@ export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) {
const cachedEmojis = guild.emojis;
guild.emojis = new Collection(
payload.emojis.map((emoji) => [emoji.id ?? emoji.name, emoji]),
payload.emojis.map((emoji) => [emoji.id!, emoji]),
);
await cacheHandlers.set("guilds", payload.guild_id, guild);
+2
View File
@@ -30,6 +30,8 @@ export async function handleGuildUpdate(data: DiscordGatewayPayload) {
if (Array.isArray(cachedValue) && Array.isArray(value)) {
const different = (cachedValue.length !== value.length) ||
cachedValue.find((val) => !value.includes(val)) ||
// TODO: check if this really works hehe
// @ts-ignore this works ts is wrong
value.find((val) => !cachedValue.includes(val));
if (!different) return;
}
@@ -9,9 +9,9 @@ import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleIntegrationCreate(
data: DiscordGatewayPayload,
) {
const payload = data.d as DiscordIntegrationCreateUpdate;
eventHandlers.integrationCreate?.(
snakeKeysToCamelCase(payload) as IntegrationCreateUpdate,
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationCreateUpdate,
),
);
}
@@ -1,15 +1,15 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { IntegrationCreateUpdate } from "../../types/integration/integration_create_update.ts";
import {
DiscordIntegrationDelete,
IntegrationDelete,
} from "../../types/integration/integration_delete.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleIntegrationDelete(data: DiscordGatewayPayload) {
const payload = data.d as DiscordIntegrationDelete;
eventHandlers.integrationDelete?.(
snakeKeysToCamelCase(payload) as IntegrationDelete,
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationDelete,
),
);
}
@@ -7,9 +7,9 @@ import {
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleIntegrationUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordIntegrationCreateUpdate;
eventHandlers.integrationUpdate?.(
snakeKeysToCamelCase(payload) as IntegrationCreateUpdate,
snakeKeysToCamelCase<IntegrationCreateUpdate>(
data.d as DiscordIntegrationCreateUpdate,
),
);
}
@@ -1,10 +1,12 @@
// TODO: DM support idk need to discuss how we solve this
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 { Interaction } from "../../types/mod.ts";
export async function handleInteractionCreate(data: DiscordGatewayPayload) {
const payload = data.d as InteractionCommandPayload;
const payload = data.d as Interaction;
const discordenoMember = await structures.createDiscordenoMember(
payload.member,
payload.guild_id,
+3 -3
View File
@@ -7,7 +7,7 @@ import {
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleInviteCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordInviteCreate;
eventHandlers.inviteCreate?.(snakeKeysToCamelCase(payload) as InviteCreate);
eventHandlers.inviteCreate?.(
snakeKeysToCamelCase<InviteCreate>(data.d as DiscordInviteCreate),
);
}
+3 -3
View File
@@ -7,7 +7,7 @@ import {
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleInviteDelete(data: DiscordGatewayPayload) {
const payload = data.d as DiscordInviteDelete;
eventHandlers.inviteDelete?.(snakeKeysToCamelCase(payload) as InviteDelete);
eventHandlers.inviteDelete?.(
snakeKeysToCamelCase<InviteDelete>(data.d as DiscordInviteDelete),
);
}
+1 -1
View File
@@ -35,7 +35,7 @@ export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) {
guild,
discordenoMember,
payload.nick!,
guildMember.nick,
guildMember.nick ?? undefined,
);
}
+6 -11
View File
@@ -2,7 +2,11 @@ import { botId, eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageReactionAdd } from "../../types/messages/message_reaction_add.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;
@@ -41,17 +45,8 @@ export async function handleMessageReactionAdd(data: DiscordGatewayPayload) {
}
}
const uncachedOptions = {
...payload,
id: payload.message_id,
channelId: payload.channel_id,
guildId: payload.guild_id || "",
};
eventHandlers.reactionAdd?.(
uncachedOptions,
payload.emoji,
payload.user_id,
snakeKeysToCamelCase<MessageReactionAdd>(payload),
message,
);
}
@@ -1,7 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageReactionRemove } from "../../types/messages/message_reaction_remove.ts";
import {
DiscordMessageReactionRemove,
MessageReactionRemove,
} from "../../types/messages/message_reaction_remove.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionRemove(
data: DiscordGatewayPayload,
@@ -18,24 +22,17 @@ export async function handleMessageReactionRemove(
if (reaction) {
reaction.count--;
if (reaction.count === 0) message.reactions = message.reactions?.filter(r => r.count !== 0);
if (reaction.count === 0) {
message.reactions = message.reactions?.filter((r) => r.count !== 0);
}
if (!message.reactions?.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
}
}
const uncachedOptions = {
...payload,
id: payload.message_id,
channelId: payload.channel_id,
guildId: payload.guild_id,
};
eventHandlers.reactionRemove?.(
uncachedOptions,
payload.emoji,
payload.user_id,
snakeKeysToCamelCase<MessageReactionRemove>(payload),
message,
);
}
@@ -1,7 +1,11 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordMessageReactionRemoveAll } from "../../types/messages/message_reaction_remove_all.ts";
import {
DiscordMessageReactionRemoveAll,
MessageReactionRemoveAll,
} from "../../types/messages/message_reaction_remove_all.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleMessageReactionRemoveAll(
data: DiscordGatewayPayload,
@@ -15,5 +19,8 @@ export async function handleMessageReactionRemoveAll(
await cacheHandlers.set("messages", payload.message_id, message);
}
eventHandlers.reactionRemoveAll?.(payload);
eventHandlers.reactionRemoveAll?.(
snakeKeysToCamelCase<MessageReactionRemoveAll>(payload),
message,
);
}
@@ -18,13 +18,16 @@ export async function handleMessageReactionRemoveEmoji(
reaction.emoji.name === payload.emoji.name
),
);
if (!message.reactions.length) message.reactions = undefined
if (!message.reactions.length) message.reactions = undefined;
await cacheHandlers.set("messages", payload.message_id, message);
}
eventHandlers.reactionRemoveEmoji?.(
data.d,
payload.emoji,
payload.message_id,
payload.channel_id,
payload.guild_id,
);
}
+3 -12
View File
@@ -9,22 +9,13 @@ export async function handleMessageUpdate(data: DiscordGatewayPayload) {
const channel = await cacheHandlers.get("channels", payload.channel_id);
if (!channel) return;
const cachedMessage = await cacheHandlers.get("messages", payload.id);
if (!cachedMessage) return;
const oldMessage = {
attachments: cachedMessage.attachments,
content: cachedMessage.content,
embeds: cachedMessage.embeds,
editedTimestamp: cachedMessage.editedTimestamp,
tts: cachedMessage.tts,
pinned: cachedMessage.pinned,
};
const oldMessage = await cacheHandlers.get("messages", payload.id);
if (!oldMessage) return;
// Messages with embeds can trigger update but they wont have edited_timestamp
if (
!payload.edited_timestamp ||
(cachedMessage.content === payload.content)
(oldMessage.content === payload.content)
) {
return;
}
+8 -2
View File
@@ -1,10 +1,16 @@
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordPresenceUpdate } from "../../types/misc/presence_update.ts";
import {
DiscordPresenceUpdate,
PresenceUpdate,
} from "../../types/misc/presence_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handlePresenceUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordPresenceUpdate;
const payload = snakeKeysToCamelCase<PresenceUpdate>(
data.d as DiscordPresenceUpdate,
);
const oldPresence = await cacheHandlers.get("presences", payload.user.id);
await cacheHandlers.set("presences", payload.user.id, payload);
+3 -2
View File
@@ -4,7 +4,8 @@ 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 { delay, snakeKeysToCamelCase } from "../../util/utils.ts";
import { DiscordGuildMemberWithUser } from "../../types/mod.ts";
import { camelKeysToSnakeCase, delay } from "../../util/utils.ts";
import { ws } from "../../ws/ws.ts";
export async function handleReady(
@@ -103,7 +104,7 @@ async function loaded(shardId: number) {
await Promise.allSettled(
members.map(async (member) => {
const discordenoMember = await structures.createDiscordenoMember(
snakeKeysToCamelCase(member),
camelKeysToSnakeCase<DiscordGuildMemberWithUser>(member),
guildId,
);
+8 -2
View File
@@ -1,7 +1,13 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts";
import { DiscordTypingStart } from "../../types/misc/typing_start.ts";
import {
DiscordTypingStart,
TypingStart,
} from "../../types/misc/typing_start.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export function handleTypingStart(data: DiscordGatewayPayload) {
eventHandlers.typingStart?.(data.d as DiscordTypingStart);
eventHandlers.typingStart?.(
snakeKeysToCamelCase<TypingStart>(data.d as DiscordTypingStart),
);
}
+2 -1
View File
@@ -2,9 +2,10 @@ 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";
export async function handleGuildRoleCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildRoleCreateUpdate;
const payload = data.d as DiscordGuildRoleCreate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (!guild) return;
+2 -1
View File
@@ -2,9 +2,10 @@ 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";
export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordGuildRoleCreateUpdate;
const payload = data.d as DiscordGuildRoleUpdate;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
if (!guild) return;
+7 -2
View File
@@ -1,11 +1,16 @@
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 {
DiscordVoiceServerUpdate,
VoiceServerUpdate,
} from "../../types/voice/voice_server_update.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
export async function handleVoiceServerUpdate(data: DiscordGatewayPayload) {
const payload = snakeKeysToCamelCase(data.d as DiscordVoiceServerUpdate) as VoiceServerUpdate;
const payload = snakeKeysToCamelCase<VoiceServerUpdate>(
data.d as DiscordVoiceServerUpdate,
);
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
+30 -23
View File
@@ -2,51 +2,58 @@ 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 { DiscordVoiceState } from "../../types/voice/voice_state.ts";
import { DiscordGuildMemberWithUser } from "../../types/mod.ts";
import {
DiscordVoiceState,
VoiceState,
} from "../../types/voice/voice_state.ts";
import {
camelKeysToSnakeCase,
snakeKeysToCamelCase,
} from "../../util/utils.ts";
export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordVoiceState;
if (!payload.guild_id) return;
const payload = snakeKeysToCamelCase<VoiceState>(
data.d as DiscordVoiceState,
);
if (!payload.guildId) return;
const guild = await cacheHandlers.get("guilds", payload.guild_id);
const guild = await cacheHandlers.get("guilds", payload.guildId);
if (!guild) return;
const member = payload.member
? await structures.createDiscordenoMember(payload.member, guild.id)
: await cacheHandlers.get("members", payload.user_id);
? await structures.createDiscordenoMember(
camelKeysToSnakeCase<DiscordGuildMemberWithUser>(payload),
guild.id,
)
: await cacheHandlers.get("members", payload.userId);
if (!member) return;
// No cached state before so lets make one for em
const cachedState = guild.voiceStates.get(payload.user_id);
const cachedState = guild.voiceStates.get(payload.userId);
guild.voiceStates.set(payload.user_id, {
...payload,
guildId: payload.guild_id,
channelId: payload.channel_id || "",
userId: payload.user_id,
sessionId: payload.session_id,
selfDeaf: payload.self_deaf,
selfMute: payload.self_mute,
selfStream: payload.self_stream || false,
});
guild.voiceStates.set(
payload.userId,
payload,
);
await cacheHandlers.set("guilds", payload.guild_id, guild);
await cacheHandlers.set("guilds", payload.guildId, guild);
if (cachedState?.channelId !== payload.channel_id) {
if (cachedState?.channelId !== payload.channelId) {
// Either joined or moved channels
if (payload.channel_id) {
if (payload.channelId) {
if (cachedState?.channelId) { // Was in a channel before
eventHandlers.voiceChannelSwitch?.(
member,
payload.channel_id,
payload.channelId,
cachedState.channelId,
);
} else { // Was not in a channel before so user just joined
eventHandlers.voiceChannelJoin?.(member, payload.channel_id);
eventHandlers.voiceChannelJoin?.(member, payload.channelId);
}
} // Left the channel
else if (cachedState?.channelId) {
guild.voiceStates.delete(payload.user_id);
guild.voiceStates.delete(payload.userId);
eventHandlers.voiceChannelLeave?.(member, cachedState.channelId);
}
}