From 72b07574f20197af08b43f7cbea06fcb32670d9d Mon Sep 17 00:00:00 2001 From: ayntee Date: Sat, 27 Mar 2021 12:37:11 +0400 Subject: [PATCH] types: add gateway types --- .../commands/APPLICATION_COMMAND_CREATE.ts | 8 +- .../commands/APPLICATION_COMMAND_DELETE.ts | 8 +- .../commands/APPLICATION_COMMAND_UPDATE.ts | 8 +- .../integrations/INTEGRATION_CREATE.ts | 3 +- .../integrations/INTEGRATION_DELETE.ts | 3 +- .../integrations/INTEGRATION_UPDATE.ts | 3 +- .../interactions/INTERACTION_CREATE.ts | 5 +- src/handlers/invites/INVITE_CREATE.ts | 11 +- src/handlers/invites/INVITE_DELETE.ts | 10 +- src/handlers/misc/PRESENCE_UPDATE.ts | 7 +- src/handlers/misc/TYPING_START.ts | 8 +- src/handlers/misc/USER_UPDATE.ts | 5 +- src/handlers/voice/VOICE_SERVER_UPDATE.ts | 3 +- src/handlers/voice/VOICE_STATE_UPDATE.ts | 7 +- src/handlers/webhooks/WEBHOOKS_UPDATE.ts | 8 +- src/types/gateway.ts | 163 ++++++++++++++++++ 16 files changed, 229 insertions(+), 31 deletions(-) diff --git a/src/handlers/commands/APPLICATION_COMMAND_CREATE.ts b/src/handlers/commands/APPLICATION_COMMAND_CREATE.ts index 52c334962..707780312 100644 --- a/src/handlers/commands/APPLICATION_COMMAND_CREATE.ts +++ b/src/handlers/commands/APPLICATION_COMMAND_CREATE.ts @@ -1,13 +1,17 @@ import { eventHandlers } from "../../bot.ts"; +import { + DiscordApplicationCommandCreateUpdateDelete, + DiscordGatewayPayload, +} from "../../types/gateway.ts"; export function handleApplicationCommandCreate( - data: DiscordPayload, + data: DiscordGatewayPayload, ) { const { guild_id: guildId, application_id: applicationId, ...rest - } = data.d as ApplicationCommandEvent; + } = data.d as DiscordApplicationCommandCreateUpdateDelete; eventHandlers.applicationCommandCreate?.({ ...rest, diff --git a/src/handlers/commands/APPLICATION_COMMAND_DELETE.ts b/src/handlers/commands/APPLICATION_COMMAND_DELETE.ts index aa33e76fa..40f04dc8d 100644 --- a/src/handlers/commands/APPLICATION_COMMAND_DELETE.ts +++ b/src/handlers/commands/APPLICATION_COMMAND_DELETE.ts @@ -1,11 +1,15 @@ import { eventHandlers } from "../../bot.ts"; +import { + DiscordApplicationCommandCreateUpdateDelete, + DiscordGatewayPayload, +} from "../../types/gateway.ts"; -export function handleApplicationCommandDelete(data: DiscordPayload) { +export function handleApplicationCommandDelete(data: DiscordGatewayPayload) { const { application_id: applicationId, guild_id: guildId, ...rest - } = data.d as ApplicationCommandEvent; + } = data.d as DiscordApplicationCommandCreateUpdateDelete; eventHandlers.applicationCommandDelete?.({ ...rest, diff --git a/src/handlers/commands/APPLICATION_COMMAND_UPDATE.ts b/src/handlers/commands/APPLICATION_COMMAND_UPDATE.ts index 7621330c2..e4fd270c8 100644 --- a/src/handlers/commands/APPLICATION_COMMAND_UPDATE.ts +++ b/src/handlers/commands/APPLICATION_COMMAND_UPDATE.ts @@ -1,11 +1,15 @@ import { eventHandlers } from "../../bot.ts"; +import { + DiscordApplicationCommandCreateUpdateDelete, + DiscordGatewayPayload, +} from "../../types/gateway.ts"; -export function handleApplicationCommandUpdate(data: DiscordPayload) { +export function handleApplicationCommandUpdate(data: DiscordGatewayPayload) { const { application_id: applicationId, guild_id: guildId, ...rest - } = data.d as ApplicationCommandEvent; + } = data.d as DiscordApplicationCommandCreateUpdateDelete; eventHandlers.applicationCommandUpdate?.({ ...rest, diff --git a/src/handlers/integrations/INTEGRATION_CREATE.ts b/src/handlers/integrations/INTEGRATION_CREATE.ts index 1f22e832b..a558e7fd7 100644 --- a/src/handlers/integrations/INTEGRATION_CREATE.ts +++ b/src/handlers/integrations/INTEGRATION_CREATE.ts @@ -1,7 +1,8 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; export function handleIntegrationCreate( - data: DiscordPayload, + data: DiscordGatewayPayload, ) { const { guild_id: guildId, diff --git a/src/handlers/integrations/INTEGRATION_DELETE.ts b/src/handlers/integrations/INTEGRATION_DELETE.ts index 129698f51..2df762c06 100644 --- a/src/handlers/integrations/INTEGRATION_DELETE.ts +++ b/src/handlers/integrations/INTEGRATION_DELETE.ts @@ -1,6 +1,7 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export function handleIntegrationDelete(data: DiscordPayload) { +export function handleIntegrationDelete(data: DiscordGatewayPayload) { const { guild_id: guildId, application_id: applicationId, diff --git a/src/handlers/integrations/INTEGRATION_UPDATE.ts b/src/handlers/integrations/INTEGRATION_UPDATE.ts index 743e63b38..12415de80 100644 --- a/src/handlers/integrations/INTEGRATION_UPDATE.ts +++ b/src/handlers/integrations/INTEGRATION_UPDATE.ts @@ -1,6 +1,7 @@ import { eventHandlers } from "../../bot.ts"; +import { DiscordGatewayPayload } from "../../types/gateway.ts"; -export function handleIntegrationUpdate(data: DiscordPayload) { +export function handleIntegrationUpdate(data: DiscordGatewayPayload) { const { enable_emoticons: enableEmoticons, expire_behavior: expireBehavior, diff --git a/src/handlers/interactions/INTERACTION_CREATE.ts b/src/handlers/interactions/INTERACTION_CREATE.ts index e3d83a0b3..4aba548f7 100644 --- a/src/handlers/interactions/INTERACTION_CREATE.ts +++ b/src/handlers/interactions/INTERACTION_CREATE.ts @@ -1,8 +1,9 @@ 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 handleInteractionCreate(data: DiscordPayload) { +export async function handleInteractionCreate(data: DiscordGatewayPayload) { const payload = data.d as InteractionCommandPayload; const memberStruct = await structures.createMemberStruct( payload.member, diff --git a/src/handlers/invites/INVITE_CREATE.ts b/src/handlers/invites/INVITE_CREATE.ts index 692a8897e..bf33433d1 100644 --- a/src/handlers/invites/INVITE_CREATE.ts +++ b/src/handlers/invites/INVITE_CREATE.ts @@ -1,8 +1,11 @@ import { eventHandlers } from "../../bot.ts"; +import { + DiscordGatewayPayload, + DiscordInviteCreate, +} from "../../types/gateway.ts"; -export function handleInviteCreate(payload: DiscordPayload) { - if (payload.t !== "INVITE_CREATE") return; - //TODO: replace with tocamelcase +export function handleInviteCreate(payload: DiscordGatewayPayload) { + // TODO: replace with tocamelcase const { channel_id: channelId, created_at: createdAt, @@ -12,7 +15,7 @@ export function handleInviteCreate(payload: DiscordPayload) { target_user_type: targetUserType, max_uses: maxUses, ...rest - } = payload.d as InviteCreateEvent; + } = payload.d as DiscordInviteCreate; eventHandlers.inviteCreate?.({ ...rest, diff --git a/src/handlers/invites/INVITE_DELETE.ts b/src/handlers/invites/INVITE_DELETE.ts index c38f79573..9d42920a0 100644 --- a/src/handlers/invites/INVITE_DELETE.ts +++ b/src/handlers/invites/INVITE_DELETE.ts @@ -1,13 +1,15 @@ import { eventHandlers } from "../../bot.ts"; +import { + DiscordGatewayPayload, + DiscordInviteDelete, +} from "../../types/gateway.ts"; -export function handleInviteDelete(payload: DiscordPayload) { - if (payload.t !== "INVITE_DELETE") return; - +export function handleInviteDelete(payload: DiscordGatewayPayload) { const { channel_id: channelID, guild_id: guildID, ...rest - } = payload.d as InviteDeleteEvent; + } = payload.d as DiscordInviteDelete; eventHandlers.inviteDelete?.({ ...rest, diff --git a/src/handlers/misc/PRESENCE_UPDATE.ts b/src/handlers/misc/PRESENCE_UPDATE.ts index 6a980e6b6..46a058200 100644 --- a/src/handlers/misc/PRESENCE_UPDATE.ts +++ b/src/handlers/misc/PRESENCE_UPDATE.ts @@ -1,9 +1,12 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; -import { DiscordGatewayPayload } from "../../types/gateway.ts"; +import { + DiscordGatewayPayload, + DiscordPresenceUpdate, +} from "../../types/gateway.ts"; export async function handlePresenceUpdate(data: DiscordGatewayPayload) { - const payload = data.d as PresenceUpdatePayload; + const payload = data.d as DiscordPresenceUpdate; const oldPresence = await cacheHandlers.get("presences", payload.user.id); await cacheHandlers.set("presences", payload.user.id, payload); diff --git a/src/handlers/misc/TYPING_START.ts b/src/handlers/misc/TYPING_START.ts index d60349788..5f1b67a1e 100644 --- a/src/handlers/misc/TYPING_START.ts +++ b/src/handlers/misc/TYPING_START.ts @@ -1,5 +1,9 @@ import { eventHandlers } from "../../bot.ts"; +import { + DiscordGatewayPayload, + DiscordTypingStart, +} from "../../types/gateway.ts"; -export function handleTypingStart(data: DiscordPayload) { - eventHandlers.typingStart?.(data.d as TypingStartPayload); +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 1645cd365..5efcef4b7 100644 --- a/src/handlers/misc/USER_UPDATE.ts +++ b/src/handlers/misc/USER_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 handleUserUpdate(data: DiscordPayload) { - const userData = data.d as UserPayload; +export async function handleUserUpdate(data: DiscordGatewayPayload) { + const userData = data.d as DiscordUser; const member = await cacheHandlers.get("members", userData.id); if (!member) return; diff --git a/src/handlers/voice/VOICE_SERVER_UPDATE.ts b/src/handlers/voice/VOICE_SERVER_UPDATE.ts index 4bb7bb901..87a6ea429 100644 --- a/src/handlers/voice/VOICE_SERVER_UPDATE.ts +++ b/src/handlers/voice/VOICE_SERVER_UPDATE.ts @@ -1,8 +1,9 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordVoiceServerUpdate } from "../../types/gateway.ts"; export async function handleVoiceServerUpdate(data: DiscordPayload) { - const payload = data.d as DiscordVoiceServerUpdateEvent; + const payload = data.d as DiscordVoiceServerUpdate; const guild = await cacheHandlers.get("guilds", payload.guild_id); if (!guild) return; diff --git a/src/handlers/voice/VOICE_STATE_UPDATE.ts b/src/handlers/voice/VOICE_STATE_UPDATE.ts index 9c00749a1..3bef06689 100644 --- a/src/handlers/voice/VOICE_STATE_UPDATE.ts +++ b/src/handlers/voice/VOICE_STATE_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 handleVoiceStateUpdate(data: DiscordPayload) { - const payload = data.d as VoiceStateUpdatePayload; +export async function handleVoiceStateUpdate(data: DiscordGatewayPayload) { + const payload = data.d as DiscordVoiceState; if (!payload.guild_id) return; const guild = await cacheHandlers.get("guilds", payload.guild_id); diff --git a/src/handlers/webhooks/WEBHOOKS_UPDATE.ts b/src/handlers/webhooks/WEBHOOKS_UPDATE.ts index ed4e7272d..8c00f5fd1 100644 --- a/src/handlers/webhooks/WEBHOOKS_UPDATE.ts +++ b/src/handlers/webhooks/WEBHOOKS_UPDATE.ts @@ -1,7 +1,11 @@ import { eventHandlers } from "../../bot.ts"; +import { + DiscordGatewayPayload, + DiscordWebhooksUpdate, +} from "../../types/gateway.ts"; -export function handleWebhooksUpdate(data: DiscordPayload) { - const options = data.d as WebhookUpdatePayload; +export function handleWebhooksUpdate(data: DiscordGatewayPayload) { + const options = data.d as DiscordWebhooksUpdate; eventHandlers.webhooksUpdate?.( options.channel_id, options.guild_id, diff --git a/src/types/gateway.ts b/src/types/gateway.ts index e9b4f7979..bd992bed2 100644 --- a/src/types/gateway.ts +++ b/src/types/gateway.ts @@ -397,6 +397,169 @@ export type DiscordMessageReactionRemoveEmoji = Pick< "channel_id" | "guild_id" | "message_id" | "emoji" >; +/** https://discord.com/developers/docs/topics/gateway#presence-update */ +export interface DiscordPresenceUpdate { + /** The user presence is being updated for */ + user: DiscordUser; + /** id of the guild */ + guild_id: string; + /** Either "idle", "dnd", "online", or "offline" */ + status: "idle" | "dnd" | "online" | "offline"; + /** User's current activities */ + activities: DiscordActivity[]; + /** User's platform-dependent status */ + client_status: DiscordClientStatus; +} + +/** https://discord.com/developers/docs/topics/gateway#client-status-object */ +export interface DiscordClientStatus { + /** The user's status set for an active desktop (Windows, Linux, Mac) application session */ + desktop?: string; + /** The user's status set for an active mobile (iOS, Android) application session */ + mobile?: string; + /** The user's status set for an active web (browser, bot account) application session */ + web?: string; +} + +/** https://discord.com/developers/docs/topics/gateway#activity-object */ +export interface DiscordActivity { + /** The activity's name */ + name: string; + /** Activity type */ + type: DiscordActivityTypes; + /** Stream url, is validated when type is 1 */ + url?: string | null; + /** Unix timestamp of when the activity was added to the user's session */ + created_at: number; + /** Unix timestamps for start and/or end of the game */ + timestamps?: DiscordActivityTimestamps; + /** Application id for the game */ + application_id?: string; + /** What the player is currently doing */ + details?: string | null; + /** The user's current party status */ + state?: string | null; + /** The emoji used for a custom status */ + emoji?: DiscordActivityEmoji | null; + /** Information for the current party of the player */ + party?: DiscordActivityParty; + /** Images for the presence and their hover texts */ + assets?: DiscordActivityAssets; + /** Secrets for Rich Presence joining and spectating */ + secrets?: DiscordActivitySecrets; + /** Whether or not the activity is an instanced game session */ + instance?: boolean; + /** Activity flags `OR`d together, describes what the payload includes */ + flags?: number; +} + +/** https://discord.com/developers/docs/topics/gateway#activity-object-activity-types */ +export enum DiscordActivityTypes { + Game, + Streaming, + Listening, + Custom = 4, + Competing, +} + +/** https://discord.com/developers/docs/topics/gateway#activity-object-activity-timestamps */ +export interface DiscordActivityTimestamps { + /** Unix time (in milliseconds) of when the activity started */ + start?: number; + /** Unix time (in milliseconds) of when the activity ends */ + end?: number; +} + +/** https://discord.com/developers/docs/topics/gateway#activity-object-activity-emoji */ +export interface DiscordActivityEmoji { + /** The name of the emoji */ + name: string; + /** The id of the emoji */ + id?: string; + /** Whether this emoji is animated */ + animated?: boolean; +} + +/** https://discord.com/developers/docs/topics/gateway#activity-object-activity-party */ +export interface DiscordActivityParty { + /** The id of the party */ + id?: string; + /** Used to show the party's current and maximum size */ + size?: [current_size: number, max_size: number]; +} + +/** https://discord.com/developers/docs/topics/gateway#activity-object-activity-assets */ +export interface DiscordActivityAssets { + /** The id for a large asset of the activity, usually a snowflake */ + large_image?: string; + /** Text displayed when hovering over the large image of the activity */ + large_text?: string; + /** The id for a small asset of the activity, usually a snowflake */ + small_image?: string; + /** Text displayed when hovering over the small image of the activity */ + small_text?: string; +} + +/** https://discord.com/developers/docs/topics/gateway#activity-object-activity-secrets */ +export interface DiscordActivitySecrets { + /** The secret for joining a party */ + join?: string; + /** The secret for spectating a game */ + spectate?: string; + /** The secret for a specific instanced match */ + match?: string; +} + +/** https://discord.com/developers/docs/topics/gateway#activity-object-activity-flags */ +export enum ActivityFlags { + INSTANCE = 1 << 0, + JOIN = 1 << 1, + SPECTATE = 1 << 2, + JOIN_REQUEST = 1 << 3, + SYNC = 1 << 4, + PLAY = 1 << 5, +} + +/** https://discord.com/developers/docs/topics/gateway#typing-start */ +export interface DiscordTypingStart { + /** id of the channel */ + channel_id: string; + /** id of the guild */ + guild_id?: string; + /** id of the user */ + user_id: string; + /** Unix time (in seconds) of when the user started typing */ + timestamp: number; + /** The member who started typing if this happened in a guild */ + member?: DiscordMember; +} + +/** https://discord.com/developers/docs/topics/gateway#voice-server-update */ +export interface DiscordVoiceServerUpdate { + /** Voice connection token */ + token: string; + /** The guild this voice server update is for */ + guild_id: string; + /** The voice server host */ + endpoint: string; +} + +/** https://discord.com/developers/docs/topics/gateway#webhooks-update */ +export interface DiscordWebhooksUpdate { + /** id of the guild */ + guild_id: string; + /** id of the channel */ + channel_id: string; +} + +/** https://discord.com/developers/docs/topics/gateway#commands */ +export type DiscordApplicationCommandCreateUpdateDelete = + & DiscordApplicationCommand + & { + /** id of the guild the command is in */ + guild_id: string; + }; + /** 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 */