diff --git a/src/bot.ts b/src/bot.ts index 9875d7bc4..64b58d3c8 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,5 +1,6 @@ import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts"; import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts"; +import { DiscordGetGatewayBot } from "./types/gateway/get_gateway_bot.ts"; import { baseEndpoints, GATEWAY_VERSION } from "./util/constants.ts"; import { spawnShards } from "./ws/shard_manager.ts"; @@ -14,7 +15,7 @@ export let botGatewayData: DiscordGetGatewayBot; export let proxyWSURL = `wss://gateway.discord.gg`; export let lastShardId = 0; -export const identifyPayload: DiscordIdentify = { +export const identifyPayload = { token: "", compress: true, properties: { diff --git a/src/handlers/channels/CHANNEL_CREATE.ts b/src/handlers/channels/CHANNEL_CREATE.ts index 06aa2c561..69a3642ad 100644 --- a/src/handlers/channels/CHANNEL_CREATE.ts +++ b/src/handlers/channels/CHANNEL_CREATE.ts @@ -1,7 +1,8 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; -import { DiscordChannel, DiscordGatewayPayload } from "../../types/mod.ts"; +import { DiscordChannel } 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; diff --git a/src/handlers/channels/CHANNEL_DELETE.ts b/src/handlers/channels/CHANNEL_DELETE.ts index 2a7f110eb..004863506 100644 --- a/src/handlers/channels/CHANNEL_DELETE.ts +++ b/src/handlers/channels/CHANNEL_DELETE.ts @@ -1,10 +1,8 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; -import { - DiscordChannel, - DiscordChannelTypes, - DiscordGatewayPayload, -} from "../../types/mod.ts"; +import { DiscordChannel } 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; diff --git a/src/handlers/channels/CHANNEL_PINS_UPDATE.ts b/src/handlers/channels/CHANNEL_PINS_UPDATE.ts index 29ddb3a70..85234d64c 100644 --- a/src/handlers/channels/CHANNEL_PINS_UPDATE.ts +++ b/src/handlers/channels/CHANNEL_PINS_UPDATE.ts @@ -1,9 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; -import { - DiscordChannelPinsUpdate, - DiscordGatewayPayload, -} from "../../types/mod.ts"; +import { DiscordChannelPinsUpdate } 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; diff --git a/src/handlers/channels/CHANNEL_UPDATE.ts b/src/handlers/channels/CHANNEL_UPDATE.ts index cb7658a6d..736e297f6 100644 --- a/src/handlers/channels/CHANNEL_UPDATE.ts +++ b/src/handlers/channels/CHANNEL_UPDATE.ts @@ -1,7 +1,8 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; -import { DiscordChannel, DiscordGatewayPayload } from "../../types/mod.ts"; +import { DiscordChannel } 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; diff --git a/src/handlers/commands/APPLICATION_COMMAND_CREATE.ts b/src/handlers/commands/APPLICATION_COMMAND_CREATE.ts index 08acf2cb1..7b936b250 100644 --- a/src/handlers/commands/APPLICATION_COMMAND_CREATE.ts +++ b/src/handlers/commands/APPLICATION_COMMAND_CREATE.ts @@ -1,4 +1,5 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export function handleApplicationCommandCreate( data: DiscordGatewayPayload, diff --git a/src/handlers/commands/APPLICATION_COMMAND_DELETE.ts b/src/handlers/commands/APPLICATION_COMMAND_DELETE.ts index 92a5bf25c..69b3f144d 100644 --- a/src/handlers/commands/APPLICATION_COMMAND_DELETE.ts +++ b/src/handlers/commands/APPLICATION_COMMAND_DELETE.ts @@ -1,4 +1,5 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export function handleApplicationCommandDelete(data: DiscordGatewayPayload) { const { diff --git a/src/handlers/commands/APPLICATION_COMMAND_UPDATE.ts b/src/handlers/commands/APPLICATION_COMMAND_UPDATE.ts index db36be346..8e5328d33 100644 --- a/src/handlers/commands/APPLICATION_COMMAND_UPDATE.ts +++ b/src/handlers/commands/APPLICATION_COMMAND_UPDATE.ts @@ -1,4 +1,5 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export function handleApplicationCommandUpdate(data: DiscordGatewayPayload) { const { diff --git a/src/handlers/emojis/GUILD_EMOJIS_UPDATE.ts b/src/handlers/emojis/GUILD_EMOJIS_UPDATE.ts index 83fce84bc..031f97bb5 100644 --- a/src/handlers/emojis/GUILD_EMOJIS_UPDATE.ts +++ b/src/handlers/emojis/GUILD_EMOJIS_UPDATE.ts @@ -1,10 +1,8 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; -import { - DiscordGatewayPayload, - DiscordGuildEmojisUpdate, -} from "../../types/mod.ts"; import { Collection } from "../../util/collection.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordGuildEmojisUpdate } from "../../types/emojis/guild_emojis_update.ts"; export async function handleGuildEmojisUpdate(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildEmojisUpdate; diff --git a/src/handlers/guilds/GUILD_BAN_ADD.ts b/src/handlers/guilds/GUILD_BAN_ADD.ts index 96d48d84a..7fa687f12 100644 --- a/src/handlers/guilds/GUILD_BAN_ADD.ts +++ b/src/handlers/guilds/GUILD_BAN_ADD.ts @@ -1,5 +1,6 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export async function handleGuildBanAdd(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildBanAddRemove; diff --git a/src/handlers/guilds/GUILD_BAN_REMOVE.ts b/src/handlers/guilds/GUILD_BAN_REMOVE.ts index 7b6c160c4..14022ede5 100644 --- a/src/handlers/guilds/GUILD_BAN_REMOVE.ts +++ b/src/handlers/guilds/GUILD_BAN_REMOVE.ts @@ -1,5 +1,6 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export async function handleGuildBanRemove(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildBanAddRemove; diff --git a/src/handlers/guilds/GUILD_CREATE.ts b/src/handlers/guilds/GUILD_CREATE.ts index 3bd318705..d298b39a8 100644 --- a/src/handlers/guilds/GUILD_CREATE.ts +++ b/src/handlers/guilds/GUILD_CREATE.ts @@ -2,6 +2,8 @@ import { eventHandlers } from "../../bot.ts"; import { cache, cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; import { basicShards } from "../../ws/shard.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordGuild } from "../../types/guilds/guild.ts"; export async function handleGuildCreate( data: DiscordGatewayPayload, @@ -23,6 +25,8 @@ export async function handleGuildCreate( await cacheHandlers.delete("unavailableGuilds", payload.id); shard.unavailableGuildIds.delete(payload.id); + + return eventHandlers.guildAvailable?.(guildStruct); } if (!cache.isReady) return eventHandlers.guildLoaded?.(guildStruct); diff --git a/src/handlers/guilds/GUILD_DELETE.ts b/src/handlers/guilds/GUILD_DELETE.ts index 10ab94493..a1ab0dd59 100644 --- a/src/handlers/guilds/GUILD_DELETE.ts +++ b/src/handlers/guilds/GUILD_DELETE.ts @@ -1,6 +1,8 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { basicShards } from "../../ws/shard.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordUnavailableGuild } from "../../types/guilds/unavailable_guild.ts"; export async function handleGuildDelete( data: DiscordGatewayPayload, @@ -18,9 +20,11 @@ export async function handleGuildDelete( if (shard) shard.unavailableGuildIds.add(payload.id); await cacheHandlers.set("unavailableGuilds", payload.id, Date.now()); - } - eventHandlers.guildDelete?.(guild); + eventHandlers.guildUnavailable?.(guild); + } else { + eventHandlers.guildDelete?.(guild); + } cacheHandlers.forEach("messages", (message) => { if (message.guildId === payload.id) { diff --git a/src/handlers/guilds/GUILD_INTEGRATIONS_UPDATE.ts b/src/handlers/guilds/GUILD_INTEGRATIONS_UPDATE.ts index 8b5c251a2..97fbf2a50 100644 --- a/src/handlers/guilds/GUILD_INTEGRATIONS_UPDATE.ts +++ b/src/handlers/guilds/GUILD_INTEGRATIONS_UPDATE.ts @@ -1,5 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordGuildIntegrationsUpdate } from "../../types/guilds/guild_integrations_update.ts"; export async function handleGuildIntegrationsUpdate( data: DiscordGatewayPayload, diff --git a/src/handlers/guilds/GUILD_UPDATE.ts b/src/handlers/guilds/GUILD_UPDATE.ts index 53064fc3d..cb92b88af 100644 --- a/src/handlers/guilds/GUILD_UPDATE.ts +++ b/src/handlers/guilds/GUILD_UPDATE.ts @@ -1,5 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordGuild } from "../../types/guilds/guild.ts"; export async function handleGuildUpdate(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuild; diff --git a/src/handlers/integrations/INTEGRATION_CREATE.ts b/src/handlers/integrations/INTEGRATION_CREATE.ts index 0fb5d2cd0..4148db518 100644 --- a/src/handlers/integrations/INTEGRATION_CREATE.ts +++ b/src/handlers/integrations/INTEGRATION_CREATE.ts @@ -1,4 +1,5 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export function handleIntegrationCreate( data: DiscordGatewayPayload, diff --git a/src/handlers/integrations/INTEGRATION_DELETE.ts b/src/handlers/integrations/INTEGRATION_DELETE.ts index 2b4df37fe..c89ae3d3b 100644 --- a/src/handlers/integrations/INTEGRATION_DELETE.ts +++ b/src/handlers/integrations/INTEGRATION_DELETE.ts @@ -1,4 +1,5 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export function handleIntegrationDelete(data: DiscordGatewayPayload) { const { diff --git a/src/handlers/integrations/INTEGRATION_UPDATE.ts b/src/handlers/integrations/INTEGRATION_UPDATE.ts index 086ae10e2..13fb98bed 100644 --- a/src/handlers/integrations/INTEGRATION_UPDATE.ts +++ b/src/handlers/integrations/INTEGRATION_UPDATE.ts @@ -1,4 +1,5 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export function handleIntegrationUpdate(data: DiscordGatewayPayload) { const { diff --git a/src/handlers/interactions/INTERACTION_CREATE.ts b/src/handlers/interactions/INTERACTION_CREATE.ts index 40282a4b4..dc59476d3 100644 --- a/src/handlers/interactions/INTERACTION_CREATE.ts +++ b/src/handlers/interactions/INTERACTION_CREATE.ts @@ -1,6 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export async function handleInteractionCreate(data: DiscordGatewayPayload) { const payload = data.d as InteractionCommandPayload; diff --git a/src/handlers/invites/INVITE_CREATE.ts b/src/handlers/invites/INVITE_CREATE.ts index 9f1df9af1..f53f0b102 100644 --- a/src/handlers/invites/INVITE_CREATE.ts +++ b/src/handlers/invites/INVITE_CREATE.ts @@ -1,4 +1,6 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordInviteCreate } from "../../types/invites/invite_create.ts"; export function handleInviteCreate(payload: DiscordGatewayPayload) { // TODO: replace with tocamelcase diff --git a/src/handlers/invites/INVITE_DELETE.ts b/src/handlers/invites/INVITE_DELETE.ts index 057c5820c..cddae5a27 100644 --- a/src/handlers/invites/INVITE_DELETE.ts +++ b/src/handlers/invites/INVITE_DELETE.ts @@ -1,4 +1,6 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordInviteDelete } from "../../types/invites/invite_delete.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export function handleInviteDelete(payload: DiscordGatewayPayload) { const { diff --git a/src/handlers/members/GUILD_MEMBERS_CHUNK.ts b/src/handlers/members/GUILD_MEMBERS_CHUNK.ts index c3427ea57..e3452cb34 100644 --- a/src/handlers/members/GUILD_MEMBERS_CHUNK.ts +++ b/src/handlers/members/GUILD_MEMBERS_CHUNK.ts @@ -1,6 +1,8 @@ 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"; export async function handleGuildMembersChunk(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildMembersChunk; diff --git a/src/handlers/members/GUILD_MEMBER_ADD.ts b/src/handlers/members/GUILD_MEMBER_ADD.ts index f548baa4c..f9d5609fb 100644 --- a/src/handlers/members/GUILD_MEMBER_ADD.ts +++ b/src/handlers/members/GUILD_MEMBER_ADD.ts @@ -1,6 +1,8 @@ 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"; export async function handleGuildMemberAdd(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildMemberAdd; diff --git a/src/handlers/members/GUILD_MEMBER_REMOVE.ts b/src/handlers/members/GUILD_MEMBER_REMOVE.ts index 59415f038..0b4e66495 100644 --- a/src/handlers/members/GUILD_MEMBER_REMOVE.ts +++ b/src/handlers/members/GUILD_MEMBER_REMOVE.ts @@ -1,5 +1,7 @@ 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"; export async function handleGuildMemberRemove(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildMemberRemove; diff --git a/src/handlers/members/GUILD_MEMBER_UPDATE.ts b/src/handlers/members/GUILD_MEMBER_UPDATE.ts index d15c6ee2f..f8b5ffe41 100644 --- a/src/handlers/members/GUILD_MEMBER_UPDATE.ts +++ b/src/handlers/members/GUILD_MEMBER_UPDATE.ts @@ -1,6 +1,8 @@ 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"; export async function handleGuildMemberUpdate(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildMemberUpdate; diff --git a/src/handlers/messages/MESSAGE_CREATE.ts b/src/handlers/messages/MESSAGE_CREATE.ts index 017f5bf74..04c096849 100644 --- a/src/handlers/messages/MESSAGE_CREATE.ts +++ b/src/handlers/messages/MESSAGE_CREATE.ts @@ -1,6 +1,8 @@ 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"; export async function handleMessageCreate(data: DiscordGatewayPayload) { const payload = data.d as DiscordMessage; diff --git a/src/handlers/messages/MESSAGE_DELETE.ts b/src/handlers/messages/MESSAGE_DELETE.ts index 99a1e0394..48f25006f 100644 --- a/src/handlers/messages/MESSAGE_DELETE.ts +++ b/src/handlers/messages/MESSAGE_DELETE.ts @@ -1,5 +1,7 @@ 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"; export async function handleMessageDelete(data: DiscordGatewayPayload) { const payload = data.d as DiscordMessageDelete; diff --git a/src/handlers/messages/MESSAGE_DELETE_BULK.ts b/src/handlers/messages/MESSAGE_DELETE_BULK.ts index 188493557..050717f1a 100644 --- a/src/handlers/messages/MESSAGE_DELETE_BULK.ts +++ b/src/handlers/messages/MESSAGE_DELETE_BULK.ts @@ -1,5 +1,7 @@ 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"; export async function handleMessageDeleteBulk(data: DiscordGatewayPayload) { const payload = data.d as DiscordMessageDeleteBulk; diff --git a/src/handlers/messages/MESSAGE_REACTION_ADD.ts b/src/handlers/messages/MESSAGE_REACTION_ADD.ts index b5878fbc1..1ff39127b 100644 --- a/src/handlers/messages/MESSAGE_REACTION_ADD.ts +++ b/src/handlers/messages/MESSAGE_REACTION_ADD.ts @@ -1,6 +1,8 @@ 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"; export async function handleMessageReactionAdd(data: DiscordGatewayPayload) { const payload = data.d as DiscordMessageReactionAdd; diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts index 8a530fa00..91e2dd7bf 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE.ts @@ -1,6 +1,8 @@ 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 { DiscordMessageReactionRemove } from "../../types/messages/message_reaction_remove.ts"; export async function handleMessageReactionRemove( data: DiscordGatewayPayload, diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts index 218baa31e..29ff5b3ed 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts @@ -1,5 +1,7 @@ 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"; export async function handleMessageReactionRemoveAll( data: DiscordGatewayPayload, diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts index ccac604ca..677c51b02 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE_EMOJI.ts @@ -1,5 +1,7 @@ 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"; export async function handleMessageReactionRemoveEmoji( data: DiscordGatewayPayload, diff --git a/src/handlers/messages/MESSAGE_UPDATE.ts b/src/handlers/messages/MESSAGE_UPDATE.ts index 7544106ea..e0bd4d1be 100644 --- a/src/handlers/messages/MESSAGE_UPDATE.ts +++ b/src/handlers/messages/MESSAGE_UPDATE.ts @@ -1,6 +1,8 @@ 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"; export async function handleMessageUpdate(data: DiscordGatewayPayload) { const payload = data.d as DiscordMessage; diff --git a/src/handlers/misc/PRESENCE_UPDATE.ts b/src/handlers/misc/PRESENCE_UPDATE.ts index 0590c0955..66a6cc6c3 100644 --- a/src/handlers/misc/PRESENCE_UPDATE.ts +++ b/src/handlers/misc/PRESENCE_UPDATE.ts @@ -1,5 +1,7 @@ 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"; export async function handlePresenceUpdate(data: DiscordGatewayPayload) { const payload = data.d as DiscordPresenceUpdate; diff --git a/src/handlers/misc/READY.ts b/src/handlers/misc/READY.ts index 793c6cca7..095b8a992 100644 --- a/src/handlers/misc/READY.ts +++ b/src/handlers/misc/READY.ts @@ -9,6 +9,8 @@ import { initialMemberLoadQueue } from "../../structures/guild.ts"; import { structures } from "../../structures/mod.ts"; import { delay } from "../../util/utils.ts"; import { allowNextShard, basicShards } from "../../ws/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordReady } from "../../types/gateway/ready.ts"; export async function handleReady( data: DiscordGatewayPayload, diff --git a/src/handlers/misc/TYPING_START.ts b/src/handlers/misc/TYPING_START.ts index 8f0274953..9775758d7 100644 --- a/src/handlers/misc/TYPING_START.ts +++ b/src/handlers/misc/TYPING_START.ts @@ -1,4 +1,6 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordTypingStart } from "../../types/misc/typing_start.ts"; export function handleTypingStart(data: DiscordGatewayPayload) { eventHandlers.typingStart?.(data.d as DiscordTypingStart); diff --git a/src/handlers/misc/USER_UPDATE.ts b/src/handlers/misc/USER_UPDATE.ts index 7008702eb..07e9dbcbc 100644 --- a/src/handlers/misc/USER_UPDATE.ts +++ b/src/handlers/misc/USER_UPDATE.ts @@ -1,5 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordUser } from "../../types/users/user.ts"; export async function handleUserUpdate(data: DiscordGatewayPayload) { const userData = data.d as DiscordUser; diff --git a/src/handlers/roles/GUILD_ROLE_CREATE.ts b/src/handlers/roles/GUILD_ROLE_CREATE.ts index cc6880660..24720435e 100644 --- a/src/handlers/roles/GUILD_ROLE_CREATE.ts +++ b/src/handlers/roles/GUILD_ROLE_CREATE.ts @@ -1,6 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export async function handleGuildRoleCreate(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildRoleCreateUpdate; diff --git a/src/handlers/roles/GUILD_ROLE_DELETE.ts b/src/handlers/roles/GUILD_ROLE_DELETE.ts index b06f1da9e..390fb0cfd 100644 --- a/src/handlers/roles/GUILD_ROLE_DELETE.ts +++ b/src/handlers/roles/GUILD_ROLE_DELETE.ts @@ -1,5 +1,7 @@ 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"; export async function handleGuildRoleDelete(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildRoleDelete; diff --git a/src/handlers/roles/GUILD_ROLE_UPDATE.ts b/src/handlers/roles/GUILD_ROLE_UPDATE.ts index 55032d3c5..9c02deeb9 100644 --- a/src/handlers/roles/GUILD_ROLE_UPDATE.ts +++ b/src/handlers/roles/GUILD_ROLE_UPDATE.ts @@ -1,6 +1,7 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; export async function handleGuildRoleUpdate(data: DiscordGatewayPayload) { const payload = data.d as DiscordGuildRoleCreateUpdate; diff --git a/src/handlers/voice/VOICE_STATE_UPDATE.ts b/src/handlers/voice/VOICE_STATE_UPDATE.ts index d67f55d58..0a62c2949 100644 --- a/src/handlers/voice/VOICE_STATE_UPDATE.ts +++ b/src/handlers/voice/VOICE_STATE_UPDATE.ts @@ -1,6 +1,8 @@ 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"; export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) { const payload = data.d as DiscordVoiceState; diff --git a/src/handlers/webhooks/WEBHOOKS_UPDATE.ts b/src/handlers/webhooks/WEBHOOKS_UPDATE.ts index 0b1f53d19..86e10a68e 100644 --- a/src/handlers/webhooks/WEBHOOKS_UPDATE.ts +++ b/src/handlers/webhooks/WEBHOOKS_UPDATE.ts @@ -1,4 +1,6 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordWebhooksUpdate } from "../../types/webhooks/webhooks_update.ts"; export function handleWebhooksUpdate(data: DiscordGatewayPayload) { const options = data.d as DiscordWebhooksUpdate; diff --git a/src/helpers/channels/create_channel.ts b/src/helpers/channels/create_channel.ts index daf1b2040..6292308fa 100644 --- a/src/helpers/channels/create_channel.ts +++ b/src/helpers/channels/create_channel.ts @@ -1,12 +1,10 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; -import { - CreateGuildChannel, - DiscordChannel, - DiscordChannelTypes, - PermissionStrings, -} from "../../types/mod.ts"; +import { DiscordChannel } from "../../types/channels/channel.ts"; +import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; +import { CreateGuildChannel } from "../../types/guilds/create_guild_channel.ts"; +import { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import { endpoints } from "../../util/constants.ts"; import { calculateBits, @@ -17,7 +15,7 @@ import { export async function createChannel( guildId: string, name: string, - options?: CreateGuildChannel, + options?: CreateGuildChannel ) { const requiredPerms: Set = new Set(["MANAGE_CHANNELS"]); @@ -28,8 +26,10 @@ export async function createChannel( await requireBotGuildPermissions(guildId, [...requiredPerms]); - const result = - (await rest.runMethod("post", endpoints.GUILD_CHANNELS(guildId), { + const result = (await rest.runMethod( + "post", + endpoints.GUILD_CHANNELS(guildId), + { ...options, name, permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ @@ -39,7 +39,8 @@ export async function createChannel( deny: calculateBits(perm.deny), })), type: options?.type || DiscordChannelTypes.GUILD_TEXT, - })) as DiscordChannel; + } + )) as DiscordChannel; const channelStruct = await structures.createChannelStruct(result); await cacheHandlers.set("channels", channelStruct.id, channelStruct); diff --git a/src/helpers/channels/delete_channel.ts b/src/helpers/channels/delete_channel.ts index 3a2e92797..764c73844 100644 --- a/src/helpers/channels/delete_channel.ts +++ b/src/helpers/channels/delete_channel.ts @@ -1,6 +1,6 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; -import { Errors } from "../../types/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/channels/edit_channel_overwrite.ts b/src/helpers/channels/edit_channel_overwrite.ts index e68b357e6..451dd6f08 100644 --- a/src/helpers/channels/edit_channel_overwrite.ts +++ b/src/helpers/channels/edit_channel_overwrite.ts @@ -1,5 +1,5 @@ import { rest } from "../../rest/rest.ts"; -import { Overwrite } from "../../types/mod.ts"; +import { Overwrite } from "../../types/channels/overwrite.ts"; import { endpoints } from "../../util/constants.ts"; import { calculateBits, diff --git a/src/helpers/channels/follow_channel.ts b/src/helpers/channels/follow_channel.ts index bd44d8bc7..b8c9e7e27 100644 --- a/src/helpers/channels/follow_channel.ts +++ b/src/helpers/channels/follow_channel.ts @@ -1,5 +1,5 @@ import { rest } from "../../rest/rest.ts"; -import { DiscordFollowedChannel } from "../../types/mod.ts"; +import { DiscordFollowedChannel } from "../../types/channels/followed_channel.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/channels/get_channel.ts b/src/helpers/channels/get_channel.ts index eb2268252..bd5e880bb 100644 --- a/src/helpers/channels/get_channel.ts +++ b/src/helpers/channels/get_channel.ts @@ -1,7 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; -import { DiscordChannel } from "../../types/mod.ts"; +import { DiscordChannel } from "../../types/channels/channel.ts"; import { endpoints } from "../../util/constants.ts"; /** Fetches a single channel object from the api. @@ -9,11 +9,10 @@ import { endpoints } from "../../util/constants.ts"; * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.** */ export async function getChannel(channelId: string, addToCache = true) { - const result = - (await rest.runMethod( - "get", - endpoints.CHANNEL_BASE(channelId), - )) as DiscordChannel; + const result = (await rest.runMethod( + "get", + endpoints.CHANNEL_BASE(channelId), + )) as DiscordChannel; const channelStruct = await structures.createChannelStruct( result, diff --git a/src/helpers/channels/get_channel_webhooks.ts b/src/helpers/channels/get_channel_webhooks.ts index 050c21e01..2fd0ecd4b 100644 --- a/src/helpers/channels/get_channel_webhooks.ts +++ b/src/helpers/channels/get_channel_webhooks.ts @@ -1,5 +1,5 @@ import { rest } from "../../rest/rest.ts"; -import { DiscordWebhook } from "../../types/mod.ts"; +import { DiscordWebhook } from "../../types/webhooks/webhook.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/channels/get_channels.ts b/src/helpers/channels/get_channels.ts index 15e7b9cbb..21b0999d0 100644 --- a/src/helpers/channels/get_channels.ts +++ b/src/helpers/channels/get_channels.ts @@ -1,7 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; -import { DiscordChannel } from "../../types/mod.ts"; +import { DiscordChannel } from "../../types/channels/channel.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns a list of guild channel objects. @@ -9,11 +9,10 @@ import { endpoints } from "../../util/constants.ts"; * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.** */ export async function getChannels(guildId: string, addToCache = true) { - const result = - (await rest.runMethod( - "get", - endpoints.GUILD_CHANNELS(guildId), - ) as DiscordChannel[]); + const result = (await rest.runMethod( + "get", + endpoints.GUILD_CHANNELS(guildId), + ) as DiscordChannel[]); return Promise.all(result.map(async (res) => { const channelStruct = await structures.createChannelStruct(res, guildId); diff --git a/src/helpers/channels/get_pins.ts b/src/helpers/channels/get_pins.ts index 14bddf199..6f6d12a2d 100644 --- a/src/helpers/channels/get_pins.ts +++ b/src/helpers/channels/get_pins.ts @@ -1,15 +1,14 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; -import { DiscordMessage } from "../../types/mod.ts"; +import { DiscordMessage } from "../../types/messages/message.ts"; import { endpoints } from "../../util/constants.ts"; /** Get pinned messages in this channel. */ export async function getPins(channelId: string) { - const result = - (await rest.runMethod( - "get", - endpoints.CHANNEL_PINS(channelId), - )) as DiscordMessage[]; + const result = (await rest.runMethod( + "get", + endpoints.CHANNEL_PINS(channelId), + )) as DiscordMessage[]; return Promise.all( result.map((res) => structures.createMessageStruct(res)), diff --git a/src/helpers/channels/start_typing.ts b/src/helpers/channels/start_typing.ts index edcaa82a1..ca181dcbe 100644 --- a/src/helpers/channels/start_typing.ts +++ b/src/helpers/channels/start_typing.ts @@ -1,6 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; -import { DiscordChannelTypes, Errors } from "../../types/mod.ts"; +import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { botHasChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/channels/swap_channels.ts b/src/helpers/channels/swap_channels.ts index a85b8ca07..8b3662278 100644 --- a/src/helpers/channels/swap_channels.ts +++ b/src/helpers/channels/swap_channels.ts @@ -1,5 +1,5 @@ import { rest } from "../../rest/rest.ts"; -import { ModifyGuildChannelPositions } from "../../types/mod.ts"; +import { ModifyGuildChannelPositions } from "../../types/guilds/modify_guild_channel_position.ts"; import { endpoints } from "../../util/constants.ts"; /** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */ diff --git a/src/helpers/commands/edit_slash_response.ts b/src/helpers/commands/edit_slash_response.ts index 94f82a003..fa81ac460 100644 --- a/src/helpers/commands/edit_slash_response.ts +++ b/src/helpers/commands/edit_slash_response.ts @@ -1,6 +1,7 @@ import { applicationId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** To edit your response to a slash command. If a messageId is not provided it will default to editing the original response. */ diff --git a/src/helpers/commands/get_slash_commands.ts b/src/helpers/commands/get_slash_commands.ts index 8e86a24bf..ce2cc50bf 100644 --- a/src/helpers/commands/get_slash_commands.ts +++ b/src/helpers/commands/get_slash_commands.ts @@ -5,13 +5,12 @@ import { endpoints } from "../../util/constants.ts"; /** Fetch all of the global commands for your application. */ export async function getSlashCommands(guildId?: string) { - const result = - (await rest.runMethod( - "get", - guildId - ? endpoints.COMMANDS_GUILD(applicationId, guildId) - : endpoints.COMMANDS(applicationId), - )) as SlashCommand[]; + const result = (await rest.runMethod( + "get", + guildId + ? endpoints.COMMANDS_GUILD(applicationId, guildId) + : endpoints.COMMANDS(applicationId), + )) as SlashCommand[]; return new Collection(result.map((command) => [command.name, command])); } diff --git a/src/helpers/guilds/create_guild.ts b/src/helpers/guilds/create_guild.ts index 2eb52b212..144d260a4 100644 --- a/src/helpers/guilds/create_guild.ts +++ b/src/helpers/guilds/create_guild.ts @@ -4,12 +4,11 @@ import { endpoints } from "../../util/constants.ts"; /** Create a new guild. Returns a guild object on success. Fires a Guild Create Gateway event. This endpoint can be used only by bots in less than 10 guilds. */ export async function createGuild(options: CreateServerOptions) { - const guild = - (await rest.runMethod( - "post", - endpoints.GUILDS, - options, - )) as CreateGuildPayload; + const guild = (await rest.runMethod( + "post", + endpoints.GUILDS, + options, + )) as CreateGuildPayload; return structures.createGuildStruct(guild, 0); } diff --git a/src/helpers/guilds/get_audit_logs.ts b/src/helpers/guilds/get_audit_logs.ts index b4563bbe3..63ee31d2f 100644 --- a/src/helpers/guilds/get_audit_logs.ts +++ b/src/helpers/guilds/get_audit_logs.ts @@ -18,7 +18,8 @@ export async function getAuditLogs( ? AuditLogs[options.action_type] : undefined, limit: options.limit && options.limit >= 1 && options.limit <= 100 - ? options.limit : 50, + ? options.limit + : 50, }, ); diff --git a/src/helpers/guilds/get_bans.ts b/src/helpers/guilds/get_bans.ts index aabd031c5..c32820525 100644 --- a/src/helpers/guilds/get_bans.ts +++ b/src/helpers/guilds/get_bans.ts @@ -7,11 +7,10 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts"; export async function getBans(guildId: string) { await requireBotGuildPermissions(guildId, ["BAN_MEMBERS"]); - const results = - (await rest.runMethod( - "get", - endpoints.GUILD_BANS(guildId), - )) as BannedUser[]; + const results = (await rest.runMethod( + "get", + endpoints.GUILD_BANS(guildId), + )) as BannedUser[]; return new Collection( results.map((res) => [res.user.id, res]), diff --git a/src/helpers/guilds/get_prune_count.ts b/src/helpers/guilds/get_prune_count.ts index 3b16b6c9a..1d81987ba 100644 --- a/src/helpers/guilds/get_prune_count.ts +++ b/src/helpers/guilds/get_prune_count.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { camelKeysToSnakeCase } from "../../util/utils.ts"; diff --git a/src/helpers/guilds/get_widget.ts b/src/helpers/guilds/get_widget.ts index 4583dcd98..f44197d37 100644 --- a/src/helpers/guilds/get_widget.ts +++ b/src/helpers/guilds/get_widget.ts @@ -1,5 +1,6 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns the widget for the guild. */ diff --git a/src/helpers/guilds/get_widget_image_url.ts b/src/helpers/guilds/get_widget_image_url.ts index 10605f4d8..6e9eb01b8 100644 --- a/src/helpers/guilds/get_widget_image_url.ts +++ b/src/helpers/guilds/get_widget_image_url.ts @@ -1,4 +1,5 @@ import { cacheHandlers } from "../../cache.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns the widget image URL for the guild. */ diff --git a/src/helpers/invites/delete_invite.ts b/src/helpers/invites/delete_invite.ts index de97f59c9..643cbdde9 100644 --- a/src/helpers/invites/delete_invite.ts +++ b/src/helpers/invites/delete_invite.ts @@ -1,5 +1,6 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { botHasChannelPermissions, diff --git a/src/helpers/members/avatar_url.ts b/src/helpers/members/avatar_url.ts index 3b9d9def3..ae6c90a2a 100644 --- a/src/helpers/members/avatar_url.ts +++ b/src/helpers/members/avatar_url.ts @@ -1,17 +1,15 @@ -import { Member } from "../../structures/mod.ts"; -import { rawAvatarURL } from "./raw_avatar_url.ts"; +import { endpoints } from "../../util/constants.ts"; +import { formatImageURL } from "../../util/utils.ts"; -/** The users custom avatar or the default avatar */ +/** The users custom avatar or the default avatar if you don't have a member object. */ export function avatarURL( - member: Member, + userId: string, + discriminator: string, + avatar?: string | null, size: ImageSize = 128, format?: ImageFormats, ) { - return rawAvatarURL( - member.id, - member.discriminator, - member.avatar, - size, - format, - ); + return avatar + ? formatImageURL(endpoints.USER_AVATAR(userId, avatar), size, format) + : endpoints.USER_DEFAULT_AVATAR(Number(discriminator) % 5); } diff --git a/src/helpers/members/edit_bot_profile.ts b/src/helpers/members/edit_bot_profile.ts index c010c9f67..062d0433f 100644 --- a/src/helpers/members/edit_bot_profile.ts +++ b/src/helpers/members/edit_bot_profile.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { urlToBase64 } from "../../util/utils.ts"; diff --git a/src/helpers/members/edit_member.ts b/src/helpers/members/edit_member.ts index a7c1a9a9e..067590835 100644 --- a/src/helpers/members/edit_member.ts +++ b/src/helpers/members/edit_member.ts @@ -1,6 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions, diff --git a/src/helpers/members/fetch_members.ts b/src/helpers/members/fetch_members.ts index 448f13eac..173a65b3d 100644 --- a/src/helpers/members/fetch_members.ts +++ b/src/helpers/members/fetch_members.ts @@ -1,7 +1,11 @@ import { identifyPayload } from "../../bot.ts"; import { Member } from "../../structures/mod.ts"; +import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { Collection } from "../../util/collection.ts"; import { requestAllMembers } from "../../ws/shard_manager.ts"; +import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts"; +import { Errors } from "../../types/misc/errors.ts"; /** * ⚠️ BEGINNER DEVS!! YOU SHOULD ALMOST NEVER NEED THIS AND YOU CAN GET FROM cache.members.get() @@ -19,7 +23,7 @@ export function fetchMembers( // You can request 1 member without the intent if ( (!options?.limit || options.limit > 1) && - !(identifyPayload.intents && Intents.GUILD_MEMBERS) + !(identifyPayload.intents && DiscordGatewayIntents.GUILD_MEMBERS) ) { throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS); } diff --git a/src/helpers/members/get_member.ts b/src/helpers/members/get_member.ts index bc332d57f..e98ca9320 100644 --- a/src/helpers/members/get_member.ts +++ b/src/helpers/members/get_member.ts @@ -1,5 +1,4 @@ import { cacheHandlers } from "../../cache.ts"; -import { RequestManager } from "../../rest/request_manager.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; import { endpoints } from "../../util/constants.ts"; @@ -16,9 +15,11 @@ export async function getMember( const guild = await cacheHandlers.get("guilds", guildId); if (!guild && !options?.force) return; - const data = (await rest.runMethod("get", - endpoints.GUILD_MEMBER(guildId, id), - )) as MemberCreatePayload; + const data = + (await rest.runMethod( + "get", + endpoints.GUILD_MEMBER(guildId, id), + )) as MemberCreatePayload; const memberStruct = await structures.createMemberStruct(data, guildId); await cacheHandlers.set("members", memberStruct.id, memberStruct); diff --git a/src/helpers/members/get_members.ts b/src/helpers/members/get_members.ts index 18126948b..2884689f6 100644 --- a/src/helpers/members/get_members.ts +++ b/src/helpers/members/get_members.ts @@ -2,6 +2,9 @@ import { identifyPayload } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { Member, structures } from "../../structures/mod.ts"; +import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts"; +import { DiscordGuildMember } from "../../types/guilds/guild_member.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; @@ -14,7 +17,7 @@ import { endpoints } from "../../util/constants.ts"; * GW(fetchMembers): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is 960/m. */ export async function getMembers(guildId: string, options?: GetMemberOptions) { - if (!(identifyPayload.intents && Intents.GUILD_MEMBERS)) { + if (!(identifyPayload.intents && DiscordGatewayIntents.GUILD_MEMBERS)) { throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS); } @@ -39,19 +42,12 @@ export async function getMembers(guildId: string, options?: GetMemberOptions) { ); } - const result = - (await rest.runMethod( - "get", - `${endpoints.GUILD_MEMBERS(guildId)}?limit=${ - membersLeft > 1000 - ? 1000 - : membersLeft - }${ - options?.after - ? `&after=${options.after}` - : "" - }`, - )) as MemberCreatePayload[]; + const result = (await rest.runMethod( + "get", + `${endpoints.GUILD_MEMBERS(guildId)}?limit=${ + membersLeft > 1000 ? 1000 : membersLeft + }${options?.after ? `&after=${options.after}` : ""}`, + )) as DiscordGuildMember[]; const memberStructures = await Promise.all( result.map(async (member) => { diff --git a/src/helpers/members/kick_member.ts b/src/helpers/members/kick_member.ts index fe2fdd2f1..3adee00f6 100644 --- a/src/helpers/members/kick_member.ts +++ b/src/helpers/members/kick_member.ts @@ -1,5 +1,6 @@ import { botId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { highestRole, diff --git a/src/helpers/members/prune_members.ts b/src/helpers/members/prune_members.ts index 31e558946..74f48ed5b 100644 --- a/src/helpers/members/prune_members.ts +++ b/src/helpers/members/prune_members.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { camelKeysToSnakeCase } from "../../util/utils.ts"; diff --git a/src/helpers/members/raw_avatar_url.ts b/src/helpers/members/raw_avatar_url.ts deleted file mode 100644 index c95d5c5d6..000000000 --- a/src/helpers/members/raw_avatar_url.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { endpoints } from "../../util/constants.ts"; -import { formatImageURL } from "../../util/utils.ts"; - -/** The users custom avatar or the default avatar if you don't have a member object. */ -export function rawAvatarURL( - userId: string, - discriminator: string, - avatar?: string | null, - size: ImageSize = 128, - format?: ImageFormats, -) { - return avatar - ? formatImageURL(endpoints.USER_AVATAR(userId, avatar), size, format) - : endpoints.USER_DEFAULT_AVATAR(Number(discriminator) % 5); -} diff --git a/src/helpers/messages/delete_messages.ts b/src/helpers/messages/delete_messages.ts index 5c8cbd34e..ce7055d1a 100644 --- a/src/helpers/messages/delete_messages.ts +++ b/src/helpers/messages/delete_messages.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/messages/edit_message.ts b/src/helpers/messages/edit_message.ts index 6e74e21c5..1be74894f 100644 --- a/src/helpers/messages/edit_message.ts +++ b/src/helpers/messages/edit_message.ts @@ -1,6 +1,8 @@ import { botId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; import { Message, structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; +import { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; @@ -15,7 +17,7 @@ export async function editMessage( if (typeof content === "string") content = { content }; - const requiredPerms: Permission[] = ["SEND_MESSAGES"]; + const requiredPerms: PermissionStrings[] = ["SEND_MESSAGES"]; if (content.tts) requiredPerms.push("SEND_TTS_MESSAGES"); diff --git a/src/helpers/messages/get_message.ts b/src/helpers/messages/get_message.ts index 55ea57ce9..04d07c7ac 100644 --- a/src/helpers/messages/get_message.ts +++ b/src/helpers/messages/get_message.ts @@ -10,11 +10,10 @@ export async function getMessage(channelId: string, id: string) { "READ_MESSAGE_HISTORY", ]); - const result = - (await rest.runMethod( - "get", - endpoints.CHANNEL_MESSAGE(channelId, id), - )) as MessageCreateOptions; + const result = (await rest.runMethod( + "get", + endpoints.CHANNEL_MESSAGE(channelId, id), + )) as MessageCreateOptions; return structures.createMessageStruct(result); } diff --git a/src/helpers/messages/get_messages.ts b/src/helpers/messages/get_messages.ts index d70e8ce58..7fafe7ee1 100644 --- a/src/helpers/messages/get_messages.ts +++ b/src/helpers/messages/get_messages.ts @@ -19,12 +19,11 @@ export async function getMessages( if (options?.limit && options.limit > 100) return; - const result = - (await rest.runMethod( - "get", - endpoints.CHANNEL_MESSAGES(channelId), - options, - )) as MessageCreateOptions[]; + const result = (await rest.runMethod( + "get", + endpoints.CHANNEL_MESSAGES(channelId), + options, + )) as MessageCreateOptions[]; return Promise.all( result.map((res) => structures.createMessageStruct(res)), diff --git a/src/helpers/messages/get_reactions.ts b/src/helpers/messages/get_reactions.ts index 880406b9a..0f9ae95e4 100644 --- a/src/helpers/messages/get_reactions.ts +++ b/src/helpers/messages/get_reactions.ts @@ -9,12 +9,11 @@ export async function getReactions( reaction: string, options?: DiscordGetReactionsParams, ) { - const users = - (await rest.runMethod( - "get", - endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction), - options, - )) as UserPayload[]; + const users = (await rest.runMethod( + "get", + endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction), + options, + )) as UserPayload[]; return new Collection(users.map((user) => [user.id, user])); } diff --git a/src/helpers/messages/publish_message.ts b/src/helpers/messages/publish_message.ts index 7eb526f58..3c52869ec 100644 --- a/src/helpers/messages/publish_message.ts +++ b/src/helpers/messages/publish_message.ts @@ -4,11 +4,10 @@ import { endpoints } from "../../util/constants.ts"; /** Crosspost a message in a News Channel to following channels. */ export async function publishMessage(channelId: string, messageId: string) { - const data = - (await rest.runMethod( - "post", - endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId), - )) as MessageCreateOptions; + const data = (await rest.runMethod( + "post", + endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId), + )) as MessageCreateOptions; return structures.createMessageStruct(data); } diff --git a/src/helpers/messages/send_message.ts b/src/helpers/messages/send_message.ts index c29b56c03..b7c628917 100644 --- a/src/helpers/messages/send_message.ts +++ b/src/helpers/messages/send_message.ts @@ -2,7 +2,8 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; -import { PermissionStrings } from "../../types/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; +import { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/mod.ts b/src/helpers/mod.ts index 5a54f89b6..413436fb9 100644 --- a/src/helpers/mod.ts +++ b/src/helpers/mod.ts @@ -70,7 +70,6 @@ import { getMembersByQuery } from "./members/get_members_by_query.ts"; import { kick, kickMember } from "./members/kick_member.ts"; import { moveMember } from "./members/move_member.ts"; import { pruneMembers } from "./members/prune_members.ts"; -import { rawAvatarURL } from "./members/raw_avatar_url.ts"; import { sendDirectMessage } from "./members/send_direct_message.ts"; import { unban, unbanMember } from "./members/unban_member.ts"; import { addReaction } from "./messages/add_reaction.ts"; diff --git a/src/helpers/roles/add_role.ts b/src/helpers/roles/add_role.ts index b8770fe9a..6213823c3 100644 --- a/src/helpers/roles/add_role.ts +++ b/src/helpers/roles/add_role.ts @@ -1,5 +1,6 @@ import { botId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { isHigherPosition, diff --git a/src/helpers/roles/remove_role.ts b/src/helpers/roles/remove_role.ts index 96462916e..786b0136e 100644 --- a/src/helpers/roles/remove_role.ts +++ b/src/helpers/roles/remove_role.ts @@ -1,5 +1,6 @@ import { botId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { isHigherPosition, diff --git a/src/helpers/templates/create_guild_template.ts b/src/helpers/templates/create_guild_template.ts index ea3ea2718..54a6936e9 100644 --- a/src/helpers/templates/create_guild_template.ts +++ b/src/helpers/templates/create_guild_template.ts @@ -23,12 +23,11 @@ export async function createGuildTemplate( throw new Error("The description can only be in between 0-120 characters."); } - const template = - (await rest.runMethod( - "post", - endpoints.GUILD_TEMPLATES(guildId), - data, - )) as GuildTemplate; + const template = (await rest.runMethod( + "post", + endpoints.GUILD_TEMPLATES(guildId), + data, + )) as GuildTemplate; return structures.createTemplateStruct(template); } diff --git a/src/helpers/templates/delete_guild_template.ts b/src/helpers/templates/delete_guild_template.ts index 36ab88ead..26fd9ad7f 100644 --- a/src/helpers/templates/delete_guild_template.ts +++ b/src/helpers/templates/delete_guild_template.ts @@ -13,11 +13,10 @@ export async function deleteGuildTemplate( ) { await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - const deletedTemplate = - (await rest.runMethod( - "delete", - `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, - )) as GuildTemplate; + const deletedTemplate = (await rest.runMethod( + "delete", + `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, + )) as GuildTemplate; return structures.createTemplateStruct(deletedTemplate); } diff --git a/src/helpers/templates/edit_guild_template.ts b/src/helpers/templates/edit_guild_template.ts index d4d458f3e..72c1934c6 100644 --- a/src/helpers/templates/edit_guild_template.ts +++ b/src/helpers/templates/edit_guild_template.ts @@ -22,12 +22,11 @@ export async function editGuildTemplate( throw new Error("The description can only be in between 0-120 characters."); } - const template = - (await rest.runMethod( - "patch", - `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, - data, - )) as GuildTemplate; + const template = (await rest.runMethod( + "patch", + `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, + data, + )) as GuildTemplate; return structures.createTemplateStruct(template); } diff --git a/src/helpers/templates/get_guild_templates.ts b/src/helpers/templates/get_guild_templates.ts index 5c6c7bc74..02290900e 100644 --- a/src/helpers/templates/get_guild_templates.ts +++ b/src/helpers/templates/get_guild_templates.ts @@ -10,11 +10,10 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts"; export async function getGuildTemplates(guildId: string) { await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - const templates = - (await rest.runMethod( - "get", - endpoints.GUILD_TEMPLATES(guildId), - )) as GuildTemplate[]; + const templates = (await rest.runMethod( + "get", + endpoints.GUILD_TEMPLATES(guildId), + )) as GuildTemplate[]; return templates.map((template) => structures.createTemplateStruct(template)); } diff --git a/src/helpers/templates/get_template.ts b/src/helpers/templates/get_template.ts index 40e73c4f8..9c209123b 100644 --- a/src/helpers/templates/get_template.ts +++ b/src/helpers/templates/get_template.ts @@ -4,11 +4,10 @@ import { endpoints } from "../../util/constants.ts"; /** Returns the guild template if it exists */ export async function getTemplate(templateCode: string) { - const result = - (await rest.runMethod( - "get", - endpoints.GUILD_TEMPLATE(templateCode), - ) as GuildTemplate); + const result = (await rest.runMethod( + "get", + endpoints.GUILD_TEMPLATE(templateCode), + ) as GuildTemplate); const template = await structures.createTemplateStruct(result); return template; diff --git a/src/helpers/templates/sync_guild_template.ts b/src/helpers/templates/sync_guild_template.ts index a5fe10502..e657bb905 100644 --- a/src/helpers/templates/sync_guild_template.ts +++ b/src/helpers/templates/sync_guild_template.ts @@ -10,11 +10,10 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts"; export async function syncGuildTemplate(guildId: string, templateCode: string) { await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - const template = - (await rest.runMethod( - "put", - `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, - )) as GuildTemplate; + const template = (await rest.runMethod( + "put", + `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, + )) as GuildTemplate; return structures.createTemplateStruct(template); } diff --git a/src/helpers/webhooks/create_webhook.ts b/src/helpers/webhooks/create_webhook.ts index f94128ff8..bd771596f 100644 --- a/src/helpers/webhooks/create_webhook.ts +++ b/src/helpers/webhooks/create_webhook.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { urlToBase64 } from "../../util/utils.ts"; diff --git a/src/helpers/webhooks/edit_webhook_message.ts b/src/helpers/webhooks/edit_webhook_message.ts index fd68d5e4b..1653f9a77 100644 --- a/src/helpers/webhooks/edit_webhook_message.ts +++ b/src/helpers/webhooks/edit_webhook_message.ts @@ -1,5 +1,6 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; export async function editWebhookMessage( diff --git a/src/helpers/webhooks/execute_webhook.ts b/src/helpers/webhooks/execute_webhook.ts index 2a378645c..cee2d2fa2 100644 --- a/src/helpers/webhooks/execute_webhook.ts +++ b/src/helpers/webhooks/execute_webhook.ts @@ -1,5 +1,6 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** Execute a webhook with webhook Id and webhook token */ diff --git a/src/interactions/server.ts b/src/interactions/server.ts index 36ba85654..2c79031c4 100644 --- a/src/interactions/server.ts +++ b/src/interactions/server.ts @@ -90,7 +90,11 @@ async function handleApplicationCommand( } /** Internal function to verify security. Discord will send bad and good data and this function is important to verify it. If it is not verified properly, Discord will kill your bot. */ -export function verifySecurity(buffer: Uint8Array, signature: string, time: string) { +export function verifySecurity( + buffer: Uint8Array, + signature: string, + time: string, +) { const sig = new Uint8Array(64); const timestamp = new TextEncoder().encode(time); diff --git a/src/rest/rest.ts b/src/rest/rest.ts index 345f6b271..b38fb3865 100644 --- a/src/rest/rest.ts +++ b/src/rest/rest.ts @@ -22,6 +22,7 @@ export const rest = { console.error(error); }, // PLACEHOLDERS TO ALLOW USERS TO CUSTOMIZE + debug: function (_type, error) {}, fetching() {}, fetched() {}, fetchSuccess() {}, diff --git a/src/rest/run_method.ts b/src/rest/run_method.ts index b8f69712c..6c3780b4c 100644 --- a/src/rest/run_method.ts +++ b/src/rest/run_method.ts @@ -1,18 +1,23 @@ +import { Errors } from "../types/misc/errors.ts"; +import { IMAGE_BASE_URL } from "../util/constants.ts"; +import { API_VERSION } from "../util/constants.ts"; +import { BASE_URL } from "../util/constants.ts"; import { rest } from "./rest.ts"; export function runMethod( - method: RequestMethods, + method: "get" | "post" | "put" | "delete" | "patch", url: string, body?: unknown, retryCount = 0, bucketId?: string | null, ) { - rest.eventHandlers.debug?.( - { - type: "requestCreate", - data: { method, url, body, retryCount, bucketId }, - }, - ); + rest.eventHandlers.debug?.("requestCreate", { + method, + url, + body, + retryCount, + bucketId, + }); const errorStack = new Error("Location:"); Error.captureStackTrace(errorStack); @@ -45,38 +50,49 @@ export function runMethod( return new Promise(async (resolve, reject) => { const callback = async () => { try { - const rateLimitResetIn = await rest.checkRatelimits(url); + const rateLimitResetIn = rest.checkRateLimits(url); if (rateLimitResetIn) { return { rateLimited: rateLimitResetIn, beforeFetch: true, bucketId }; } const query = method === "get" && body ? // deno-lint-ignore no-explicit-any - Object.entries(body as any).map(([key, value]) => - // deno-lint-ignore no-explicit-any - `${encodeURIComponent(key)}=${encodeURIComponent(value as any)}` - ) + Object.entries(body as any) + .map( + ([key, value]) => + `${encodeURIComponent(key)}=${ + encodeURIComponent( + value as string | number | boolean, + ) + }`, + ) .join("&") : ""; const urlToUse = method === "get" && query ? `${url}?${query}` : url; - rest.eventHandlers.debug?.( - { - type: "requestFetch", - data: { method, url, body, retryCount, bucketId }, - }, - ); + rest.eventHandlers.debug?.("requestFetch", { + method, + url, + body, + retryCount, + bucketId, + }); const response = await fetch( urlToUse, rest.createRequestBody(body, method), ); - rest.eventHandlers.debug?.( - { - type: "requestFetched", - data: { method, url, body, retryCount, bucketId, response }, - }, + rest.eventHandlers.debug?.("requestFetched", { + method, + url, + body, + retryCount, + bucketId, + response, + }); + const bucketIdFromHeaders = rest.processRequestHeaders( + url, + response.headers, ); - const bucketIdFromHeaders = rest.processHeaders(url, response.headers); await rest.handleStatusCode(response, errorStack); // Sometimes Discord returns an empty 204 response that can't be made to JSON. @@ -88,12 +104,14 @@ export function runMethod( json.message === "You are being rate limited." ) { if (retryCount > 10) { - rest.eventHandlers.debug?.( - { - type: "error", - data: { method, url, body, retryCount, bucketId, errorStack }, - }, - ); + rest.eventHandlers.error?.("globalRateLimit", { + method, + url, + body, + retryCount, + bucketId, + errorStack, + }); throw new Error(Errors.RATE_LIMIT_RETRY_MAXED); } @@ -104,20 +122,23 @@ export function runMethod( }; } - rest.eventHandlers.debug?.( - { - type: "requestSuccess", - data: { method, url, body, retryCount, bucketId }, - }, - ); + rest.eventHandlers.debug?.("requestSuccess", { + method, + url, + body, + retryCount, + bucketId, + }); return resolve(json); } catch (error) { - rest.eventHandlers.debug?.( - { - type: "error", - data: { method, url, body, retryCount, bucketId, errorStack }, - }, - ); + rest.eventHandlers.error?.("unknown", { + method, + url, + body, + retryCount, + bucketId, + errorStack, + }); return reject(error); } }; diff --git a/src/structures/member.ts b/src/structures/member.ts index 00cc0108e..bb893164d 100644 --- a/src/structures/member.ts +++ b/src/structures/member.ts @@ -1,8 +1,8 @@ import { cache, cacheHandlers } from "../cache.ts"; +import { avatarURL } from "../helpers/members/avatar_url.ts"; import { banMember } from "../helpers/members/ban_member.ts"; import { editMember } from "../helpers/members/edit_member.ts"; import { kickMember } from "../helpers/members/kick_member.ts"; -import { rawAvatarURL } from "../helpers/members/raw_avatar_url.ts"; import { sendDirectMessage } from "../helpers/members/send_direct_message.ts"; import { addRole } from "../helpers/roles/add_role.ts"; import { removeRole } from "../helpers/roles/remove_role.ts"; @@ -11,7 +11,7 @@ import { createNewProp } from "../util/utils.ts"; const baseMember: Partial = { get avatarURL() { - return rawAvatarURL(this.id!, this.discriminator!, this.avatar!); + return avatarURL(this.id!, this.discriminator!, this.avatar!); }, get mention() { return `<@!${this.id!}>`; diff --git a/src/types/gateway/identify.ts b/src/types/gateway/identify.ts index 3832bb2ae..f9a7599b3 100644 --- a/src/types/gateway/identify.ts +++ b/src/types/gateway/identify.ts @@ -16,7 +16,7 @@ export interface Identify { /** Presence structure for initial presence information */ presence?: UpdateStatus; /** Enables dispatching of guild subscription events (presence and typing events) */ - guild_subscriptions?: boolean; + guildSubscriptions?: boolean; /** The Gateway Intents you wish to receive */ intents: number; } diff --git a/src/types/guilds/list_guild_members.ts b/src/types/guilds/list_guild_members.ts index 2fda0a7bb..ce297344b 100644 --- a/src/types/guilds/list_guild_members.ts +++ b/src/types/guilds/list_guild_members.ts @@ -1,4 +1,3 @@ - export interface ListGuildMembers { /** Max number of members to return (1-1000). Default: 1 */ limit?: number; diff --git a/src/types/users/connection.ts b/src/types/users/connection.ts index 97e638452..13a78c17a 100644 --- a/src/types/users/connection.ts +++ b/src/types/users/connection.ts @@ -1,5 +1,6 @@ import { SnakeCaseProps } from "../util.ts"; import { DiscordVisibilityTypes } from "./visibility_types.ts"; +import { Integration } from "../guilds/integration.ts" export interface Connection { /** id of the connection account */ diff --git a/src/util/permissions.ts b/src/util/permissions.ts index 9fa068d87..62ee90feb 100644 --- a/src/util/permissions.ts +++ b/src/util/permissions.ts @@ -1,15 +1,11 @@ import { botId } from "../bot.ts"; import { cacheHandlers } from "../cache.ts"; import { Channel, Guild, Member, Role } from "../structures/mod.ts"; -import { - DiscordBitwisePermissionFlags, - PermissionStrings, -} from "../types/mod.ts"; +import { Errors } from "../types/misc/errors.ts"; +import { DiscordBitwisePermissionFlags } from "../types/permissions/bitwise_permission_flags.ts"; +import { PermissionStrings } from "../types/permissions/permission_strings.ts"; -async function getCached( - table: "guilds", - key: string | Guild, -): Promise; +async function getCached(table: "guilds", key: string | Guild): Promise; async function getCached( table: "channels", key: string | Channel, @@ -24,7 +20,7 @@ async function getCached( ) { const cached = typeof key === "string" ? // @ts-ignore TS is wrong here - (await cacheHandlers.get(table, key)) + await cacheHandlers.get(table, key) : key; if (!cached || typeof cached === "string") { throw new Error( @@ -119,14 +115,15 @@ export async function calculateChannelOverwrites( /** Checks if the given permission bits are matching the given permissions. `ADMINISTRATOR` always returns `true` */ export function validatePermissions( permissionBits: string, - permissions: Permission[], + permissions: PermissionStrings[], ) { if (BigInt(permissionBits) & 8n) return true; return permissions.every( (permission) => // Check if permission is in permissionBits - BigInt(permissionBits) & BigInt(Permissions[permission]), + BigInt(permissionBits) & + BigInt(DiscordBitwisePermissionFlags[permission]), ); } @@ -134,7 +131,7 @@ export function validatePermissions( export async function hasGuildPermissions( guild: string | Guild, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[], ) { // First we need the role permission bits this member has const basePermissions = await calculateBasePermissions(guild, member); @@ -145,7 +142,7 @@ export async function hasGuildPermissions( /** Checks if the bot has these permissions in the given guild */ export function botHasGuildPermissions( guild: string | Guild, - permissions: Permission[], + permissions: PermissionStrings[], ) { // Since Bot is a normal member we can use the hasRolePermissions() function return hasGuildPermissions(guild, botId, permissions); @@ -155,13 +152,10 @@ export function botHasGuildPermissions( export async function hasChannelPermissions( channel: string | Channel, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[], ) { // First we need the overwrite bits this member has - const channelOverwrites = await calculateChannelOverwrites( - channel, - member, - ); + const channelOverwrites = await calculateChannelOverwrites(channel, member); // Second use the validatePermissions function to check if the member has every permission return validatePermissions(channelOverwrites, permissions); } @@ -169,7 +163,7 @@ export async function hasChannelPermissions( /** Checks if the bot has these permissions f0r the given channel */ export function botHasChannelPermissions( channel: string | Channel, - permissions: Permission[], + permissions: PermissionStrings[], ) { // Since Bot is a normal member we can use the hasRolePermissions() function return hasChannelPermissions(channel, botId, permissions); @@ -184,8 +178,10 @@ export function missingPermissions( return permissions.filter( (permission) => - !(BigInt(permissionBits) & - BigInt(DiscordBitwisePermissionFlags[permission])), + !( + BigInt(permissionBits) & + BigInt(DiscordBitwisePermissionFlags[permission]) + ), ); } @@ -193,7 +189,7 @@ export function missingPermissions( export async function getMissingGuildPermissions( guild: string | Guild, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[], ) { // First we need the role permissino bits this member has const permissionBits = await calculateBasePermissions(guild, member); @@ -205,7 +201,7 @@ export async function getMissingGuildPermissions( export async function getMissingChannelPermissions( channel: string | Channel, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[], ) { // First we need the role permissino bits this member has const permissionBits = await calculateChannelOverwrites(channel, member); @@ -217,7 +213,7 @@ export async function getMissingChannelPermissions( export async function requireGuildPermissions( guild: string | Guild, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[], ) { const missing = await getMissingGuildPermissions(guild, member, permissions); if (missing.length) { @@ -229,7 +225,7 @@ export async function requireGuildPermissions( /** Throws an error if the bot does not have all permissions */ export function requireBotGuildPermissions( guild: string | Guild, - permissions: Permission[], + permissions: PermissionStrings[], ) { // Since Bot is a normal member we can use the throwOnMissingGuildPermission() function return requireGuildPermissions(guild, botId, permissions); @@ -239,7 +235,7 @@ export function requireBotGuildPermissions( export async function requireChannelPermissions( channel: string | Channel, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[], ) { const missing = await getMissingChannelPermissions( channel, @@ -255,7 +251,7 @@ export async function requireChannelPermissions( /** Throws an error if the bot has not all of the given channel permissions */ export function requireBotChannelPermissions( channel: string | Channel, - permissions: Permission[], + permissions: PermissionStrings[], ) { // Since Bot is a normal member we can use the throwOnMissingChannelPermission() function return requireChannelPermissions(channel, botId, permissions); @@ -263,19 +259,20 @@ export function requireBotChannelPermissions( /** This function converts a bitwise string to permission strings */ export function calculatePermissions(permissionBits: bigint) { - return Object.keys(Permissions).filter((permission) => { + return Object.keys(DiscordBitwisePermissionFlags).filter((permission) => { // Since Object.keys() not only returns the permission names but also the bit values we need to return false if it is a Number if (Number(permission)) return false; // Check if permissionBits has this permission - return permissionBits & BigInt(Permissions[permission as Permission]); - }) as Permission[]; + return permissionBits & + BigInt(DiscordBitwisePermissionFlags[permission as PermissionStrings]); + }) as PermissionStrings[]; } /** This function converts an array of permissions into the bitwise string. */ -export function calculateBits(permissions: Permission[]) { +export function calculateBits(permissions: PermissionStrings[]) { return permissions .reduce((bits, perm) => { - bits |= BigInt(Permissions[perm]); + bits |= BigInt(DiscordBitwisePermissionFlags[perm]); return bits; }, 0n) .toString(); @@ -289,9 +286,8 @@ export async function highestRole( guild = await getCached("guilds", guild); // Get the roles from the member - const memberRoles = ( - await getCached("members", member) - ).guilds.get(guild.id)?.roles; + const memberRoles = (await getCached("members", member)).guilds.get(guild.id) + ?.roles; // This member has no roles so the highest one is the @everyone role if (!memberRoles) return guild.roles.get(guild.id) as Role; diff --git a/src/util/utils.ts b/src/util/utils.ts index 2e3abec21..18041d85b 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -1,5 +1,6 @@ import { encode } from "../../deps.ts"; -import { DiscordGatewayOpcodes } from "../types/mod.ts"; +import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; +import { Errors } from "../types/misc/errors.ts"; import { basicShards, sendWS } from "../ws/shard.ts"; import { SLASH_COMMANDS_NAME_REGEX } from "./constants.ts"; diff --git a/src/ws/shard_manager.ts b/src/ws/shard_manager.ts index 7789baa01..e02a93c21 100644 --- a/src/ws/shard_manager.ts +++ b/src/ws/shard_manager.ts @@ -2,7 +2,7 @@ import { eventHandlers } from "../bot.ts"; import { cache } from "../cache.ts"; import { handlers } from "../handlers/mod.ts"; import { Member } from "../structures/mod.ts"; -import { DiscordGatewayOpcodes } from "../types/mod.ts"; +import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; import { Collection } from "../util/collection.ts"; import { delay } from "../util/utils.ts"; import { createShard, requestGuildMembers } from "./mod.ts";