From 45a32463704830c44c5e3507ea58bd67532a25bc Mon Sep 17 00:00:00 2001 From: ayntee Date: Mon, 8 Mar 2021 19:41:20 +0400 Subject: [PATCH] fix(ws/shard): update status update payload (#559) * fix(ws/shard): update status update payload * :( * Update src/types/discord.ts Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com> * it\'s activitypayload * Update src/types/discord.ts * idk * idk Co-authored-by: ITOH <72305210+itohatweb@users.noreply.github.com> --- src/types/discord.ts | 13 +++++++++++++ src/util/utils.ts | 30 ++++++++++++++++-------------- src/ws/shard.ts | 24 +++--------------------- src/ws/shard_manager.ts | 21 +++------------------ 4 files changed, 35 insertions(+), 53 deletions(-) diff --git a/src/types/discord.ts b/src/types/discord.ts index cf5efb255..b4906e373 100644 --- a/src/types/discord.ts +++ b/src/types/discord.ts @@ -6,6 +6,7 @@ import { } from "./guild.ts"; import { MemberCreatePayload } from "./member.ts"; import { Activity, Application } from "./message.ts"; +import { ActivityPayload } from "./mod.ts"; import { ClientStatusPayload } from "./presence.ts"; export interface DiscordPayload { @@ -363,3 +364,15 @@ export interface InviteDeleteEvent { /** the unique invite code */ code: string; } + +/** https://discord.com/developers/docs/topics/gateway#update-status-gateway-status-update-structure */ +export interface GatewayStatusUpdatePayload { + /** unix time (in milliseconds) of when the client went idle, or null if the client is not idle */ + since: number | null; + /** null, or the user's activities */ + activities: Pick[] | null; + /** the user's new status */ + status: StatusType; + /** whether or not the client is afk */ + afk: boolean; +} diff --git a/src/util/utils.ts b/src/util/utils.ts index 1b2bb9b1f..348cfbd5e 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -1,30 +1,32 @@ import { encode } from "../../deps.ts"; import { + Activity, ActivityType, + GatewayOpcode, + GatewayStatusUpdatePayload, ImageFormats, ImageSize, StatusType, } from "../types/mod.ts"; -import { sendGatewayCommand } from "../ws/shard_manager.ts"; +import { basicShards, sendWS } from "../ws/shard.ts"; export const sleep = (timeout: number) => { return new Promise((resolve) => setTimeout(resolve, timeout)); }; -export interface BotStatusRequest { - status: StatusType; - game: { - name?: string; - type: ActivityType; - }; -} - -export function editBotsStatus( - status: StatusType, - name?: string, - type = ActivityType.Game, +export function editBotStatus( + data: Pick, ) { - sendGatewayCommand("EDIT_BOTS_STATUS", { status, game: { name, type } }); + basicShards.forEach((shard) => { + sendWS({ + op: GatewayOpcode.StatusUpdate, + d: { + since: null, + afk: false, + ...data, + }, + }, shard.id); + }); } export function chooseRandom(array: T[]) { diff --git a/src/ws/shard.ts b/src/ws/shard.ts index 800348e40..f6627d17a 100644 --- a/src/ws/shard.ts +++ b/src/ws/shard.ts @@ -6,14 +6,15 @@ import { DiscordPayload, FetchMembersOptions, GatewayOpcode, + GatewayStatusUpdatePayload, ReadyPayload, } from "../types/mod.ts"; -import { BotStatusRequest, delay } from "../util/utils.ts"; +import { delay } from "../util/utils.ts"; import { decompressWith } from "./deps.ts"; import { handleDiscordPayload } from "./shard_manager.ts"; import { Collection } from "../util/collection.ts"; -const basicShards = new Collection(); +export const basicShards = new Collection(); const heartbeating = new Map(); const utf8decoder = new TextDecoder(); const RequestMembersQueue: RequestMemberQueuedRequest[] = []; @@ -383,25 +384,6 @@ async function processGatewayQueue() { await processGatewayQueue(); } -export function botGatewayStatusRequest(payload: BotStatusRequest) { - basicShards.forEach((shard) => { - sendWS({ - op: GatewayOpcode.StatusUpdate, - d: { - since: null, - game: payload.game.name - ? { - name: payload.game.name, - type: payload.game.type, - } - : null, - status: payload.status, - afk: false, - }, - }, shard.id); - }); -} - /** Enqueues the specified data to be transmitted to the server over the WebSocket connection, */ export function sendWS(payload: DiscordPayload, shardID = 0) { const shard = basicShards.get(shardID); diff --git a/src/ws/shard_manager.ts b/src/ws/shard_manager.ts index b43a8eaa5..f2b244c75 100644 --- a/src/ws/shard_manager.ts +++ b/src/ws/shard_manager.ts @@ -8,15 +8,12 @@ import { DiscordPayload, FetchMembersOptions, GatewayOpcode, + GatewayStatusUpdatePayload, } from "../types/mod.ts"; import { cache } from "../util/cache.ts"; import { Collection } from "../util/collection.ts"; -import { BotStatusRequest, delay } from "../util/utils.ts"; -import { - botGatewayStatusRequest, - createShard, - requestGuildMembers, -} from "./mod.ts"; +import { delay } from "../util/utils.ts"; +import { createShard, requestGuildMembers } from "./mod.ts"; let createNextShard = true; @@ -108,15 +105,3 @@ export async function requestAllMembers( options, ); } - -export function sendGatewayCommand( - type: "EDIT_BOTS_STATUS", - // deno-lint-ignore no-explicit-any - payload: Record, -) { - if (type === "EDIT_BOTS_STATUS") { - botGatewayStatusRequest(payload as BotStatusRequest); - } - - return; -}