types: add gateway types

This commit is contained in:
ayntee
2021-03-27 12:37:11 +04:00
parent b1512e0334
commit 72b07574f2
16 changed files with 229 additions and 31 deletions

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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,

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);

View File

@@ -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,

View File

@@ -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 */