feat: Running in bun 🚀

This commit is contained in:
Yuzu 2022-07-11 23:10:52 -05:00
parent 025a957a8b
commit 9a298ee927
7 changed files with 35 additions and 8 deletions

View File

@ -60,6 +60,7 @@ export function createShardManager(options: CreateShardManager) {
*/
identify: async function (shardId: number) {
let shard = this.shards.get(shardId);
if (!shard) {
shard = createShard({
...this.createShardOptions,

View File

@ -18,14 +18,14 @@ export function spawnShards(gateway: GatewayManager) {
gateway.prepareBuckets();
// SPREAD THIS OUT TO DIFFERENT WORKERS TO BEGIN STARTING UP
gateway.buckets.forEach(async (bucket, bucketId) => {
gateway.buckets.forEach((bucket, bucketId) => {
// gateway.debug("GW DEBUG", `2. Running forEach loop in spawnShards function.`);
for (const worker of bucket.workers) {
// gateway.debug("GW DEBUG", `3. Running for of loop in spawnShards function.`);
for (const shardId of worker.queue) {
await gateway.tellWorkerToIdentify(worker.id, shardId, bucketId);
gateway.tellWorkerToIdentify(worker.id, shardId, bucketId).catch(console.error);
}
}
});

View File

@ -59,7 +59,7 @@ export function createShard(
compress: options.gatewayConfig.compress ?? false,
intents: options.gatewayConfig.intents ?? 0,
properties: {
os: options.gatewayConfig?.properties?.os ?? Deno.build.os,
os: options.gatewayConfig?.properties?.os ?? "linux",
browser: options.gatewayConfig?.properties?.browser ?? "Discordeno",
device: options.gatewayConfig?.properties?.device ?? "Discordeno",
},

View File

@ -14,7 +14,6 @@ export async function handleMessage(shard: Shard, message_: MessageEvent<any>):
// Discord might send zlib compressed payloads.
if (shard.gatewayConfig.compress && message instanceof Blob) {
message = decoder.decode(decompressWith(new Uint8Array(await message.arrayBuffer())));
console.log(message);
}
// Safeguard incase decompression failed to make a string.

View File

@ -24,6 +24,7 @@ export async function runMethod<T = any>(
);
const errorStack = new Error("Location:");
// @ts-ignore Breaks deno deploy. Luca said add ts-ignore until it's fixed
Error.captureStackTrace?.(errorStack);

View File

@ -21,14 +21,18 @@ export async function sendRequest<T>(rest: RestManager, options: RestSendRequest
// CUSTOM HANDLER FOR USER TO LOG OR WHATEVER WHENEVER A FETCH IS MADE
rest.debug(`[REST - fetching] URL: ${options.url} | ${JSON.stringify(options)}`);
const newURL = options.url.startsWith(BASE_URL) ? options.url : `${BASE_URL}/v${rest.version}/${options.url}`;
rest.debug(`[REST - url data] URL: ${newURL}`);
const response = await fetch(
options.url.startsWith(BASE_URL) ? options.url : `${BASE_URL}/v${rest.version}/${options.url}`,
{
new Request(newURL, {
method: options.method,
headers: options.payload?.headers,
body: options.payload?.body,
},
}),
);
rest.debug(`[REST - fetched] URL: ${options.url} | ${JSON.stringify(options)}`);
const bucketIdFromHeaders = rest.processRequestHeaders(

22
tests/bun.js Normal file
View File

@ -0,0 +1,22 @@
import { GatewayIntents, Session } from "./deps.ts";
const token = "lol";
const intents = GatewayIntents.MessageContent | GatewayIntents.Guilds | GatewayIntents.GuildMessages;
const session = new Session({ token, intents });
// uncomment to debug stuff
// session.on("debug", (any) => {
// console.debug(any);
// })
session.on("ready", (payload) => {
console.log("Logged in as:", payload.user.username);
});
session.on("messageCreate", (message) => {
if (message.content.startsWith("ping")) {
message.reply({ content: "pong!" }).catch((err) => console.error(err));
}
});
await session.start();