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;
identifyPayload.token = config.token;
identifyPayload.intents = config.intents.reduce(
(bits, next) =>
(bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next),
0
(
bits,
next,
) => (bits |= typeof next === "string"
? DiscordGatewayIntents[next]
: next),
0,
);
lastShardId = botGatewayData.shards;
identifyPayload.shard = [0, lastShardId];
@@ -90,9 +94,13 @@ export async function startBigBrainBot(data: BigBrainBotConfig) {
}
identifyPayload.intents = data.intents.reduce(
(bits, next) =>
(bits |= typeof next === "string" ? DiscordGatewayIntents[next] : next),
0
(
bits,
next,
) => (bits |= typeof next === "string"
? DiscordGatewayIntents[next]
: next),
0,
);
// PROXY DOESNT NEED US SPAWNING SHARDS
+2 -4
View File
@@ -92,8 +92,7 @@ export async function sendMessage(
}
}
const result =
(await rest.runMethod(
const result = (await rest.runMethod(
"post",
endpoints.CHANNEL_MESSAGES(channelId),
camelKeysToSnakeCase({
@@ -102,8 +101,7 @@ export async function sendMessage(
? {
messageReference: {
...content.messageReference,
failIfNotExists:
content.messageReference.failIfNotExists === true,
failIfNotExists: content.messageReference.failIfNotExists === true,
},
}
: {}),
+8 -7
View File
@@ -36,15 +36,16 @@ export async function processQueue(id: string) {
// EXECUTE THE REQUEST
// IF THIS IS A GET REQUEST, CHANGE THE BODY TO QUERY PARAMETERS
const query =
queuedRequest.request.method.toUpperCase() === "GET" &&
const query = queuedRequest.request.method.toUpperCase() === "GET" &&
queuedRequest.payload.body
? Object.entries(queuedRequest.payload.body)
.map(
([key, value]) =>
`${encodeURIComponent(key)}=${encodeURIComponent(
value as string
)}`
`${encodeURIComponent(key)}=${
encodeURIComponent(
value as string,
)
}`,
)
.join("&")
: "";
@@ -59,13 +60,13 @@ export async function processQueue(id: string) {
try {
const response = await fetch(
urlToUse,
rest.createRequestBody(queuedRequest)
rest.createRequestBody(queuedRequest),
);
rest.eventHandlers.fetched(queuedRequest.payload);
const bucketIdFromHeaders = rest.processRequestHeaders(
queuedRequest.request.url,
response.headers
response.headers,
);
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. */
export function processRequest(
request: ServerRequest,
payload: RunMethodOptions
payload: RunMethodOptions,
) {
const route = request.url.substring(request.url.indexOf("api/"));
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,
bucketId,
url,
+2 -1
View File
@@ -11,7 +11,8 @@ export async function createShard(shardId: number) {
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) => {
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) {
message = decompressWith(message, 0, (slice: Uint8Array) =>
ws.utf8decoder.decode(slice)
message = decompressWith(
message,
0,
(slice: Uint8Array) => ws.utf8decoder.decode(slice),
);
}
@@ -29,7 +31,7 @@ export async function handleOnMessage(message: any, shardId: number) {
case DiscordGatewayOpcodes.Hello:
ws.heartbeat(
shardId,
(messageData.d as DiscordHeartbeat).heartbeat_interval
(messageData.d as DiscordHeartbeat).heartbeat_interval,
);
break;
case DiscordGatewayOpcodes.HeartbeatACK:
+1 -1
View File
@@ -31,7 +31,7 @@ export async function identify(shardId: number, maxShards: number) {
JSON.stringify({
op: DiscordGatewayOpcodes.Identify,
d: { ...ws.identifyPayload, shard: [shardId, maxShards] },
})
}),
);
};