Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Commit a202497

Browse files
authored
feat!: implement adapters (#66)
* chore(Deps): update typescript and eslint * feat: create DiscordGatewayAdapter interface * feat: signalJoinVoiceChannel now uses adapter * feat: integrate adapter into VoiceConnection * refactor(DiscordGatewayAdapter): abstract class instead of interface * feat(VoiceConnection): adapter drives packets * chore: remove Discord.js client tracking * feat: decouple library from Discord.js * feat(Adapter): redesign adapter * fix: adapter bugs * chore: fix examples for adapter * chore: update example README
1 parent b2b7c81 commit a202497

File tree

11 files changed

+168
-189
lines changed

11 files changed

+168
-189
lines changed

examples/basic/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
AudioPlayerStatus,
2323
VoiceConnectionStatus,
2424
} from '@discordjs/voice';
25+
import { createDiscordJSAdapter } from './adapter';
2526

2627
const player = createAudioPlayer();
2728

@@ -36,7 +37,11 @@ function playSong() {
3637
}
3738

3839
async function connectToChannel(channel: VoiceChannel) {
39-
const connection = joinVoiceChannel(channel);
40+
const connection = joinVoiceChannel({
41+
channelId: channel.id,
42+
guildId: channel.guild.id,
43+
adapterCreator: createDiscordJSAdapter(channel),
44+
});
4045

4146
try {
4247
await entersState(connection, VoiceConnectionStatus.Ready, 30e3);

examples/basic/adapter.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { DiscordGatewayAdapterCreator, DiscordGatewayAdapterLibraryMethods } from '../../';
2+
import { VoiceChannel, Snowflake, Client, Constants } from 'discord.js';
3+
import {
4+
GatewayVoiceServerUpdateDispatchData,
5+
GatewayVoiceStateUpdateDispatchData,
6+
} from 'discord-api-types/v8/gateway';
7+
8+
const adapters = new Map<Snowflake, DiscordGatewayAdapterLibraryMethods>();
9+
const trackedClients = new Set<Client>();
10+
11+
/**
12+
* Tracks a Discord.js client, listening to VOICE_SERVER_UPDATE and VOICE_STATE_UPDATE events.
13+
* @param client The Discord.js Client to track
14+
*/
15+
function trackClient(client: Client) {
16+
if (trackedClients.has(client)) return;
17+
trackedClients.add(client);
18+
client.ws.on(Constants.WSEvents.VOICE_SERVER_UPDATE, (payload: GatewayVoiceServerUpdateDispatchData) => {
19+
adapters.get(payload.guild_id)?.onVoiceServerUpdate(payload);
20+
});
21+
client.ws.on(Constants.WSEvents.VOICE_STATE_UPDATE, (payload: GatewayVoiceStateUpdateDispatchData) => {
22+
if (payload.guild_id && payload.session_id && payload.user_id === client.user?.id) {
23+
adapters.get(payload.guild_id)?.onVoiceStateUpdate(payload);
24+
}
25+
});
26+
}
27+
28+
/**
29+
* Creates an adapter for a Voice Channel
30+
* @param channel The channel to create the adapter for
31+
*/
32+
export function createDiscordJSAdapter(channel: VoiceChannel): DiscordGatewayAdapterCreator {
33+
return (methods) => {
34+
adapters.set(channel.guild.id, methods);
35+
trackClient(channel.client);
36+
return {
37+
sendPayload(data) {
38+
return channel.guild.shard.send(data);
39+
},
40+
destroy() {
41+
return adapters.delete(channel.guild.id);
42+
},
43+
};
44+
};
45+
}

examples/basic/basic-example.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
AudioPlayerStatus,
1111
VoiceConnectionStatus,
1212
} from '@discordjs/voice';
13+
import { createDiscordJSAdapter } from './adapter';
1314

1415
/*
1516
In this example, we are creating a single audio player that plays to a number of
@@ -57,7 +58,11 @@ async function connectToChannel(channel: VoiceChannel) {
5758
to this voice channel, @discordjs/voice will just return the existing connection for
5859
us!
5960
*/
60-
const connection = joinVoiceChannel(channel);
61+
const connection = joinVoiceChannel({
62+
channelId: channel.id,
63+
guildId: channel.guild.id,
64+
adapterCreator: createDiscordJSAdapter(channel),
65+
});
6166

6267
/*
6368
If we're dealing with a connection that isn't yet Ready, we can set a reasonable

package-lock.json

Lines changed: 20 additions & 122 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@
4747
"@typescript-eslint/eslint-plugin": "^4.14.2",
4848
"@typescript-eslint/parser": "^4.14.2",
4949
"discord-api-types": "^0.12.1",
50-
"discord.js": "^12.5.1",
51-
"eslint": "^7.19.0",
50+
"eslint": "^7.20.0",
5251
"eslint-config-marine": "^8.1.0",
5352
"eslint-config-prettier": "^7.2.0",
5453
"eslint-plugin-prettier": "^3.3.1",
5554
"husky": "^4.3.8",
5655
"lint-staged": "^10.5.4",
5756
"prettier": "^2.2.1",
58-
"typescript": "^4.2.0-dev.20210204"
57+
"typescript": "^4.2.2"
5958
},
6059
"husky": {
6160
"hooks": {

0 commit comments

Comments
 (0)