This commit is contained in:
Skillz4Killz
2021-04-08 14:35:33 +00:00
committed by GitHub
parent d016fc3764
commit 2babaf5991
8 changed files with 60 additions and 45 deletions
+14 -6
View File
@@ -41,9 +41,13 @@ export async function startBot(config: BotConfig) {
proxyWSURL = botGatewayData.url; proxyWSURL = botGatewayData.url;
identifyPayload.token = config.token; identifyPayload.token = config.token;
identifyPayload.intents = config.intents.reduce( identifyPayload.intents = config.intents.reduce(
(bits, next) => (
(bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next), bits,
0 next,
) => (bits |= typeof next === "string"
? DiscordGatewayIntents[next]
: next),
0,
); );
lastShardId = botGatewayData.shards; lastShardId = botGatewayData.shards;
identifyPayload.shard = [0, lastShardId]; identifyPayload.shard = [0, lastShardId];
@@ -90,9 +94,13 @@ export async function startBigBrainBot(data: BigBrainBotConfig) {
} }
identifyPayload.intents = data.intents.reduce( identifyPayload.intents = data.intents.reduce(
(bits, next) => (
(bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next), bits,
0 next,
) => (bits |= typeof next === "string"
? DiscordGatewayIntents[next]
: next),
0,
); );
// PROXY DOESNT NEED US SPAWNING SHARDS // PROXY DOESNT NEED US SPAWNING SHARDS
+15 -17
View File
@@ -92,23 +92,21 @@ export async function sendMessage(
} }
} }
const result = const result = (await rest.runMethod(
(await rest.runMethod( "post",
"post", endpoints.CHANNEL_MESSAGES(channelId),
endpoints.CHANNEL_MESSAGES(channelId), camelKeysToSnakeCase({
camelKeysToSnakeCase({ ...content,
...content, ...(content.messageReference?.messageId
...(content.messageReference?.messageId ? {
? { messageReference: {
messageReference: { ...content.messageReference,
...content.messageReference, failIfNotExists: content.messageReference.failIfNotExists === true,
failIfNotExists: },
content.messageReference.failIfNotExists === true, }
}, : {}),
} }),
: {}), )) as DiscordMessage;
}),
)) as DiscordMessage;
return structures.createMessageStruct(result); return structures.createMessageStruct(result);
} }
+16 -15
View File
@@ -36,18 +36,19 @@ export async function processQueue(id: string) {
// EXECUTE THE REQUEST // EXECUTE THE REQUEST
// IF THIS IS A GET REQUEST, CHANGE THE BODY TO QUERY PARAMETERS // IF THIS IS A GET REQUEST, CHANGE THE BODY TO QUERY PARAMETERS
const query = const query = queuedRequest.request.method.toUpperCase() === "GET" &&
queuedRequest.request.method.toUpperCase() === "GET" && queuedRequest.payload.body
queuedRequest.payload.body ? Object.entries(queuedRequest.payload.body)
? Object.entries(queuedRequest.payload.body) .map(
.map( ([key, value]) =>
([key, value]) => `${encodeURIComponent(key)}=${
`${encodeURIComponent(key)}=${encodeURIComponent( encodeURIComponent(
value as string value as string,
)}` )
) }`,
.join("&") )
: ""; .join("&")
: "";
const urlToUse = const urlToUse =
queuedRequest.request.method.toUpperCase() === "GET" && query queuedRequest.request.method.toUpperCase() === "GET" && query
? `${queuedRequest.request.url}?${query}` ? `${queuedRequest.request.url}?${query}`
@@ -59,13 +60,13 @@ export async function processQueue(id: string) {
try { try {
const response = await fetch( const response = await fetch(
urlToUse, urlToUse,
rest.createRequestBody(queuedRequest) rest.createRequestBody(queuedRequest),
); );
rest.eventHandlers.fetched(queuedRequest.payload); rest.eventHandlers.fetched(queuedRequest.payload);
const bucketIdFromHeaders = rest.processRequestHeaders( const bucketIdFromHeaders = rest.processRequestHeaders(
queuedRequest.request.url, queuedRequest.request.url,
response.headers response.headers,
); );
if (response.status < 200 || response.status >= 400) { if (response.status < 200 || response.status >= 400) {
@@ -123,7 +124,7 @@ export async function processQueue(id: string) {
// IF IT HAS MAXED RETRIES SOMETHING SERIOUSLY WRONG. CANCEL OUT. // IF IT HAS MAXED RETRIES SOMETHING SERIOUSLY WRONG. CANCEL OUT.
if ( if (
queuedRequest.payload.retryCount >= queuedRequest.payload.retryCount >=
queuedRequest.options.maxRetryCount queuedRequest.options.maxRetryCount
) { ) {
rest.eventHandlers.retriesMaxed(queuedRequest.payload); rest.eventHandlers.retriesMaxed(queuedRequest.payload);
queuedRequest.request.respond({ queuedRequest.request.respond({
+1 -1
View File
@@ -4,7 +4,7 @@ import { rest } from "./rest.ts";
/** Processes a request and assigns it to a queue or creates a queue if none exists for it. */ /** Processes a request and assigns it to a queue or creates a queue if none exists for it. */
export function processRequest( export function processRequest(
request: ServerRequest, request: ServerRequest,
payload: RunMethodOptions payload: RunMethodOptions,
) { ) {
const route = request.url.substring(request.url.indexOf("api/")); const route = request.url.substring(request.url.indexOf("api/"));
const parts = route.split("/"); const parts = route.split("/");
+6 -1
View File
@@ -140,7 +140,12 @@ export function runMethod<T = any>(
} }
}; };
rest.processRequest({ url, method, respond: (data: { status: number, body?: string; }) => resolve(JSON.parse(data.body || "{}")) }, { rest.processRequest({
url,
method,
respond: (data: { status: number; body?: string }) =>
resolve(JSON.parse(data.body || "{}")),
}, {
callback, callback,
bucketId, bucketId,
url, url,
+2 -1
View File
@@ -11,7 +11,8 @@ export async function createShard(shardId: number) {
ws.log("ERROR", { shardId, error: errorEvent }); ws.log("ERROR", { shardId, error: errorEvent });
}; };
socket.onmessage = ({ data: message }) => ws.handleOnMessage(message, shardId); socket.onmessage = ({ data: message }) =>
ws.handleOnMessage(message, shardId);
socket.onclose = (event) => { socket.onclose = (event) => {
ws.log("CLOSED", { shardId, payload: event }); ws.log("CLOSED", { shardId, payload: event });
+5 -3
View File
@@ -15,8 +15,10 @@ export async function handleOnMessage(message: any, shardId: number) {
} }
if (message instanceof Uint8Array) { if (message instanceof Uint8Array) {
message = decompressWith(message, 0, (slice: Uint8Array) => message = decompressWith(
ws.utf8decoder.decode(slice) message,
0,
(slice: Uint8Array) => ws.utf8decoder.decode(slice),
); );
} }
@@ -29,7 +31,7 @@ export async function handleOnMessage(message: any, shardId: number) {
case DiscordGatewayOpcodes.Hello: case DiscordGatewayOpcodes.Hello:
ws.heartbeat( ws.heartbeat(
shardId, shardId,
(messageData.d as DiscordHeartbeat).heartbeat_interval (messageData.d as DiscordHeartbeat).heartbeat_interval,
); );
break; break;
case DiscordGatewayOpcodes.HeartbeatACK: case DiscordGatewayOpcodes.HeartbeatACK:
+1 -1
View File
@@ -31,7 +31,7 @@ export async function identify(shardId: number, maxShards: number) {
JSON.stringify({ JSON.stringify({
op: DiscordGatewayOpcodes.Identify, op: DiscordGatewayOpcodes.Identify,
d: { ...ws.identifyPayload, shard: [shardId, maxShards] }, d: { ...ws.identifyPayload, shard: [shardId, maxShards] },
}) }),
); );
}; };