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
+2 -4
View File
@@ -92,8 +92,7 @@ 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({
@@ -102,8 +101,7 @@ export async function sendMessage(
? { ? {
messageReference: { messageReference: {
...content.messageReference, ...content.messageReference,
failIfNotExists: failIfNotExists: content.messageReference.failIfNotExists === true,
content.messageReference.failIfNotExists === true,
}, },
} }
: {}), : {}),
+8 -7
View File
@@ -36,15 +36,16 @@ 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( `${encodeURIComponent(key)}=${
value as string encodeURIComponent(
)}` value as string,
)
}`,
) )
.join("&") .join("&")
: ""; : "";
@@ -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) {
+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] },
}) }),
); );
}; };