@m7mdxzx1/discord-video-strem 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +294 -0
  2. package/dist/client/GatewayEvents.d.ts +27 -0
  3. package/dist/client/GatewayEvents.js +1 -0
  4. package/dist/client/GatewayOpCodes.d.ts +40 -0
  5. package/dist/client/GatewayOpCodes.js +41 -0
  6. package/dist/client/Streamer.d.ts +41 -0
  7. package/dist/client/Streamer.js +189 -0
  8. package/dist/client/encryptor/TransportEncryptor.d.ts +16 -0
  9. package/dist/client/encryptor/TransportEncryptor.js +38 -0
  10. package/dist/client/index.d.ts +4 -0
  11. package/dist/client/index.js +4 -0
  12. package/dist/client/packet/AudioPacketizer.d.ts +8 -0
  13. package/dist/client/packet/AudioPacketizer.js +22 -0
  14. package/dist/client/packet/BaseMediaPacketizer.d.ts +109 -0
  15. package/dist/client/packet/BaseMediaPacketizer.js +243 -0
  16. package/dist/client/packet/VideoPacketizerAnnexB.d.ts +132 -0
  17. package/dist/client/packet/VideoPacketizerAnnexB.js +231 -0
  18. package/dist/client/packet/VideoPacketizerVP8.d.ts +15 -0
  19. package/dist/client/packet/VideoPacketizerVP8.js +56 -0
  20. package/dist/client/packet/index.d.ts +4 -0
  21. package/dist/client/packet/index.js +4 -0
  22. package/dist/client/processing/AnnexBHelper.d.ts +93 -0
  23. package/dist/client/processing/AnnexBHelper.js +132 -0
  24. package/dist/client/voice/BaseMediaConnection.d.ts +118 -0
  25. package/dist/client/voice/BaseMediaConnection.js +319 -0
  26. package/dist/client/voice/MediaUdp.d.ts +26 -0
  27. package/dist/client/voice/MediaUdp.js +140 -0
  28. package/dist/client/voice/StreamConnection.d.ts +10 -0
  29. package/dist/client/voice/StreamConnection.js +30 -0
  30. package/dist/client/voice/VoiceConnection.d.ts +7 -0
  31. package/dist/client/voice/VoiceConnection.js +10 -0
  32. package/dist/client/voice/VoiceMessageTypes.d.ts +136 -0
  33. package/dist/client/voice/VoiceMessageTypes.js +1 -0
  34. package/dist/client/voice/VoiceOpCodes.d.ts +21 -0
  35. package/dist/client/voice/VoiceOpCodes.js +22 -0
  36. package/dist/client/voice/index.d.ts +5 -0
  37. package/dist/client/voice/index.js +5 -0
  38. package/dist/index.d.ts +5 -0
  39. package/dist/index.js +5 -0
  40. package/dist/media/AudioStream.d.ts +25 -0
  41. package/dist/media/AudioStream.js +63 -0
  42. package/dist/media/BaseMediaStream.d.ts +31 -0
  43. package/dist/media/BaseMediaStream.js +145 -0
  44. package/dist/media/LibavCodecId.d.ts +541 -0
  45. package/dist/media/LibavCodecId.js +552 -0
  46. package/dist/media/LibavDecoder.d.ts +5 -0
  47. package/dist/media/LibavDecoder.js +63 -0
  48. package/dist/media/LibavDemuxer.d.ts +23 -0
  49. package/dist/media/LibavDemuxer.js +295 -0
  50. package/dist/media/VideoStream.d.ts +7 -0
  51. package/dist/media/VideoStream.js +10 -0
  52. package/dist/media/index.d.ts +1 -0
  53. package/dist/media/index.js +1 -0
  54. package/dist/media/newApi.d.ts +126 -0
  55. package/dist/media/newApi.js +387 -0
  56. package/dist/media/utils.d.ts +1 -0
  57. package/dist/media/utils.js +4 -0
  58. package/dist/utils.d.ts +28 -0
  59. package/dist/utils.js +54 -0
  60. package/package.json +69 -0
  61. package/src/client/GatewayEvents.ts +41 -0
  62. package/src/client/GatewayOpCodes.ts +40 -0
  63. package/src/client/Streamer.ts +279 -0
  64. package/src/client/encryptor/TransportEncryptor.ts +62 -0
  65. package/src/client/index.ts +4 -0
  66. package/src/client/packet/AudioPacketizer.ts +28 -0
  67. package/src/client/packet/BaseMediaPacketizer.ts +307 -0
  68. package/src/client/packet/VideoPacketizerAnnexB.ts +263 -0
  69. package/src/client/packet/VideoPacketizerVP8.ts +73 -0
  70. package/src/client/packet/index.ts +4 -0
  71. package/src/client/processing/AnnexBHelper.ts +142 -0
  72. package/src/client/voice/BaseMediaConnection.ts +407 -0
  73. package/src/client/voice/MediaUdp.ts +171 -0
  74. package/src/client/voice/StreamConnection.ts +33 -0
  75. package/src/client/voice/VoiceConnection.ts +15 -0
  76. package/src/client/voice/VoiceMessageTypes.ts +164 -0
  77. package/src/client/voice/VoiceOpCodes.ts +21 -0
  78. package/src/client/voice/index.ts +5 -0
  79. package/src/index.ts +5 -0
  80. package/src/media/AudioStream.ts +81 -0
  81. package/src/media/BaseMediaStream.ts +173 -0
  82. package/src/media/LibavCodecId.ts +566 -0
  83. package/src/media/LibavDecoder.ts +82 -0
  84. package/src/media/LibavDemuxer.ts +348 -0
  85. package/src/media/VideoStream.ts +15 -0
  86. package/src/media/index.ts +1 -0
  87. package/src/media/newApi.ts +618 -0
  88. package/src/media/utils.ts +6 -0
  89. package/src/utils.ts +77 -0
package/README.md ADDED
@@ -0,0 +1,294 @@
1
+ # Discord self-bot video
2
+
3
+ Fork: [Discord-video-experiment](https://github.com/mrjvs/Discord-video-experiment)
4
+
5
+ > [!CAUTION]
6
+ > Using any kind of automation programs on your account can result in your account getting permanently banned by Discord. Use at your own risk
7
+
8
+ This project implements the custom Discord UDP protocol for sending media. Since Discord is likely change their custom protocol, this library is subject to break at any point. An effort will be made to keep this library up to date with the latest Discord protocol, but it is not guranteed.
9
+
10
+ For better stability it is recommended to use WebRTC protocol instead since Discord is forced to adhere to spec, which means that the non-signaling portion of the code is guaranteed to work.
11
+
12
+ ## Features
13
+
14
+ - Playing video & audio in a voice channel (`Go Live`, or webcam video)
15
+
16
+ ## Implementation
17
+
18
+ What I implemented and what I did not.
19
+
20
+ ### Video codecs
21
+
22
+ - [X] VP8
23
+ - [ ] VP9
24
+ - [X] H.264
25
+ - [X] H.265
26
+ - [ ] AV1
27
+
28
+ ### Packet types
29
+
30
+ - [X] RTP (sending of realtime data)
31
+ - [ ] RTX (retransmission)
32
+
33
+ ### Connection types
34
+
35
+ - [X] Regular Voice Connection
36
+ - [X] Go live
37
+
38
+ ### Encryption
39
+
40
+ - [X] Transport Encryption
41
+ - [ ] [End-to-end Encryption](https://github.com/dank074/Discord-video-stream/issues/102)
42
+
43
+ ### Extras
44
+
45
+ - [X] Figure out rtp header extensions (discord specific) (discord seems to use one-byte RTP header extension https://www.rfc-editor.org/rfc/rfc8285.html#section-4.2)
46
+
47
+ Extensions supported by Discord (taken from the webrtc sdp exchange)
48
+
49
+ ```
50
+ "a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level"
51
+ "a=extmap:2 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time"
52
+ "a=extmap:3 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01"
53
+ "a=extmap:4 urn:ietf:params:rtp-hdrext:sdes:mid"
54
+ "a=extmap:5 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay"
55
+ "a=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type"
56
+ "a=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-timing"
57
+ "a=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/color-space"
58
+ "a=extmap:10 urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id"
59
+ "a=extmap:11 urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id"
60
+ "a=extmap:13 urn:3gpp:video-orientation"
61
+ "a=extmap:14 urn:ietf:params:rtp-hdrext:toffset"
62
+ ```
63
+
64
+ ## Requirements
65
+
66
+ For full functionality, this library requires an FFmpeg build with `libzmq` enabled. Here is our recommendation:
67
+ - Windows & Linux: [BtbN's FFmpeg Builds](https://github.com/BtbN/FFmpeg-Builds)
68
+ - macOS (Intel): [evermeet.cx](https://evermeet.cx/ffmpeg/)
69
+ - macOS (Apple Silicon): Install from Homebrew
70
+
71
+ ## Usage
72
+
73
+ Install the package, alongside its peer-dependency discord.js-selfbot-v13:
74
+
75
+ ```
76
+ npm install @dank074/discord-video-stream@latest
77
+ npm install discord.js-selfbot-v13@latest
78
+ ```
79
+
80
+ Create a new Streamer, and pass it a selfbot Client
81
+
82
+ ```typescript
83
+ import { Client } from "discord.js-selfbot-v13";
84
+ import { Streamer } from '@dank074/discord-video-stream';
85
+
86
+ const streamer = new Streamer(new Client());
87
+ await streamer.client.login('TOKEN HERE');
88
+
89
+ ```
90
+
91
+ Make client join a voice channel
92
+
93
+ ```typescript
94
+ await streamer.joinVoice("GUILD ID HERE", "CHANNEL ID HERE");
95
+ ```
96
+
97
+ Start sending media
98
+
99
+ ```typescript
100
+ import { prepareStream, playStream, Utils } from "@dank074/discord-video-stream"
101
+ try {
102
+ const { command, output } = prepareStream("DIRECT VIDEO URL OR READABLE STREAM HERE", {
103
+ // Specify either width or height for aspect ratio aware scaling
104
+ // Specify both for stretched output
105
+ height: 1080,
106
+
107
+ // Force frame rate, or leave blank to use source frame rate
108
+ frameRate: 30,
109
+ bitrateVideo: 5000,
110
+ bitrateVideoMax: 7500,
111
+ videoCodec: Utils.normalizeVideoCodec("H264" /* or H265, VP9 */),
112
+ h26xPreset: "veryfast" // or superfast, ultrafast, ...
113
+ });
114
+ command.on("error", (err, stdout, stderr) => {
115
+ // Handle ffmpeg errors here
116
+ });
117
+
118
+ await playStream(output, streamer, {
119
+ type: "go-live" // use "camera" for camera stream
120
+ });
121
+
122
+ console.log("Finished playing video");
123
+ } catch (e) {
124
+ console.log(e);
125
+ }
126
+ ```
127
+
128
+ ## Encoder options available
129
+
130
+ ```typescript
131
+ /**
132
+ * Disable transcoding of the video stream. If specified, all video related
133
+ * options have no effects
134
+ *
135
+ * Only use this if your video stream is Discord streaming friendly, otherwise
136
+ * you'll get a glitchy output
137
+ */
138
+ noTranscoding?: boolean;
139
+ /**
140
+ * Video output width
141
+ */
142
+ width?: number;
143
+ /**
144
+ * Video output height
145
+ */
146
+ height?: number;
147
+ /**
148
+ * Video output frames per second
149
+ */
150
+ fps?: number;
151
+ /**
152
+ * Video average bitrate in kbps
153
+ */
154
+ bitrateVideo?: number;
155
+ /**
156
+ * Video max bitrate in kbps
157
+ */
158
+ bitrateVideoMax?: number;
159
+ /**
160
+ * Audio bitrate in kbps
161
+ */
162
+ bitrateAudio?: number;
163
+ /**
164
+ * Enable audio output
165
+ */
166
+ includeAudio?: boolean;
167
+ /**
168
+ * Enables hardware accelerated video decoding. Enabling this option might result in an exception
169
+ * being thrown by Ffmpeg process if your system does not support hardware acceleration
170
+ */
171
+ hardwareAcceleratedDecoding?: boolean;
172
+ /**
173
+ * Output video codec. **Only** supports H264, H265, and VP8 currently
174
+ */
175
+ videoCodec?: SupportedVideoCodec;
176
+ /**
177
+ * Encoding preset for H264 or H265. The faster it is, the lower the quality
178
+ */
179
+ h26xPreset?: 'ultrafast' | 'superfast' | 'veryfast' | 'faster' | 'fast' | 'medium' | 'slow' | 'slower' | 'veryslow';
180
+ /**
181
+ * Adds ffmpeg params to minimize latency and start outputting video as fast as possible.
182
+ * Might create lag in video output in some rare cases
183
+ */
184
+ minimizeLatency?: boolean;
185
+ /**
186
+ * Custom headers for HTTP requests
187
+ */
188
+ customHeaders?: Record<string, string>;
189
+ /**
190
+ * Custom ffmpeg flags/options to pass directly to ffmpeg
191
+ * These will be added to the command after other options
192
+ */
193
+ customFfmpegFlags?: string[];
194
+ ```
195
+
196
+ ## `playStream` options available
197
+
198
+ ```typescript
199
+ /**
200
+ * Set stream type as "Go Live" or camera stream
201
+ */
202
+ type?: "go-live" | "camera",
203
+
204
+ /**
205
+ * Override video width sent to Discord.
206
+ *
207
+ * DO NOT SPECIFY UNLESS YOU KNOW WHAT YOU'RE DOING!
208
+ */
209
+ width?: number,
210
+
211
+ /**
212
+ * Override video height sent to Discord.
213
+ *
214
+ * DO NOT SPECIFY UNLESS YOU KNOW WHAT YOU'RE DOING!
215
+ */
216
+ height?: number,
217
+
218
+ /**
219
+ * Override video frame rate sent to Discord.
220
+ *
221
+ * DO NOT SPECIFY UNLESS YOU KNOW WHAT YOU'RE DOING!
222
+ */
223
+ frameRate?: number,
224
+
225
+ /**
226
+ * Same as ffmpeg's `readrate_initial_burst` command line flag
227
+ *
228
+ * See https://ffmpeg.org/ffmpeg.html#:~:text=%2Dreadrate_initial_burst
229
+ */
230
+ readrateInitialBurst?: number,
231
+ ```
232
+
233
+ ## Streamer options available
234
+
235
+ These control internal operations of the library, and can be changed through the `opts` property on the `Streamer` class. You probably shouldn't change it without a good reason
236
+
237
+ ```typescript
238
+ /**
239
+ * Enables sending RTCP sender reports. Helps the receiver synchronize the
240
+ * audio/video frames, except in some weird cases which is why you can disable it
241
+ */
242
+ rtcpSenderReportEnabled?: boolean;
243
+ /**
244
+ * ChaCha20-Poly1305 Encryption is faster than AES-256-GCM, except when using AES-NI
245
+ */
246
+ forceChacha20Encryption?: boolean;
247
+ ```
248
+
249
+ ## Performance tips
250
+
251
+ See [this page](./PERFORMANCE.md) for some tips on improving performance
252
+
253
+ ## Running example
254
+
255
+ `examples/basic/src/config.json`:
256
+
257
+ ```json
258
+ "token": "SELF TOKEN HERE",
259
+ "acceptedAuthors": ["USER_ID_HERE"],
260
+ ```
261
+
262
+ 1. Configure your `config.json` with your accepted authors ids, and your self token
263
+ 2. Generate js files with ```npm run build```
264
+ 3. Start program with: ```npm run start```
265
+ 4. Join a voice channel
266
+ 5. Start streaming with commands:
267
+
268
+ for go-live
269
+
270
+ ```
271
+ $play-live <Direct video link>
272
+ ```
273
+
274
+ or for cam
275
+
276
+ ```
277
+ $play-cam <Direct video link>
278
+ ```
279
+
280
+ for example:
281
+
282
+ ```
283
+ $play-live http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4
284
+ ```
285
+
286
+ ## FAQS
287
+
288
+ - Can I stream on existing voice connection (CAM) and in a go-live connection simultaneously?
289
+
290
+ Yes, just send the media packets over both udp connections. The voice gateway expects you to signal when a user turns on their camera, so make sure you signal using `client.signalVideo(guildId, channelId, true)` before you start sending cam media packets.
291
+
292
+ - Does this library work with bot tokens?
293
+
294
+ No, Discord blocks video from bots which is why this library uses a selfbot library as peer dependency. You must use a user token
@@ -0,0 +1,27 @@
1
+ type GatewayEventGeneric<Type extends string = string, Data = unknown> = {
2
+ t: Type;
3
+ d: Data;
4
+ };
5
+ export declare namespace GatewayEvent {
6
+ type VoiceStateUpdate = GatewayEventGeneric<"VOICE_STATE_UPDATE", {
7
+ user_id: string;
8
+ session_id: string;
9
+ }>;
10
+ type VoiceServerUpdate = GatewayEventGeneric<"VOICE_SERVER_UPDATE", {
11
+ guild_id: string;
12
+ channel_id?: string;
13
+ endpoint: string;
14
+ token: string;
15
+ }>;
16
+ type StreamCreate = GatewayEventGeneric<"STREAM_CREATE", {
17
+ stream_key: string;
18
+ rtc_server_id: string;
19
+ }>;
20
+ type StreamServerUpdate = GatewayEventGeneric<"STREAM_SERVER_UPDATE", {
21
+ stream_key: string;
22
+ endpoint: string;
23
+ token: string;
24
+ }>;
25
+ }
26
+ export type GatewayEvent = GatewayEvent.VoiceStateUpdate | GatewayEvent.VoiceServerUpdate | GatewayEvent.StreamCreate | GatewayEvent.StreamServerUpdate;
27
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,40 @@
1
+ export declare enum GatewayOpCodes {
2
+ DISPATCH = 0,
3
+ HEARTBEAT = 1,
4
+ IDENTIFY = 2,
5
+ PRESENCE_UPDATE = 3,
6
+ VOICE_STATE_UPDATE = 4,
7
+ VOICE_SERVER_PING = 5,
8
+ RESUME = 6,
9
+ RECONNECT = 7,
10
+ REQUEST_GUILD_MEMBERS = 8,
11
+ INVALID_SESSION = 9,
12
+ HELLO = 10,
13
+ HEARTBEAT_ACK = 11,
14
+ CALL_CONNECT = 13,
15
+ GUILD_SUBSCRIPTIONS = 14,
16
+ LOBBY_CONNECT = 15,
17
+ LOBBY_DISCONNECT = 16,
18
+ LOBBY_VOICE_STATES_UPDATE = 17,
19
+ STREAM_CREATE = 18,
20
+ STREAM_DELETE = 19,
21
+ STREAM_WATCH = 20,
22
+ STREAM_PING = 21,
23
+ STREAM_SET_PAUSED = 22,
24
+ REQUEST_GUILD_APPLICATION_COMMANDS = 24,
25
+ EMBEDDED_ACTIVITY_LAUNCH = 25,
26
+ EMBEDDED_ACTIVITY_CLOSE = 26,
27
+ EMBEDDED_ACTIVITY_UPDATE = 27,
28
+ REQUEST_FORUM_UNREADS = 28,
29
+ REMOTE_COMMAND = 29,
30
+ GET_DELETED_ENTITY_IDS_NOT_MATCHING_HASH = 30,
31
+ REQUEST_SOUNDBOARD_SOUNDS = 31,
32
+ SPEED_TEST_CREATE = 32,
33
+ SPEED_TEST_DELETE = 33,
34
+ REQUEST_LAST_MESSAGES = 34,
35
+ SEARCH_RECENT_MEMBERS = 35,
36
+ REQUEST_CHANNEL_STATUSES = 36,
37
+ GUILD_SUBSCRIPTIONS_BULK = 37,
38
+ GUILD_CHANNELS_RESYNC = 38,
39
+ REQUEST_CHANNEL_MEMBER_COUNT = 39
40
+ }
@@ -0,0 +1,41 @@
1
+ export var GatewayOpCodes;
2
+ (function (GatewayOpCodes) {
3
+ GatewayOpCodes[GatewayOpCodes["DISPATCH"] = 0] = "DISPATCH";
4
+ GatewayOpCodes[GatewayOpCodes["HEARTBEAT"] = 1] = "HEARTBEAT";
5
+ GatewayOpCodes[GatewayOpCodes["IDENTIFY"] = 2] = "IDENTIFY";
6
+ GatewayOpCodes[GatewayOpCodes["PRESENCE_UPDATE"] = 3] = "PRESENCE_UPDATE";
7
+ GatewayOpCodes[GatewayOpCodes["VOICE_STATE_UPDATE"] = 4] = "VOICE_STATE_UPDATE";
8
+ GatewayOpCodes[GatewayOpCodes["VOICE_SERVER_PING"] = 5] = "VOICE_SERVER_PING";
9
+ GatewayOpCodes[GatewayOpCodes["RESUME"] = 6] = "RESUME";
10
+ GatewayOpCodes[GatewayOpCodes["RECONNECT"] = 7] = "RECONNECT";
11
+ GatewayOpCodes[GatewayOpCodes["REQUEST_GUILD_MEMBERS"] = 8] = "REQUEST_GUILD_MEMBERS";
12
+ GatewayOpCodes[GatewayOpCodes["INVALID_SESSION"] = 9] = "INVALID_SESSION";
13
+ GatewayOpCodes[GatewayOpCodes["HELLO"] = 10] = "HELLO";
14
+ GatewayOpCodes[GatewayOpCodes["HEARTBEAT_ACK"] = 11] = "HEARTBEAT_ACK";
15
+ GatewayOpCodes[GatewayOpCodes["CALL_CONNECT"] = 13] = "CALL_CONNECT";
16
+ GatewayOpCodes[GatewayOpCodes["GUILD_SUBSCRIPTIONS"] = 14] = "GUILD_SUBSCRIPTIONS";
17
+ GatewayOpCodes[GatewayOpCodes["LOBBY_CONNECT"] = 15] = "LOBBY_CONNECT";
18
+ GatewayOpCodes[GatewayOpCodes["LOBBY_DISCONNECT"] = 16] = "LOBBY_DISCONNECT";
19
+ GatewayOpCodes[GatewayOpCodes["LOBBY_VOICE_STATES_UPDATE"] = 17] = "LOBBY_VOICE_STATES_UPDATE";
20
+ GatewayOpCodes[GatewayOpCodes["STREAM_CREATE"] = 18] = "STREAM_CREATE";
21
+ GatewayOpCodes[GatewayOpCodes["STREAM_DELETE"] = 19] = "STREAM_DELETE";
22
+ GatewayOpCodes[GatewayOpCodes["STREAM_WATCH"] = 20] = "STREAM_WATCH";
23
+ GatewayOpCodes[GatewayOpCodes["STREAM_PING"] = 21] = "STREAM_PING";
24
+ GatewayOpCodes[GatewayOpCodes["STREAM_SET_PAUSED"] = 22] = "STREAM_SET_PAUSED";
25
+ GatewayOpCodes[GatewayOpCodes["REQUEST_GUILD_APPLICATION_COMMANDS"] = 24] = "REQUEST_GUILD_APPLICATION_COMMANDS";
26
+ GatewayOpCodes[GatewayOpCodes["EMBEDDED_ACTIVITY_LAUNCH"] = 25] = "EMBEDDED_ACTIVITY_LAUNCH";
27
+ GatewayOpCodes[GatewayOpCodes["EMBEDDED_ACTIVITY_CLOSE"] = 26] = "EMBEDDED_ACTIVITY_CLOSE";
28
+ GatewayOpCodes[GatewayOpCodes["EMBEDDED_ACTIVITY_UPDATE"] = 27] = "EMBEDDED_ACTIVITY_UPDATE";
29
+ GatewayOpCodes[GatewayOpCodes["REQUEST_FORUM_UNREADS"] = 28] = "REQUEST_FORUM_UNREADS";
30
+ GatewayOpCodes[GatewayOpCodes["REMOTE_COMMAND"] = 29] = "REMOTE_COMMAND";
31
+ GatewayOpCodes[GatewayOpCodes["GET_DELETED_ENTITY_IDS_NOT_MATCHING_HASH"] = 30] = "GET_DELETED_ENTITY_IDS_NOT_MATCHING_HASH";
32
+ GatewayOpCodes[GatewayOpCodes["REQUEST_SOUNDBOARD_SOUNDS"] = 31] = "REQUEST_SOUNDBOARD_SOUNDS";
33
+ GatewayOpCodes[GatewayOpCodes["SPEED_TEST_CREATE"] = 32] = "SPEED_TEST_CREATE";
34
+ GatewayOpCodes[GatewayOpCodes["SPEED_TEST_DELETE"] = 33] = "SPEED_TEST_DELETE";
35
+ GatewayOpCodes[GatewayOpCodes["REQUEST_LAST_MESSAGES"] = 34] = "REQUEST_LAST_MESSAGES";
36
+ GatewayOpCodes[GatewayOpCodes["SEARCH_RECENT_MEMBERS"] = 35] = "SEARCH_RECENT_MEMBERS";
37
+ GatewayOpCodes[GatewayOpCodes["REQUEST_CHANNEL_STATUSES"] = 36] = "REQUEST_CHANNEL_STATUSES";
38
+ GatewayOpCodes[GatewayOpCodes["GUILD_SUBSCRIPTIONS_BULK"] = 37] = "GUILD_SUBSCRIPTIONS_BULK";
39
+ GatewayOpCodes[GatewayOpCodes["GUILD_CHANNELS_RESYNC"] = 38] = "GUILD_CHANNELS_RESYNC";
40
+ GatewayOpCodes[GatewayOpCodes["REQUEST_CHANNEL_MEMBER_COUNT"] = 39] = "REQUEST_CHANNEL_MEMBER_COUNT";
41
+ })(GatewayOpCodes || (GatewayOpCodes = {}));
@@ -0,0 +1,41 @@
1
+ import { VoiceConnection } from "./voice/VoiceConnection.js";
2
+ import type { Client, DMChannel, GroupDMChannel, VoiceBasedChannel } from 'discord.js-selfbot-v13';
3
+ import type { MediaUdp } from "./voice/MediaUdp.js";
4
+ export type StreamerOptions = {
5
+ /**
6
+ * Force the use of ChaCha20 encryption. Faster on CPUs without AES-NI
7
+ */
8
+ forceChacha20Encryption: boolean;
9
+ /**
10
+ * Enable RTCP Sender Report for synchronization
11
+ */
12
+ rtcpSenderReportEnabled: boolean;
13
+ };
14
+ export declare class Streamer {
15
+ private _voiceConnection?;
16
+ private _client;
17
+ private _opts;
18
+ private _gatewayEmitter;
19
+ constructor(client: Client, opts?: Partial<StreamerOptions>);
20
+ get client(): Client;
21
+ get opts(): StreamerOptions;
22
+ get voiceConnection(): VoiceConnection | undefined;
23
+ sendOpcode(code: number, data: unknown): void;
24
+ joinVoiceChannel(channel: DMChannel | GroupDMChannel | VoiceBasedChannel): Promise<MediaUdp>;
25
+ /**
26
+ * Joins a voice channel and returns a MediaUdp object.
27
+ * @param guild_id the guild id of the voice channel. If null, it will join a DM voice channel.
28
+ * @param channel_id the channel id of the voice channel
29
+ * @returns the MediaUdp object
30
+ * @throws Error if the client is not logged in
31
+ */
32
+ joinVoice(guild_id: string | null, channel_id: string): Promise<MediaUdp>;
33
+ createStream(): Promise<MediaUdp>;
34
+ setStreamPreview(image: Buffer): Promise<void>;
35
+ stopStream(): void;
36
+ leaveVoice(): void;
37
+ signalVideo(video_enabled: boolean): void;
38
+ signalStream(): void;
39
+ signalStopStream(): void;
40
+ signalLeaveVoice(): void;
41
+ }
@@ -0,0 +1,189 @@
1
+ import { EventEmitter } from "node:events";
2
+ import { VoiceConnection } from "./voice/VoiceConnection.js";
3
+ import { StreamConnection } from "./voice/StreamConnection.js";
4
+ import { GatewayOpCodes } from "./GatewayOpCodes.js";
5
+ import { generateStreamKey, parseStreamKey } from "../utils.js";
6
+ export class Streamer {
7
+ constructor(client, opts) {
8
+ this._gatewayEmitter = new EventEmitter();
9
+ this._client = client;
10
+ this._opts = {
11
+ forceChacha20Encryption: false,
12
+ rtcpSenderReportEnabled: true,
13
+ ...opts
14
+ };
15
+ //listen for messages
16
+ this.client.on('raw', (packet) => {
17
+ // @ts-expect-error I don't know how to make this work with TypeScript, so whatever
18
+ this._gatewayEmitter.emit(packet.t, packet.d);
19
+ });
20
+ }
21
+ get client() {
22
+ return this._client;
23
+ }
24
+ get opts() {
25
+ return this._opts;
26
+ }
27
+ get voiceConnection() {
28
+ return this._voiceConnection;
29
+ }
30
+ sendOpcode(code, data) {
31
+ this.client.ws.broadcast({
32
+ op: code,
33
+ d: data,
34
+ });
35
+ }
36
+ joinVoiceChannel(channel) {
37
+ let guildId = null;
38
+ if (channel.type === "GUILD_STAGE_VOICE" || channel.type === "GUILD_VOICE") {
39
+ guildId = channel.guildId;
40
+ }
41
+ return this.joinVoice(guildId, channel.id);
42
+ }
43
+ /**
44
+ * Joins a voice channel and returns a MediaUdp object.
45
+ * @param guild_id the guild id of the voice channel. If null, it will join a DM voice channel.
46
+ * @param channel_id the channel id of the voice channel
47
+ * @returns the MediaUdp object
48
+ * @throws Error if the client is not logged in
49
+ */
50
+ joinVoice(guild_id, channel_id) {
51
+ return new Promise((resolve, reject) => {
52
+ if (!this.client.user) {
53
+ reject("Client not logged in");
54
+ return;
55
+ }
56
+ const user_id = this.client.user.id;
57
+ const voiceConn = new VoiceConnection(this, guild_id, user_id, channel_id, (udp) => {
58
+ resolve(udp);
59
+ });
60
+ this._voiceConnection = voiceConn;
61
+ this._gatewayEmitter.on("VOICE_STATE_UPDATE", (d) => {
62
+ if (user_id !== d.user_id)
63
+ return;
64
+ voiceConn.setSession(d.session_id);
65
+ });
66
+ this._gatewayEmitter.on("VOICE_SERVER_UPDATE", (d) => {
67
+ if (guild_id !== d.guild_id)
68
+ return;
69
+ // channel_id is not set for guild voice calls
70
+ if (d.channel_id && (channel_id !== d.channel_id))
71
+ return;
72
+ voiceConn.setTokens(d.endpoint, d.token);
73
+ });
74
+ this.signalVideo(false);
75
+ });
76
+ }
77
+ createStream() {
78
+ return new Promise((resolve, reject) => {
79
+ if (!this.client.user) {
80
+ reject("Client not logged in");
81
+ return;
82
+ }
83
+ if (!this.voiceConnection) {
84
+ reject("cannot start stream without first joining voice channel");
85
+ return;
86
+ }
87
+ this.signalStream();
88
+ const { guildId: clientGuildId, channelId: clientChannelId, session_id } = this.voiceConnection;
89
+ const { id: clientUserId } = this.client.user;
90
+ if (!session_id)
91
+ throw new Error("Session doesn't exist yet");
92
+ const streamConn = new StreamConnection(this, clientGuildId, clientUserId, clientChannelId, (udp) => {
93
+ resolve(udp);
94
+ });
95
+ this.voiceConnection.streamConnection = streamConn;
96
+ this._gatewayEmitter.on("STREAM_CREATE", (d) => {
97
+ const { type, channelId, guildId, userId } = parseStreamKey(d.stream_key);
98
+ if (clientGuildId !== guildId ||
99
+ clientChannelId !== channelId ||
100
+ clientUserId !== userId)
101
+ return;
102
+ streamConn.serverId = d.rtc_server_id;
103
+ streamConn.streamKey = d.stream_key;
104
+ streamConn.setSession(session_id);
105
+ });
106
+ this._gatewayEmitter.on("STREAM_SERVER_UPDATE", (d) => {
107
+ const { type, channelId, guildId, userId } = parseStreamKey(d.stream_key);
108
+ if (clientGuildId !== guildId ||
109
+ clientChannelId !== channelId ||
110
+ clientUserId !== userId)
111
+ return;
112
+ streamConn.setTokens(d.endpoint, d.token);
113
+ });
114
+ });
115
+ }
116
+ async setStreamPreview(image) {
117
+ if (!this.client.token)
118
+ throw new Error("Please login :)");
119
+ if (!this.voiceConnection?.streamConnection?.guildId)
120
+ return;
121
+ const data = `data:image/jpeg;base64,${image.toString("base64")}`;
122
+ const { guildId } = this.voiceConnection.streamConnection;
123
+ const server = await this.client.guilds.fetch(guildId);
124
+ await server.members.me?.voice.postPreview(data);
125
+ }
126
+ stopStream() {
127
+ const stream = this.voiceConnection?.streamConnection;
128
+ if (!stream)
129
+ return;
130
+ stream.stop();
131
+ this.signalStopStream();
132
+ this.voiceConnection.streamConnection = undefined;
133
+ this._gatewayEmitter.removeAllListeners("STREAM_CREATE");
134
+ this._gatewayEmitter.removeAllListeners("STREAM_SERVER_UPDATE");
135
+ }
136
+ leaveVoice() {
137
+ this.voiceConnection?.stop();
138
+ this.signalLeaveVoice();
139
+ this._voiceConnection = undefined;
140
+ this._gatewayEmitter.removeAllListeners("VOICE_STATE_UPDATE");
141
+ this._gatewayEmitter.removeAllListeners("VOICE_SERVER_UPDATE");
142
+ }
143
+ signalVideo(video_enabled) {
144
+ if (!this.voiceConnection)
145
+ return;
146
+ const { guildId: guild_id, channelId: channel_id, } = this.voiceConnection;
147
+ this.sendOpcode(GatewayOpCodes.VOICE_STATE_UPDATE, {
148
+ guild_id: guild_id,
149
+ channel_id,
150
+ self_mute: false,
151
+ self_deaf: true,
152
+ self_video: video_enabled,
153
+ });
154
+ }
155
+ signalStream() {
156
+ if (!this.voiceConnection)
157
+ return;
158
+ const { type, guildId: guild_id, channelId: channel_id, botId: user_id } = this.voiceConnection;
159
+ const streamKey = generateStreamKey(type, guild_id, channel_id, user_id);
160
+ this.sendOpcode(GatewayOpCodes.STREAM_CREATE, {
161
+ type,
162
+ guild_id,
163
+ channel_id,
164
+ preferred_region: null,
165
+ });
166
+ this.sendOpcode(GatewayOpCodes.STREAM_SET_PAUSED, {
167
+ stream_key: streamKey,
168
+ paused: false,
169
+ });
170
+ }
171
+ signalStopStream() {
172
+ if (!this.voiceConnection)
173
+ return;
174
+ const { type, guildId: guild_id, channelId: channel_id, botId: user_id } = this.voiceConnection;
175
+ const streamKey = generateStreamKey(type, guild_id, channel_id, user_id);
176
+ this.sendOpcode(GatewayOpCodes.STREAM_DELETE, {
177
+ stream_key: streamKey
178
+ });
179
+ }
180
+ signalLeaveVoice() {
181
+ this.sendOpcode(GatewayOpCodes.VOICE_STATE_UPDATE, {
182
+ guild_id: null,
183
+ channel_id: null,
184
+ self_mute: true,
185
+ self_deaf: false,
186
+ self_video: false,
187
+ });
188
+ }
189
+ }
@@ -0,0 +1,16 @@
1
+ export interface TransportEncryptor {
2
+ encrypt(plaintext: Buffer, additionalData: Buffer): Promise<[Buffer, Buffer]>;
3
+ }
4
+ export declare class AES256TransportEncryptor implements TransportEncryptor {
5
+ private _nonce;
6
+ private _secretKey;
7
+ constructor(secretKey: Buffer);
8
+ encrypt(plaintext: Buffer, additionalData: Buffer): Promise<[Buffer, Buffer]>;
9
+ }
10
+ export declare class Chacha20TransportEncryptor implements TransportEncryptor {
11
+ private static sodium;
12
+ private _nonce;
13
+ private _secretKey;
14
+ constructor(secretKey: Buffer);
15
+ encrypt(plaintext: Buffer, additionalData: Buffer): Promise<[Buffer, Buffer]>;
16
+ }
@@ -0,0 +1,38 @@
1
+ import sp from "sodium-plus";
2
+ import { max_int32bit } from "../../utils.js";
3
+ const { SodiumPlus } = sp;
4
+ export class AES256TransportEncryptor {
5
+ constructor(secretKey) {
6
+ this._nonce = 0;
7
+ this._secretKey = crypto.subtle.importKey("raw", secretKey, {
8
+ name: "AES-GCM",
9
+ length: 32
10
+ }, false, ["encrypt"]);
11
+ }
12
+ async encrypt(plaintext, additionalData) {
13
+ const nonceBuffer = Buffer.alloc(12);
14
+ nonceBuffer.writeUInt32BE(this._nonce);
15
+ this._nonce = (this._nonce + 1) % max_int32bit;
16
+ const ciphertext = Buffer.from(await crypto.subtle.encrypt({
17
+ name: "AES-GCM",
18
+ iv: nonceBuffer,
19
+ additionalData,
20
+ }, await this._secretKey, plaintext));
21
+ return [ciphertext, nonceBuffer];
22
+ }
23
+ }
24
+ export class Chacha20TransportEncryptor {
25
+ constructor(secretKey) {
26
+ this._nonce = 0;
27
+ this._secretKey = new sp.CryptographyKey(secretKey);
28
+ }
29
+ async encrypt(plaintext, additionalData) {
30
+ const nonceBuffer = Buffer.alloc(24);
31
+ nonceBuffer.writeUInt32BE(this._nonce);
32
+ this._nonce = (this._nonce + 1) % max_int32bit;
33
+ const ciphertext = await Chacha20TransportEncryptor.sodium
34
+ .then(s => s.crypto_aead_xchacha20poly1305_ietf_encrypt(plaintext, nonceBuffer, this._secretKey, additionalData));
35
+ return [ciphertext, nonceBuffer];
36
+ }
37
+ }
38
+ Chacha20TransportEncryptor.sodium = SodiumPlus.auto();