diff --git a/src/handlers/channels/CHANNEL_CREATE.ts b/src/handlers/channels/CHANNEL_CREATE.ts index e2ac843c6..d0b97d037 100644 --- a/src/handlers/channels/CHANNEL_CREATE.ts +++ b/src/handlers/channels/CHANNEL_CREATE.ts @@ -1,9 +1,10 @@ import { eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export async function handleChannelCreate(data: DiscordPayload) { - const payload = data.d as ChannelCreatePayload; +export async function handleChannelCreate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordChannel; const channelStruct = await structures.createChannelStruct(payload); await cacheHandlers.set("channels", channelStruct.id, channelStruct); diff --git a/src/handlers/channels/CHANNEL_DELETE.ts b/src/handlers/channels/CHANNEL_DELETE.ts index 3108c9082..2e839f647 100644 --- a/src/handlers/channels/CHANNEL_DELETE.ts +++ b/src/handlers/channels/CHANNEL_DELETE.ts @@ -1,8 +1,9 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export async function handleChannelDelete(data: DiscordPayload) { - const payload = data.d as ChannelCreatePayload; +export async function handleChannelDelete(data: DiscordGatewayPayload) { + const payload = data.d as DiscordChannel; const cachedChannel = await cacheHandlers.get("channels", payload.id); if (!cachedChannel) return; diff --git a/src/handlers/channels/CHANNEL_PINS_UPDATE.ts b/src/handlers/channels/CHANNEL_PINS_UPDATE.ts index 6d973e920..58a6c2e7e 100644 --- a/src/handlers/channels/CHANNEL_PINS_UPDATE.ts +++ b/src/handlers/channels/CHANNEL_PINS_UPDATE.ts @@ -1,8 +1,12 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordChannelPinsUpdate, + DiscordGatewayPayload, +} from "../../types/gateway.ts"; -export async function handleChannelPinsUpdate(data: DiscordPayload) { - const payload = data.d as DiscordChannelPinsUpdateEvent; +export async function handleChannelPinsUpdate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordChannelPinsUpdate; const channel = await cacheHandlers.get("channels", payload.channel_id); if (!channel) return; diff --git a/src/handlers/channels/CHANNEL_UPDATE.ts b/src/handlers/channels/CHANNEL_UPDATE.ts index e83c89b87..fd60c7396 100644 --- a/src/handlers/channels/CHANNEL_UPDATE.ts +++ b/src/handlers/channels/CHANNEL_UPDATE.ts @@ -1,9 +1,10 @@ import { eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export async function handleChannelUpdate(data: DiscordPayload) { - const payload = data.d as ChannelCreatePayload; +export async function handleChannelUpdate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordChannel; const cachedChannel = await cacheHandlers.get("channels", payload.id); const channelStruct = await structures.createChannelStruct(payload); diff --git a/src/handlers/emojis/GUILD_EMOJIS_UPDATE.ts b/src/handlers/emojis/GUILD_EMOJIS_UPDATE.ts index c9d7495b7..6ef317c47 100644 --- a/src/handlers/emojis/GUILD_EMOJIS_UPDATE.ts +++ b/src/handlers/emojis/GUILD_EMOJIS_UPDATE.ts @@ -1,9 +1,13 @@ import { eventHandlers } from "../../bot.ts"; -import { Collection } from "../../util/collection.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordGuildEmojisUpdate, +} from "../../types/gateway.ts"; +import { Collection } from "../../util/collection.ts"; -export async function handleGuildEmojisUpdate(data: DiscordPayload) { - const payload = data.d as GuildEmojisUpdatePayload; +export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildEmojisUpdate; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/guilds/GUILD_BAN_ADD.ts b/src/handlers/guilds/GUILD_BAN_ADD.ts index 003d2fefd..9204aaafe 100644 --- a/src/handlers/guilds/GUILD_BAN_ADD.ts +++ b/src/handlers/guilds/GUILD_BAN_ADD.ts @@ -1,8 +1,12 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordGuildBanAddRemove, +} from "../../types/gateway.ts"; -export async function handleGuildBanAdd(data: DiscordPayload) { - const payload = data.d as GuildBanPayload; +export async function handleGuildBanAdd(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildBanAddRemove; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/guilds/GUILD_BAN_REMOVE.ts b/src/handlers/guilds/GUILD_BAN_REMOVE.ts index 2985e1dfc..93e3bf519 100644 --- a/src/handlers/guilds/GUILD_BAN_REMOVE.ts +++ b/src/handlers/guilds/GUILD_BAN_REMOVE.ts @@ -1,8 +1,12 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordGuildBanAddRemove, +} from "../../types/gateway.ts"; -export async function handleGuildBanRemove(data: DiscordPayload) { - const payload = data.d as GuildBanPayload; +export async function handleGuildBanRemove(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildBanAddRemove; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/guilds/GUILD_CREATE.ts b/src/handlers/guilds/GUILD_CREATE.ts index 6bd50c2e2..3053b4a4e 100644 --- a/src/handlers/guilds/GUILD_CREATE.ts +++ b/src/handlers/guilds/GUILD_CREATE.ts @@ -1,18 +1,19 @@ import { eventHandlers } from "../../bot.ts"; import { cache, cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; import { basicShards } from "../../ws/shard.ts"; export async function handleGuildCreate( - data: DiscordPayload, + data: DiscordGatewayPayload, shardID: number, ) { - const payload = data.d as CreateGuildPayload; + const payload = data.d as DiscordGuild; // When shards resume they emit GUILD_CREATE again. if (await cacheHandlers.has("guilds", payload.id)) return; const guildStruct = await structures.createGuildStruct( - data.d as CreateGuildPayload, + data.d, shardID, ); await cacheHandlers.set("guilds", guildStruct.id, guildStruct); diff --git a/src/handlers/guilds/GUILD_DELETE.ts b/src/handlers/guilds/GUILD_DELETE.ts index f8c725579..0d3c8a0c9 100644 --- a/src/handlers/guilds/GUILD_DELETE.ts +++ b/src/handlers/guilds/GUILD_DELETE.ts @@ -1,12 +1,13 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; import { basicShards } from "../../ws/shard.ts"; export async function handleGuildDelete( - data: DiscordPayload, + data: DiscordGatewayPayload, shardID: number, ) { - const payload = data.d as GuildDeletePayload; + const payload = data.d as DiscordUnavailableGuild; const guild = await cacheHandlers.get("guilds", payload.id); if (!guild) return; diff --git a/src/handlers/guilds/GUILD_INTEGRATIONS_UPDATE.ts b/src/handlers/guilds/GUILD_INTEGRATIONS_UPDATE.ts index 5efcf87a3..d6c82560c 100644 --- a/src/handlers/guilds/GUILD_INTEGRATIONS_UPDATE.ts +++ b/src/handlers/guilds/GUILD_INTEGRATIONS_UPDATE.ts @@ -1,10 +1,14 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordGuildIntegrationsUpdate, +} from "../../types/gateway.ts"; export async function handleGuildIntegrationsUpdate( - data: DiscordPayload, + data: DiscordGatewayPayload, ) { - const payload = data.d as DiscordGuildIntegrationsUpdateEvent; + const payload = data.d as DiscordGuildIntegrationsUpdate; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/guilds/GUILD_UPDATE.ts b/src/handlers/guilds/GUILD_UPDATE.ts index dea715910..b8602c316 100644 --- a/src/handlers/guilds/GUILD_UPDATE.ts +++ b/src/handlers/guilds/GUILD_UPDATE.ts @@ -1,8 +1,9 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export async function handleGuildUpdate(data: DiscordPayload) { - const payload = data.d as UpdateGuildPayload; +export async function handleGuildUpdate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuild; const cachedGuild = await cacheHandlers.get("guilds", payload.id); if (!cachedGuild) return; diff --git a/src/handlers/members/GUILD_MEMBERS_CHUNK.ts b/src/handlers/members/GUILD_MEMBERS_CHUNK.ts index 75f917737..fbf35ac3f 100644 --- a/src/handlers/members/GUILD_MEMBERS_CHUNK.ts +++ b/src/handlers/members/GUILD_MEMBERS_CHUNK.ts @@ -1,9 +1,13 @@ import { cache, cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; +import { + DiscordGatewayPayload, + DiscordGuildMembersChunk, +} from "../../types/gateway.ts"; import { Collection } from "../../util/collection.ts"; -export async function handleGuildMembersChunk(data: DiscordPayload) { - const payload = data.d as GuildMemberChunkPayload; +export async function handleGuildMembersChunk(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildMembersChunk; const members = await Promise.all( payload.members.map(async (member) => { diff --git a/src/handlers/members/GUILD_MEMBER_ADD.ts b/src/handlers/members/GUILD_MEMBER_ADD.ts index ac2511de5..edf44f805 100644 --- a/src/handlers/members/GUILD_MEMBER_ADD.ts +++ b/src/handlers/members/GUILD_MEMBER_ADD.ts @@ -1,9 +1,13 @@ import { eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { + DiscordGatewayPayload, + DiscordGuildMemberAdd, +} from "../../types/gateway.ts"; -export async function handleGuildMemberAdd(data: DiscordPayload) { - const payload = data.d as GuildMemberAddPayload; +export async function handleGuildMemberAdd(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildMemberAdd; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/members/GUILD_MEMBER_REMOVE.ts b/src/handlers/members/GUILD_MEMBER_REMOVE.ts index 2ffa60e74..638413207 100644 --- a/src/handlers/members/GUILD_MEMBER_REMOVE.ts +++ b/src/handlers/members/GUILD_MEMBER_REMOVE.ts @@ -1,8 +1,12 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordGuildMemberRemove, +} from "../../types/gateway.ts"; -export async function handleGuildMemberRemove(data: DiscordPayload) { - const payload = data.d as GuildBanPayload; +export async function handleGuildMemberRemove(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildMemberRemove; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/members/GUILD_MEMBER_UPDATE.ts b/src/handlers/members/GUILD_MEMBER_UPDATE.ts index 75b081c4d..9655dce09 100644 --- a/src/handlers/members/GUILD_MEMBER_UPDATE.ts +++ b/src/handlers/members/GUILD_MEMBER_UPDATE.ts @@ -1,9 +1,13 @@ import { eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { + DiscordGatewayPayload, + DiscordGuildMemberUpdate, +} from "../../types/gateway.ts"; -export async function handleGuildMemberUpdate(data: DiscordPayload) { - const payload = data.d as GuildMemberUpdatePayload; +export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildMemberUpdate; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; @@ -31,7 +35,7 @@ export async function handleGuildMemberUpdate(data: DiscordPayload) { eventHandlers.nicknameUpdate?.( guild, memberStruct, - payload.nick, + payload.nick!, guildMember?.nick, ); } diff --git a/src/handlers/messages/MESSAGE_CREATE.ts b/src/handlers/messages/MESSAGE_CREATE.ts index 361c93458..634c2cd3f 100644 --- a/src/handlers/messages/MESSAGE_CREATE.ts +++ b/src/handlers/messages/MESSAGE_CREATE.ts @@ -1,9 +1,10 @@ import { eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export async function handleMessageCreate(data: DiscordPayload) { - const payload = data.d as MessageCreateOptions; +export async function handleMessageCreate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordMessage; const channel = await cacheHandlers.get("channels", payload.channel_id); if (channel) channel.lastMessageID = payload.id; diff --git a/src/handlers/messages/MESSAGE_DELETE.ts b/src/handlers/messages/MESSAGE_DELETE.ts index 4830947e2..f926bcf0a 100644 --- a/src/handlers/messages/MESSAGE_DELETE.ts +++ b/src/handlers/messages/MESSAGE_DELETE.ts @@ -1,8 +1,12 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordMessageDelete, +} from "../../types/gateway.ts"; -export async function handleMessageDelete(data: DiscordPayload) { - const payload = data.d as MessageDeletePayload; +export async function handleMessageDelete(data: DiscordGatewayPayload) { + const payload = data.d as DiscordMessageDelete; const channel = await cacheHandlers.get("channels", payload.channel_id); if (!channel) return; diff --git a/src/handlers/messages/MESSAGE_DELETE_BULK.ts b/src/handlers/messages/MESSAGE_DELETE_BULK.ts index 615b65270..b6a89aa29 100644 --- a/src/handlers/messages/MESSAGE_DELETE_BULK.ts +++ b/src/handlers/messages/MESSAGE_DELETE_BULK.ts @@ -1,8 +1,12 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordMessageDeleteBulk, +} from "../../types/gateway.ts"; -export async function handleMessageDeleteBulk(data: DiscordPayload) { - const payload = data.d as MessageDeleteBulkPayload; +export async function handleMessageDeleteBulk(data: DiscordGatewayPayload) { + const payload = data.d as DiscordMessageDeleteBulk; const channel = await cacheHandlers.get("channels", payload.channel_id); if (!channel) return; diff --git a/src/handlers/messages/MESSAGE_REACTION_ADD.ts b/src/handlers/messages/MESSAGE_REACTION_ADD.ts index 68428499b..af7df860b 100644 --- a/src/handlers/messages/MESSAGE_REACTION_ADD.ts +++ b/src/handlers/messages/MESSAGE_REACTION_ADD.ts @@ -1,9 +1,13 @@ import { botID, eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { + DiscordGatewayPayload, + DiscordMessageReactionAdd, +} from "../../types/gateway.ts"; -export async function handleMessageReactionAdd(data: DiscordPayload) { - const payload = data.d as MessageReactionPayload; +export async function handleMessageReactionAdd(data: DiscordGatewayPayload) { + const payload = data.d as DiscordMessageReactionAdd; const message = await cacheHandlers.get("messages", payload.message_id); if (message) { diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts index 8c7f13330..c637d6575 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts @@ -1,11 +1,15 @@ import { botID, eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { + DiscordGatewayPayload, + DiscordMessageReactionRemove, +} from "../../types/gateway.ts"; export async function handleMessageReactionRemove( - data: DiscordPayload, + data: DiscordGatewayPayload, ) { - const payload = data.d as MessageReactionPayload; + const payload = data.d as DiscordMessageReactionRemove; const message = await cacheHandlers.get("messages", payload.message_id); if (message) { diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts index 67d599654..06c4646e0 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts @@ -1,10 +1,14 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordMessageReactionRemoveAll, +} from "../../types/gateway.ts"; export async function handleMessageReactionRemoveAll( - data: DiscordPayload, + data: DiscordGatewayPayload, ) { - const payload = data.d as BaseMessageReactionPayload; + const payload = data.d as DiscordMessageReactionRemoveAll; const message = await cacheHandlers.get("messages", payload.message_id); if (message?.reactions) { @@ -13,5 +17,5 @@ export async function handleMessageReactionRemoveAll( await cacheHandlers.set("messages", payload.message_id, message); } - eventHandlers.reactionRemoveAll?.(data.d as BaseMessageReactionPayload); + eventHandlers.reactionRemoveAll?.(data.d); } diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts index 3672a02c6..8dc6a596c 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts @@ -1,10 +1,14 @@ -import { botID, eventHandlers } from "../../bot.ts"; +import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordMessageReactionRemoveEmoji, +} from "../../types/gateway.ts"; export async function handleMessageReactionRemoveEmoji( - data: DiscordPayload, + data: DiscordGatewayPayload, ) { - const payload = data.d as MessageReactionRemoveEmojiPayload; + const payload = data.d as DiscordMessageReactionRemoveEmoji; const message = await cacheHandlers.get("messages", payload.message_id); if (message?.reactions) { @@ -20,6 +24,6 @@ export async function handleMessageReactionRemoveEmoji( } eventHandlers.reactionRemoveEmoji?.( - data.d as MessageReactionRemoveEmojiPayload, + data.d, ); } diff --git a/src/handlers/messages/MESSAGE_UPDATE.ts b/src/handlers/messages/MESSAGE_UPDATE.ts index 5c987ef90..a3b3f8a3b 100644 --- a/src/handlers/messages/MESSAGE_UPDATE.ts +++ b/src/handlers/messages/MESSAGE_UPDATE.ts @@ -1,9 +1,10 @@ import { eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export async function handleMessageUpdate(data: DiscordPayload) { - const payload = data.d as MessageCreateOptions; +export async function handleMessageUpdate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordMessage; const channel = await cacheHandlers.get("channels", payload.channel_id); if (!channel) return; diff --git a/src/handlers/misc/PRESENCE_UPDATE.ts b/src/handlers/misc/PRESENCE_UPDATE.ts index 5ff3e5bd2..6a980e6b6 100644 --- a/src/handlers/misc/PRESENCE_UPDATE.ts +++ b/src/handlers/misc/PRESENCE_UPDATE.ts @@ -1,7 +1,8 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export async function handlePresenceUpdate(data: DiscordPayload) { +export async function handlePresenceUpdate(data: DiscordGatewayPayload) { const payload = data.d as PresenceUpdatePayload; const oldPresence = await cacheHandlers.get("presences", payload.user.id); await cacheHandlers.set("presences", payload.user.id, payload); diff --git a/src/handlers/misc/READY.ts b/src/handlers/misc/READY.ts index 5c391694e..24564aeff 100644 --- a/src/handlers/misc/READY.ts +++ b/src/handlers/misc/READY.ts @@ -7,17 +7,18 @@ import { import { cache, cacheHandlers } from "../../cache.ts"; import { initialMemberLoadQueue } from "../../structures/guild.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload, DiscordReady } from "../../types/gateway.ts"; import { delay } from "../../util/utils.ts"; import { allowNextShard, basicShards } from "../../ws/mod.ts"; export async function handleReady( - data: DiscordPayload, + data: DiscordGatewayPayload, shardID: number, ) { // The bot has already started, the last shard is resumed, however. if (cache.isReady) return; - const payload = data.d as ReadyPayload; + const payload = data.d as DiscordReady; setBotID(payload.user.id); setApplicationID(payload.application.id); @@ -44,7 +45,7 @@ export async 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 */ -function checkReady(payload: ReadyPayload, shardID: number, now: number) { +function checkReady(payload: DiscordReady, shardID: number, now: number) { const shard = basicShards.get(shardID); if (!shard) return; diff --git a/src/handlers/roles/GUILD_ROLE_CREATE.ts b/src/handlers/roles/GUILD_ROLE_CREATE.ts index a5dbcdaf0..c3105d6cc 100644 --- a/src/handlers/roles/GUILD_ROLE_CREATE.ts +++ b/src/handlers/roles/GUILD_ROLE_CREATE.ts @@ -1,9 +1,13 @@ import { eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { + DiscordGatewayPayload, + DiscordGuildRoleCreateUpdate, +} from "../../types/gateway.ts"; -export async function handleGuildRoleCreate(data: DiscordPayload) { - const payload = data.d as GuildRolePayload; +export async function handleGuildRoleCreate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildRoleCreateUpdate; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/roles/GUILD_ROLE_DELETE.ts b/src/handlers/roles/GUILD_ROLE_DELETE.ts index 9c53af655..0eb6c179b 100644 --- a/src/handlers/roles/GUILD_ROLE_DELETE.ts +++ b/src/handlers/roles/GUILD_ROLE_DELETE.ts @@ -1,8 +1,12 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { + DiscordGatewayPayload, + DiscordGuildRoleDelete, +} from "../../types/gateway.ts"; -export async function handleGuildRoleDelete(data: DiscordPayload) { - const payload = data.d as GuildRoleDeletePayload; +export async function handleGuildRoleDelete(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildRoleDelete; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/roles/GUILD_ROLE_UPDATE.ts b/src/handlers/roles/GUILD_ROLE_UPDATE.ts index 2e9f0426e..bd9856ca8 100644 --- a/src/handlers/roles/GUILD_ROLE_UPDATE.ts +++ b/src/handlers/roles/GUILD_ROLE_UPDATE.ts @@ -1,9 +1,13 @@ import { eventHandlers } from "../../bot.ts"; -import { structures } from "../../structures/mod.ts"; import { cacheHandlers } from "../../cache.ts"; +import { structures } from "../../structures/mod.ts"; +import { + DiscordGatewayPayload, + DiscordGuildRoleCreateUpdate, +} from "../../types/gateway.ts"; -export async function handleGuildRoleUpdate(data: DiscordPayload) { - const payload = data.d as GuildRolePayload; +export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordGuildRoleCreateUpdate; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/types/gateway.ts b/src/types/gateway.ts index d0d46a826..e9b4f7979 100644 --- a/src/types/gateway.ts +++ b/src/types/gateway.ts @@ -228,7 +228,7 @@ export interface DiscordGuildBanAddRemove { } /** https://discord.com/developers/docs/topics/gateway#guild-emojis-update */ -export interface GuildEmojisUpdate { +export interface DiscordGuildEmojisUpdate { /** id of the guild */ guild_id: string; /** Array of emojis */ @@ -236,7 +236,7 @@ export interface GuildEmojisUpdate { } /** https://discord.com/developers/docs/topics/gateway#guild-integrations-update */ -export interface DiscordIntegrationsUpdate { +export interface DiscordGuildIntegrationsUpdate { /** id of the guild whose integrations were updated */ guild_id: string; } @@ -247,6 +247,156 @@ export interface DiscordGuildMemberAdd extends DiscordMember { guild_id: string; } +/** https://discord.com/developers/docs/topics/gateway#guild-member-remove */ +export interface DiscordGuildMemberRemove { + /** The id of the guild */ + guild_id: string; + /** The user who was removed */ + user: DiscordUser; +} + +/** https://discord.com/developers/docs/topics/gateway#guild-member-update */ +export interface DiscordGuildMemberUpdate { + /** The id of the guild */ + guild_id: string; + /** User role ids */ + roles: string[]; + /** The user */ + user: DiscordUser; + /** Nickname of the user in the guild */ + nick?: string | null; + /** When the user joined the guild */ + joined_at: string; + /** When the user starting boosting the guild */ + premium_since?: string | null; + /** Whether the user has not yet passed the guild's Membership Screening requirements */ + pending?: boolean; +} + +/** https://discord.com/developers/docs/topics/gateway#guild-members-chunk */ +export interface DiscordGuildMembersChunk { + /** The id of the guild */ + guild_id: string; + /** Set of guild members */ + members: DiscordMember[]; + /** The chunk index in the expected chunks for this response (0 <= chunk_index < chunk_count) */ + chunk_index: number; + /** The total number of expected chunks for this response */ + chunk_count: number; + /** If passing an invalid id to `REQUEST_GUILD_MEMBERS`, it will be returned here */ + not_found?: string[]; + /** If passing true to `REQUEST_GUILD_MEMBERS`, presences of the returned members will be here */ + presences?: DiscordPresence[]; + /** The nonce used in the Guild Members Request */ + nonce?: string; +} + +/** https://discord.com/developers/docs/topics/gateway#guild-role-create */ +export interface DiscordGuildRoleCreateUpdate { + /** The id of the guild */ + guild_id: string; + /** The role created/updated */ + role: DiscordRole; +} + +/** https://discord.com/developers/docs/topics/gateway#guild-role-delete */ +export interface DiscordGuildRoleDelete { + /** id of the guild */ + guild_id: string; + /** id of the role */ + role_id: string; +} + +/** https://discord.com/developers/docs/topics/gateway#invite-create */ +export interface DiscordInviteCreate { + /** The channel the invite is for */ + channel_id: string; + /** The unique invite code */ + code: string; + /** The time at which the invite was created */ + created_at: string; + /** The guild of the invite */ + guild_id?: string; + /** The user that created the invite */ + inviter?: DiscordUser; + /** How long the invite is valid for (in seconds) */ + max_age: number; + /** The maximum number of times the invite can be used */ + max_uses: number; + /** The target user for this invite */ + target_user?: Partial; + /** The type of user target for this invite */ + target_user_type?: number; + /** Whether or not the invite is temporary (invited users will be kicked on disconnect unless they're assigned a role) */ + temporary: boolean; + /** How many times the invite has been used (always will be 0) */ + uses: number; +} + +/** https://discord.com/developers/docs/topics/gateway#invite-delete */ +export interface DiscordInviteDelete { + /** The channel of the invite */ + channel_id: string; + /** The guild of the invite */ + guild_id?: string; + /** The unique invite code */ + code: string; +} + +/** https://discord.com/developers/docs/topics/gateway#message-delete */ +export interface DiscordMessageDelete { + /** The id of the message */ + id: string; + /** The id of the channel */ + channel_id: string; + /** The id of the guild */ + guild_id?: string; +} + +/** https://discord.com/developers/docs/topics/gateway#message-delete-bulk */ +export interface DiscordMessageDeleteBulk { + /** The ids of the messages */ + ids: string[]; + /** The id of the channel */ + channel_id: string; + /** The id of the guild */ + guild_id?: string; +} + +/** https://discord.com/developers/docs/topics/gateway#message-reaction-add */ +export interface DiscordMessageReactionAdd { + /** The id of the user */ + user_id: string; + /** The id of the channel */ + channel_id: string; + /** The id of the message */ + message_id: string; + /** The id of the guild */ + guild_id?: string; + /** The member who reacted if this happened in a guild */ + member?: DiscordMember; + /** The emoji used to react */ + emoji: Partial; +} + +/** https://discord.com/developers/docs/topics/gateway#message-reaction-remove */ +export type DiscordMessageReactionRemove = Omit< + DiscordMessageReactionAdd, + "member" +>; + +/** https://discord.com/developers/docs/topics/gateway#message-reaction-remove-all */ +export type DiscordMessageReactionRemoveAll = Pick< + DiscordMessageReactionAdd, + "channel_id" | "message_id" | "guild_id" +>; + +/** https://discord.com/developers/docs/topics/gateway#message-reaction-remove-emoji */ +export type DiscordMessageReactionRemoveEmoji = Pick< + DiscordMessageReactionAdd, + "channel_id" | "guild_id" | "message_id" | "emoji" +>; + /** https://discord.com/developers/docs/topics/gateway#get-gateway-bot */ export interface DiscordGetGatewayBot { /** The WSS URL that can be used for connecting to the gateway */ diff --git a/src/ws/shard.ts b/src/ws/shard.ts index 79a45a465..588920460 100644 --- a/src/ws/shard.ts +++ b/src/ws/shard.ts @@ -1,4 +1,10 @@ import { botGatewayData, eventHandlers, proxyWSURL } from "../bot.ts"; +import { + DiscordGatewayPayload, + DiscordGetGatewayBot, + DiscordHello, + DiscordIdentify, +} from "../types/gateway.ts"; import { Collection } from "../util/collection.ts"; import { delay } from "../util/utils.ts"; import { decompressWith } from "./deps.ts"; @@ -70,7 +76,7 @@ export function createShard( if (!heartbeating.has(basicShard.id)) { await heartbeat( basicShard, - (messageData.d as DiscordHeartbeatPayload).heartbeat_interval, + (messageData.d as DiscordHello).heartbeat_interval, identifyPayload, data, ); @@ -180,7 +186,7 @@ async function heartbeat( shard: BasicShard, interval: number, payload: DiscordIdentify, - data: DiscordBotGatewayData, + data: DiscordGetGatewayBot, ) { // We lost socket connection between heartbeats, resume connection if (shard.ws.readyState === WebSocket.CLOSED) { @@ -231,7 +237,7 @@ async function heartbeat( } async function resumeConnection( - data: DiscordBotGatewayData, + data: DiscordGetGatewayBot, payload: DiscordIdentify, shardID: number, ) { @@ -361,7 +367,7 @@ async function processGatewayQueue() { } /** Enqueues the specified data to be transmitted to the server over the WebSocket connection, */ -export function sendWS(payload: DiscordPayload, shardID = 0) { +export function sendWS(payload: DiscordGatewayPayload, shardID = 0) { const shard = basicShards.get(shardID); if (!shard) return false;