Connectors
Lavalink plays the audio, but Discord only tells your bot where to connect. A connector does three things:
- Sends opcode 4 (join/leave voice) to the right shard.
- Forwards raw gateway events (
VOICE_STATE_UPDATE,VOICE_SERVER_UPDATE,GUILD_CREATE, …) toraya.handleRaw(). - Calls
raya.init(botUserId)once Discord is ready.
const { Raya, Connectors } = require('raya.js');
const raya = new Raya({ nodes, connector: new Connectors.DiscordJS(client), // before client.login()});const raya = new Raya({ nodes, connector: new Connectors.Eris(client), // before client.connect()});const raya = new Raya({ nodes, connector: new Connectors.Oceanic(client), // before client.connect()});const raya = new Raya({ nodes, // Send the opcode 4 payload through the shard that owns the guild send: (guildId, payload) => getShardFor(guildId).send(payload),});
// Forward every raw dispatch packet ({ t, d })gateway.on('dispatch', (packet) => raya.handleRaw(packet));
// Once logged inawait raya.init(botUserId);Writing your own connector
Section titled “Writing your own connector”A connector is any object with attach and send. Add getToken to enable voice channel status without passing a token.
import type { Connector, Raya } from 'raya.js';
class MyConnector implements Connector { constructor(private client: MyClient) {}
attach(raya: Raya) { this.client.on('raw', (packet) => raya.handleRaw(packet)); this.client.once('ready', () => raya.init(this.client.user.id)); }
send(guildId: string, payload) { this.client.shardFor(guildId).send(payload); }
getToken() { return this.client.token; }}