mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-02 17:00:08 +00:00
refactor: resolve promises (#515)
* Adding missing await and updating some deps * Adding missing await and updating some deps * Adding missing await and updating some deps * Fix close code 4009 until deno fixes the issue: https://github.com/denoland/deno/pull/8776 * Fix heartbeating * Add await for the requestGuildMembers in requestAllMembers * Change body && body.file to body?.file * Fix lint #1 * Change body && body.file to body?.file * Fix lint * Deno lint * Update request.ts * Fix deno lint error * Update src/ws/shard_manager.ts Co-authored-by: ayntee <ayyantee@gmail.com> * Fix fetchMembers * Fix getMembersByQuery * Try to fix RequestMembersQueue processing * Deno lint * Fix requestGuildMembers * Fix requestGuildMembers * Fix requestAllMembers * Undo useless changes * Fix merge conflict * Fix merge conflict * Change for loop to Promise.all * Deno fmt Co-authored-by: ayntee <ayyantee@gmail.com> Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
This commit is contained in:
@@ -314,7 +314,7 @@ export async function getMembersByQuery(
|
||||
if (!guild) return;
|
||||
|
||||
return new Promise((resolve) => {
|
||||
requestAllMembers(guild, resolve, { query: name, limit });
|
||||
return requestAllMembers(guild, resolve, { query: name, limit });
|
||||
}) as Promise<Collection<string, Member>>;
|
||||
}
|
||||
|
||||
@@ -391,7 +391,7 @@ export function emojiURL(id: string, animated = false) {
|
||||
|
||||
/**
|
||||
* Returns a list of emojis for the given guild.
|
||||
*
|
||||
*
|
||||
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis
|
||||
*/
|
||||
export async function getEmojis(guildID: string, addToCache = true) {
|
||||
@@ -411,7 +411,7 @@ export async function getEmojis(guildID: string, addToCache = true) {
|
||||
|
||||
/**
|
||||
* Returns an emoji for the given guild and emoji ID.
|
||||
*
|
||||
*
|
||||
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis
|
||||
*/
|
||||
export async function getEmoji(
|
||||
@@ -586,7 +586,7 @@ export function fetchMembers(guild: Guild, options?: FetchMembersOptions) {
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
requestAllMembers(guild, resolve, options);
|
||||
return requestAllMembers(guild, resolve, options);
|
||||
}) as Promise<Collection<string, Member>>;
|
||||
}
|
||||
|
||||
@@ -953,8 +953,8 @@ export async function getTemplate(templateCode: string) {
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the guild template if it exists
|
||||
/**
|
||||
* Returns the guild template if it exists
|
||||
* @deprecated will get removed in v11 use `getTemplate` instead
|
||||
*/
|
||||
export function getGuildTemplate(
|
||||
|
||||
@@ -59,7 +59,7 @@ export async function startBot(config: BotConfig) {
|
||||
);
|
||||
identifyPayload.shard = [0, botGatewayData.shards];
|
||||
|
||||
spawnShards(botGatewayData, identifyPayload, 0, botGatewayData.shards);
|
||||
await spawnShards(botGatewayData, identifyPayload, 0, botGatewayData.shards);
|
||||
}
|
||||
|
||||
/** Allows you to dynamically update the event handlers by passing in new eventHandlers */
|
||||
|
||||
@@ -121,7 +121,7 @@ async function processQueue() {
|
||||
}
|
||||
|
||||
if (Object.keys(pathQueues).length) {
|
||||
await cleanupQueues();
|
||||
cleanupQueues();
|
||||
} else queueInProcess = false;
|
||||
}
|
||||
}
|
||||
@@ -230,7 +230,8 @@ function runMethod(
|
||||
}
|
||||
|
||||
// No proxy so we need to handle all rate limiting and such
|
||||
return new Promise((resolve, reject) => {
|
||||
// deno-lint-ignore no-async-promise-executor
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const callback = async () => {
|
||||
try {
|
||||
const rateLimitResetIn = await checkRatelimits(url);
|
||||
@@ -262,7 +263,7 @@ function runMethod(
|
||||
},
|
||||
);
|
||||
const bucketIDFromHeaders = processHeaders(url, response.headers);
|
||||
handleStatusCode(response, errorStack);
|
||||
await handleStatusCode(response, errorStack);
|
||||
|
||||
// Sometimes Discord returns an empty 204 response that can't be made to JSON.
|
||||
if (response.status === 204) return resolve(undefined);
|
||||
@@ -314,7 +315,7 @@ function runMethod(
|
||||
});
|
||||
if (!queueInProcess) {
|
||||
queueInProcess = true;
|
||||
processQueue();
|
||||
await processQueue();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -336,7 +337,7 @@ async function logErrors(response: Response, errorStack?: unknown) {
|
||||
}
|
||||
}
|
||||
|
||||
function handleStatusCode(response: Response, errorStack?: unknown) {
|
||||
async function handleStatusCode(response: Response, errorStack?: unknown) {
|
||||
const status = response.status;
|
||||
|
||||
if (
|
||||
@@ -346,7 +347,7 @@ function handleStatusCode(response: Response, errorStack?: unknown) {
|
||||
return true;
|
||||
}
|
||||
|
||||
logErrors(response, errorStack);
|
||||
await logErrors(response, errorStack);
|
||||
|
||||
switch (status) {
|
||||
case HttpResponseCode.BadRequest:
|
||||
|
||||
@@ -15,8 +15,9 @@ import {
|
||||
import { BotStatusRequest, 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 Map<number, BasicShard>();
|
||||
const basicShards = new Collection<number, BasicShard>();
|
||||
const heartbeating = new Map<number, boolean>();
|
||||
const utf8decoder = new TextDecoder();
|
||||
const RequestMembersQueue: RequestMemberQueuedRequest[] = [];
|
||||
@@ -75,7 +76,7 @@ export function createShard(
|
||||
});
|
||||
};
|
||||
|
||||
ws.onmessage = ({ data: message }) => {
|
||||
ws.onmessage = async ({ data: message }) => {
|
||||
if (message instanceof ArrayBuffer) {
|
||||
message = new Uint8Array(message);
|
||||
}
|
||||
@@ -94,7 +95,7 @@ export function createShard(
|
||||
switch (messageData.op) {
|
||||
case GatewayOpcode.Hello:
|
||||
if (!heartbeating.has(basicShard.id)) {
|
||||
heartbeat(
|
||||
await heartbeat(
|
||||
basicShard,
|
||||
(messageData.d as DiscordHeartbeatPayload).heartbeat_interval,
|
||||
identifyPayload,
|
||||
@@ -110,7 +111,7 @@ export function createShard(
|
||||
{ type: "gatewayReconnect", data: { shardID: basicShard.id } },
|
||||
);
|
||||
basicShard.needToResume = true;
|
||||
resumeConnection(data, identifyPayload, basicShard.id);
|
||||
await resumeConnection(data, identifyPayload, basicShard.id);
|
||||
break;
|
||||
case GatewayOpcode.InvalidSession:
|
||||
eventHandlers.debug?.(
|
||||
@@ -125,7 +126,7 @@ export function createShard(
|
||||
break;
|
||||
}
|
||||
basicShard.needToResume = true;
|
||||
resumeConnection(data, identifyPayload, basicShard.id);
|
||||
await resumeConnection(data, identifyPayload, basicShard.id);
|
||||
break;
|
||||
default:
|
||||
if (messageData.t === "RESUMED") {
|
||||
@@ -144,13 +145,13 @@ export function createShard(
|
||||
// Update the sequence number if it is present
|
||||
if (messageData.s) basicShard.previousSequenceNumber = messageData.s;
|
||||
|
||||
handleDiscordPayload(messageData, basicShard.id);
|
||||
await handleDiscordPayload(messageData, basicShard.id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ws.onclose = ({ reason, code, wasClean }) => {
|
||||
ws.onclose = async ({ reason, code, wasClean }) => {
|
||||
eventHandlers.debug?.(
|
||||
{
|
||||
type: "wsClose",
|
||||
@@ -168,7 +169,7 @@ export function createShard(
|
||||
createShard(data, identifyPayload, false, shardID);
|
||||
} else {
|
||||
basicShard.needToResume = true;
|
||||
resumeConnection(botGatewayData, identifyPayload, shardID);
|
||||
await resumeConnection(botGatewayData, identifyPayload, shardID);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -271,13 +272,13 @@ async function resumeConnection(
|
||||
|
||||
eventHandlers.debug?.({ type: "gatewayResume", data: { shardID: shard.id } });
|
||||
// Run it once
|
||||
await createShard(data, payload, true, shard.id);
|
||||
createShard(data, payload, true, shard.id);
|
||||
// Then retry every 15 seconds
|
||||
await delay(1000 * 15);
|
||||
if (shard.needToResume) await resumeConnection(data, payload, shardID);
|
||||
}
|
||||
|
||||
export function requestGuildMembers(
|
||||
export async function requestGuildMembers(
|
||||
guildID: string,
|
||||
shardID: number,
|
||||
nonce: string,
|
||||
@@ -304,7 +305,7 @@ export function requestGuildMembers(
|
||||
|
||||
// If its closed add back to queue to redo on resume
|
||||
if (shard?.ws.readyState === WebSocket.CLOSED) {
|
||||
requestGuildMembers(guildID, shardID, nonce, options);
|
||||
await requestGuildMembers(guildID, shardID, nonce, options);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -328,7 +329,7 @@ async function processGatewayQueue() {
|
||||
return;
|
||||
}
|
||||
|
||||
basicShards.forEach((shard) => {
|
||||
await Promise.all(basicShards.map(async (shard) => {
|
||||
const index = RequestMembersQueue.findIndex((q) => q.shardID === shard.id);
|
||||
// 2 events per second is the rate limit.
|
||||
const request = RequestMembersQueue[index];
|
||||
@@ -342,7 +343,7 @@ async function processGatewayQueue() {
|
||||
},
|
||||
},
|
||||
);
|
||||
requestGuildMembers(
|
||||
await requestGuildMembers(
|
||||
request.guildID,
|
||||
request.shardID,
|
||||
request.nonce,
|
||||
@@ -366,7 +367,7 @@ async function processGatewayQueue() {
|
||||
},
|
||||
},
|
||||
);
|
||||
requestGuildMembers(
|
||||
await requestGuildMembers(
|
||||
secondRequest.guildID,
|
||||
secondRequest.shardID,
|
||||
secondRequest.nonce,
|
||||
@@ -377,7 +378,7 @@ async function processGatewayQueue() {
|
||||
RequestMembersQueue.splice(secondIndex, 1);
|
||||
}
|
||||
}
|
||||
});
|
||||
}));
|
||||
|
||||
await delay(1500);
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ export async function spawnShards(
|
||||
data.shards > lastShardID ? data.shards : lastShardID,
|
||||
];
|
||||
// Start The shard
|
||||
await createShard(data, payload, false, shardID);
|
||||
createShard(data, payload, false, shardID);
|
||||
// Spawn next shard
|
||||
await spawnShards(
|
||||
data,
|
||||
@@ -90,7 +90,7 @@ export async function handleDiscordPayload(
|
||||
}
|
||||
}
|
||||
|
||||
export function requestAllMembers(
|
||||
export async function requestAllMembers(
|
||||
guild: Guild,
|
||||
resolve: (
|
||||
value: Collection<string, Member> | PromiseLike<Collection<string, Member>>,
|
||||
@@ -99,7 +99,13 @@ export function requestAllMembers(
|
||||
) {
|
||||
const nonce = `${guild.id}-${Date.now()}`;
|
||||
cache.fetchAllMembersProcessingRequests.set(nonce, resolve);
|
||||
return requestGuildMembers(guild.id, guild.shardID, nonce, options);
|
||||
|
||||
await requestGuildMembers(
|
||||
guild.id,
|
||||
guild.shardID,
|
||||
nonce,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
export function sendGatewayCommand(
|
||||
|
||||
@@ -2,5 +2,5 @@ export {
|
||||
assertArrayIncludes,
|
||||
assertEquals,
|
||||
assertExists,
|
||||
} from "https://deno.land/std@0.85.0/testing/asserts.ts";
|
||||
} from "https://deno.land/std@0.86.0/testing/asserts.ts";
|
||||
export * from "../mod.ts";
|
||||
|
||||
Reference in New Issue
Block a user