fix: gateway example

This commit is contained in:
Yuzu 2022-09-07 22:58:11 -05:00
parent 16b9ee3bc5
commit 4c058dc2d0

View File

@ -16,53 +16,31 @@ yarn add @biscuitland/ws
## Example (GW proxy) ## Example (GW proxy)
```ts ```ts
import { DefaultWsAdapter } from "@biscuitland/ws"; import { ShardManager } from '@biscuitland/ws';
import { DefaultRestAdapter } from "@biscuitland/rest"; import { DefaultRestAdapter } from '@biscuitland/rest';
import { GATEWAY_BOT, GatewayIntents } from "@biscuitland/api-types"; 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({ const gateway = await rest.get<DiscordGetGatewayBot>('/gateway/bot');
url: "http://localhost:port...",
token: "your token goes here",
version: 10,
});
const config = await restManager.get("/gateway/bot").then(res => ({ // gateway bot code ↓
url: res.url, const ws = new ShardManager({
shards: res.shards, gateway,
sessionStartLimit: { config: { intents, token },
total: res.session_start_limit.total, handleDiscordPayload(shard, payload) {
remaining: res.session_start_limit.remaining, if (payload.t === "READY") {
resetAfter: res.session_start_limit.reset_after, const data = payload.d as DiscordReady;
maxConcurrency: res.session_start_limit.max_concurrency, console.log("logged in as:", data.user.username);
console.log("shard: %d", shard.options.id);
}
}, },
}); });
const wsManager = new DefaultWsAdapter({ await ws.spawns();
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();
``` ```
## Links ## Links