From 4c058dc2d06b4bf432700114625276b5a20e4274 Mon Sep 17 00:00:00 2001 From: Yuzu Date: Wed, 7 Sep 2022 22:58:11 -0500 Subject: [PATCH] fix: gateway example --- packages/ws/README.md | 62 ++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/packages/ws/README.md b/packages/ws/README.md index 0ced763..d920013 100644 --- a/packages/ws/README.md +++ b/packages/ws/README.md @@ -16,57 +16,35 @@ yarn add @biscuitland/ws ## Example (GW proxy) ```ts -import { DefaultWsAdapter } from "@biscuitland/ws"; -import { DefaultRestAdapter } from "@biscuitland/rest"; -import { GATEWAY_BOT, GatewayIntents } from "@biscuitland/api-types"; +import { ShardManager } from '@biscuitland/ws'; +import { DefaultRestAdapter } from '@biscuitland/rest'; +import { GatewayIntents } from '@biscuitland/api-types'; +import type { DiscordGetGatewayBot, DiscordReady } from '@biscuitland/api-types'; -const intents = GatewayIntents.Guilds | GatewayIntents.GuildMessages | GatewayIntents.MessageContent; +const intents = GatewayIntents.Guilds; +const token = 'your token goes here'; +const rest = new DefaultRestAdapter({ token }); -const restManager = new DefaultRestAdapter({ - url: "http://localhost:port...", - token: "your token goes here", - version: 10, -}); +const gateway = await rest.get('/gateway/bot'); -const config = await restManager.get("/gateway/bot").then(res => ({ - url: res.url, - shards: res.shards, - sessionStartLimit: { - total: res.session_start_limit.total, - remaining: res.session_start_limit.remaining, - resetAfter: res.session_start_limit.reset_after, - maxConcurrency: res.session_start_limit.max_concurrency, +// gateway bot code ↓ +const ws = new ShardManager({ + gateway, + config: { intents, token }, + handleDiscordPayload(shard, payload) { + if (payload.t === "READY") { + const data = payload.d as DiscordReady; + console.log("logged in as:", data.user.username); + console.log("shard: %d", shard.options.id); + } }, }); -const wsManager = new DefaultWsAdapter({ - gatewayConfig: { - token: "your token goes here", - intents: intents, - }, - handleDiscordPayload(shard, data) { - if (!data.t) return; - - await fetch("http://localhost:port...", { - method: "POST", - body: JSON.stringify({ shardId: shard.id, data: data }), - }) - .then(res => res.text()) - .catch(err => null); - }, -}); - -wsManager.options.gatewayBot = config; -wsManager.options.lastShardId = wsManager.options.gatewayBot.shards - 1; -wsManager.options.totalShards = wsManager.options.gatewayBot.shards; -wsManager.agent.options.totalShards = wsManager.options.gatewayBot.shards; - -// start a quick bot -wsManager.shards(); +await ws.spawns(); ``` ## Links * [Website](https://biscuitjs.com/) * [Documentation](https://docs.biscuitjs.com/) * [Discord](https://discord.gg/XNw2RZFzaP) -* [core](https://www.npmjs.com/package/@biscuitland/core) | [api-types](https://www.npmjs.com/package/@biscuitland/api-types) | [cache](https://www.npmjs.com/package/@biscuitland/cache) | [rest](https://www.npmjs.com/package/@biscuitland/rest) | [helpers](https://www.npmjs.com/package/@biscuitland/helpers) \ No newline at end of file +* [core](https://www.npmjs.com/package/@biscuitland/core) | [api-types](https://www.npmjs.com/package/@biscuitland/api-types) | [cache](https://www.npmjs.com/package/@biscuitland/cache) | [rest](https://www.npmjs.com/package/@biscuitland/rest) | [helpers](https://www.npmjs.com/package/@biscuitland/helpers)