feat(core): implement some ws send events (#8941)

* feat(core): implement some ws send events

* fix: check guild id and add a timeout

* fix: only check for nonce

* chore: update readme

* chore: make requested changes

* chore: make methods consistent

* chore: fix readme example

* chore: move shard ID calculation to util

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This commit is contained in:
Suneet Tipirneni
2023-01-05 17:06:50 -05:00
committed by GitHub
parent 3407e1eea3
commit 816aed478e
6 changed files with 182 additions and 75 deletions

View File

@@ -35,7 +35,7 @@ pnpm add @discordjs/core
```ts
import { REST } from '@discordjs/rest';
import { WebSocketManager } from '@discordjs/ws';
import { GatewayIntentBits, InteractionType, MessageFlags, createClient } from '@discordjs/core';
import { GatewayIntentBits, InteractionType, MessageFlags, Client } from '@discordjs/core';
// Create REST and WebSocket managers directly
const rest = new REST({ version: '10' }).setToken(token);
@@ -46,11 +46,11 @@ const ws = new WebSocketManager({
});
// Create a client to emit relevant events.
const client = createClient({ rest, ws });
const client = new Client({ rest, ws });
// Listen for interactions
// Each event contains an `api` prop along with the event data that allows you to interface with the Discord REST API
client.on('interactionCreate', async ({ interaction, api }) => {
client.on(GatewayDispatchEvents.InteractionCreate, async ({ data: interaction, api }) => {
if (!(interaction.type === InteractionType.ApplicationCommand) || interaction.data.name !== 'ping') {
return;
}
@@ -59,7 +59,7 @@ client.on('interactionCreate', async ({ interaction, api }) => {
});
// Listen for the ready event
client.on('ready', () => console.log('Ready!'));
client.once(GatewayDispatchEvents.Ready, () => console.log('Ready!'));
// Start the WebSocket connection.
ws.connect();