@ovencord/voice 0.19.0
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.
- package/LICENSE +191 -0
- package/README.md +120 -0
- package/package.json +72 -0
- package/src/DataStore.ts +189 -0
- package/src/VoiceConnection.ts +817 -0
- package/src/audio/AudioPlayer.ts +650 -0
- package/src/audio/AudioPlayerError.ts +19 -0
- package/src/audio/AudioResource.ts +296 -0
- package/src/audio/PlayerSubscription.ts +33 -0
- package/src/audio/TransformerGraph.ts +281 -0
- package/src/audio/index.ts +20 -0
- package/src/index.ts +47 -0
- package/src/joinVoiceChannel.ts +78 -0
- package/src/networking/DAVESession.ts +395 -0
- package/src/networking/Networking.ts +813 -0
- package/src/networking/VoiceUDPSocket.ts +173 -0
- package/src/networking/VoiceWebSocket.ts +236 -0
- package/src/networking/index.ts +4 -0
- package/src/receive/AudioReceiveStream.ts +97 -0
- package/src/receive/SSRCMap.ts +108 -0
- package/src/receive/SpeakingMap.ts +64 -0
- package/src/receive/VoiceReceiver.ts +213 -0
- package/src/receive/index.ts +4 -0
- package/src/util/Secretbox.ts +127 -0
- package/src/util/abortAfter.ts +11 -0
- package/src/util/adapter.ts +52 -0
- package/src/util/demuxProbe.ts +129 -0
- package/src/util/entersState.ts +55 -0
- package/src/util/generateDependencyReport.ts +93 -0
- package/src/util/index.ts +4 -0
- package/src/util/util.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2022 Noel Buechler
|
|
179
|
+
Copyright 2020 Amish Shah
|
|
180
|
+
|
|
181
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
182
|
+
you may not use this file except in compliance with the License.
|
|
183
|
+
You may obtain a copy of the License at
|
|
184
|
+
|
|
185
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
186
|
+
|
|
187
|
+
Unless required by applicable law or agreed to in writing, software
|
|
188
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
189
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
190
|
+
See the License for the specific language governing permissions and
|
|
191
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br />
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
<br />
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://discord.gg/djs"><img src="https://img.shields.io/badge/join_us-on_discord-5865F2?logo=discord&logoColor=white" alt="Discord server" /></a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/@ovencord/voice"><img src="https://img.shields.io/npm/v/@ovencord/voice.svg?maxAge=3600" alt="npm version" /></a>
|
|
10
|
+
<a href="https://www.npmjs.com/package/@ovencord/voice"><img src="https://img.shields.io/npm/dt/@ovencord/voice.svg?maxAge=3600" alt="npm downloads" /></a>
|
|
11
|
+
<a href="https://github.com/ovencord/ovencord/actions"><img src="https://github.com/ovencord/ovencord/actions/workflows/tests.yml/badge.svg" alt="Build status" /></a>
|
|
12
|
+
<a href="https://github.com/ovencord/ovencord/commits/main/packages/voice"><img alt="Last commit." src="https://img.shields.io/github/last-commit/ovencord/ovencord?logo=github&logoColor=ffffff&path=packages%2Fvoice" /></a>
|
|
13
|
+
<a href="https://opencollective.com/discordjs"><img src="https://img.shields.io/opencollective/backers/discordjs?maxAge=3600&logo=opencollective" alt="backers" /></a>
|
|
14
|
+
<a href="https://codecov.io/gh/ovencord/ovencord"><img src="https://codecov.io/gh/ovencord/ovencord/branch/main/graph/badge.svg?precision=2&flag=voice" alt="Code coverage" /></a>
|
|
15
|
+
</p>
|
|
16
|
+
<p>
|
|
17
|
+
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/ovencord/ovencord/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
|
18
|
+
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/ovencord/ovencord/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
|
19
|
+
</p>
|
|
20
|
+
</div>
|
|
21
|
+
|
|
22
|
+
## About
|
|
23
|
+
|
|
24
|
+
`@ovencord/voice` is a TypeScript implementation of the Discord Voice API for Node.js.
|
|
25
|
+
|
|
26
|
+
**Features:**
|
|
27
|
+
|
|
28
|
+
- Send and receive\* audio in Discord voice-based channels
|
|
29
|
+
- A strong focus on reliability and predictable behavior
|
|
30
|
+
- Horizontal scalability and libraries other than [discord.js](https://discord.js.org/) are supported with custom adapters
|
|
31
|
+
- A robust audio processing system that can handle a wide range of audio sources
|
|
32
|
+
|
|
33
|
+
\*_Audio receive is not documented by Discord so stable support is not guaranteed_
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
**Node.js 22.12.0 or newer is required.**
|
|
38
|
+
|
|
39
|
+
```sh
|
|
40
|
+
npm install @ovencord/voice
|
|
41
|
+
yarn add @ovencord/voice
|
|
42
|
+
pnpm add @ovencord/voice
|
|
43
|
+
bun add @ovencord/voice
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Dependencies
|
|
47
|
+
|
|
48
|
+
This library has several optional dependencies to support a variety
|
|
49
|
+
of different platforms. Install one dependency from each of the
|
|
50
|
+
categories shown below. The dependencies are listed in order of
|
|
51
|
+
preference for performance. If you can't install one of the options,
|
|
52
|
+
try installing another.
|
|
53
|
+
|
|
54
|
+
**Encryption Libraries (npm install):**
|
|
55
|
+
|
|
56
|
+
> [!NOTE]
|
|
57
|
+
> You only need to install one of these libraries if your system does not support `aes-256-gcm` (verify by running `require('node:crypto').getCiphers().includes('aes-256-gcm')`).
|
|
58
|
+
|
|
59
|
+
- `sodium-native`: ^3.3.0
|
|
60
|
+
- `sodium`: ^3.0.2
|
|
61
|
+
- `@stablelib/xchacha20poly1305`: ^2.0.0
|
|
62
|
+
- `@noble/ciphers`: ^1.0.0
|
|
63
|
+
- `libsodium-wrappers`: ^0.7.9
|
|
64
|
+
|
|
65
|
+
**DAVE Protocol Libraries (e2ee)**
|
|
66
|
+
|
|
67
|
+
> [!NOTE]
|
|
68
|
+
> At this time, `@snazzah/davey` is the only supported DAVE protocol library in this package, and comes pre-installed. In the future, we may support other libraries once they are created.
|
|
69
|
+
|
|
70
|
+
- `@snazzah/davey`: ^0.1.6
|
|
71
|
+
|
|
72
|
+
**Opus Libraries (npm install):**
|
|
73
|
+
|
|
74
|
+
- `@ovencord/opus`: ^0.4.0
|
|
75
|
+
- `opusscript`: ^0.0.7
|
|
76
|
+
|
|
77
|
+
**FFmpeg:**
|
|
78
|
+
|
|
79
|
+
- [`FFmpeg`](https://ffmpeg.org/) (installed and added to environment)
|
|
80
|
+
- `ffmpeg-static`: ^4.2.7 (npm install)
|
|
81
|
+
|
|
82
|
+
## Examples
|
|
83
|
+
|
|
84
|
+
The [voice-examples][voice-examples] repository contains examples on how to use this package. Feel free to check them out if you need a nudge in the right direction.
|
|
85
|
+
|
|
86
|
+
## Links
|
|
87
|
+
|
|
88
|
+
- [Website][website] ([source][website-source])
|
|
89
|
+
- [Documentation][documentation]
|
|
90
|
+
- [Guide][guide] ([source][guide-source])
|
|
91
|
+
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
|
92
|
+
- [discord.js Discord server][discord]
|
|
93
|
+
- [Discord Developers Discord server][discord-developers]
|
|
94
|
+
- [GitHub][source]
|
|
95
|
+
- [npm][npm]
|
|
96
|
+
- [Related libraries][related-libs]
|
|
97
|
+
|
|
98
|
+
## Contributing
|
|
99
|
+
|
|
100
|
+
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
|
101
|
+
[documentation][documentation].
|
|
102
|
+
See [the contribution guide][contributing] if you'd like to submit a PR.
|
|
103
|
+
|
|
104
|
+
## Help
|
|
105
|
+
|
|
106
|
+
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
|
107
|
+
|
|
108
|
+
[website]: https://discord.js.org
|
|
109
|
+
[website-source]: https://github.com/ovencord/ovencord/tree/main/apps/website
|
|
110
|
+
[documentation]: https://discord.js.org/docs/packages/voice/stable
|
|
111
|
+
[guide]: https://discordjs.guide
|
|
112
|
+
[guide-source]: https://github.com/ovencord/ovencord/tree/main/apps/guide
|
|
113
|
+
[guide-update]: https://discordjs.guide/legacy/additional-info/changes-in-v14
|
|
114
|
+
[discord]: https://discord.gg/djs
|
|
115
|
+
[discord-developers]: https://discord.gg/discord-developers
|
|
116
|
+
[source]: https://github.com/ovencord/ovencord/tree/main/packages/voice
|
|
117
|
+
[npm]: https://www.npmjs.com/package/@ovencord/voice
|
|
118
|
+
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
|
119
|
+
[contributing]: https://github.com/ovencord/ovencord/blob/main/.github/CONTRIBUTING.md
|
|
120
|
+
[voice-examples]: https://github.com/discordjs/voice-examples
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package.json",
|
|
3
|
+
"name": "@ovencord/voice",
|
|
4
|
+
"version": "0.19.0",
|
|
5
|
+
"description": "Implementation of the Discord Voice API for Node.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "bun test",
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"lint": "eslint src"
|
|
10
|
+
},
|
|
11
|
+
"exports": "./src/index.ts",
|
|
12
|
+
"main": "./src/index.ts",
|
|
13
|
+
"module": "./src/index.ts",
|
|
14
|
+
"types": "./src/index.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"contributors": [
|
|
19
|
+
"Ovencord Contributors"
|
|
20
|
+
],
|
|
21
|
+
"license": "Apache-2.0",
|
|
22
|
+
"keywords": [
|
|
23
|
+
"discord",
|
|
24
|
+
"discord.js",
|
|
25
|
+
"audio",
|
|
26
|
+
"voice",
|
|
27
|
+
"streaming"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "https://github.com/ovencord/ovencord.git",
|
|
32
|
+
"directory": "packages/voice"
|
|
33
|
+
},
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/ovencord/ovencord/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://ovencord.dev",
|
|
38
|
+
"funding": "https://github.com/ovencord/ovencord?sponsor",
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@snazzah/davey": "^0.1.8",
|
|
41
|
+
"@types/ws": "^8.18.1",
|
|
42
|
+
"discord-api-types": "^0.38.36",
|
|
43
|
+
"prism-media": "^1.3.5",
|
|
44
|
+
"tslib": "^2.8.1",
|
|
45
|
+
"ws": "^8.18.3"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@ovencord/api-extractor": "workspace:^",
|
|
49
|
+
"@ovencord/opus": "^0.9.0",
|
|
50
|
+
"@ovencord/scripts": "workspace:^",
|
|
51
|
+
"@favware/cliff-jumper": "^6.0.0",
|
|
52
|
+
"@noble/ciphers": "^2.1.1",
|
|
53
|
+
"@vitest/coverage-v8": "^4.0.15",
|
|
54
|
+
"cross-env": "^10.1.0",
|
|
55
|
+
"esbuild-plugin-version-injector": "^1.2.1",
|
|
56
|
+
"eslint": "^9.39.2",
|
|
57
|
+
"eslint-formatter-compact": "^9.0.1",
|
|
58
|
+
"eslint-formatter-pretty": "^7.0.0",
|
|
59
|
+
"prettier": "^3.7.4",
|
|
60
|
+
"typescript": "^5.9.3",
|
|
61
|
+
"vitest": "^4.0.15",
|
|
62
|
+
"vitest-websocket-mock": "^0.5.0",
|
|
63
|
+
"@types/bun": "latest",
|
|
64
|
+
"typescript-eslint": "^8.54.0"
|
|
65
|
+
},
|
|
66
|
+
"engines": {
|
|
67
|
+
"bun": ">=1.0.0"
|
|
68
|
+
},
|
|
69
|
+
"publishConfig": {
|
|
70
|
+
"access": "public"
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/DataStore.ts
ADDED
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { GatewayOpcodes } from 'discord-api-types/v10';
|
|
2
|
+
import type { VoiceConnection } from './VoiceConnection';
|
|
3
|
+
import type { AudioPlayer } from './audio/index';
|
|
4
|
+
|
|
5
|
+
export interface JoinConfig {
|
|
6
|
+
channelId: string | null;
|
|
7
|
+
group: string;
|
|
8
|
+
guildId: string;
|
|
9
|
+
selfDeaf: boolean;
|
|
10
|
+
selfMute: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Sends a voice state update to the main websocket shard of a guild, to indicate joining/leaving/moving across
|
|
15
|
+
* voice channels.
|
|
16
|
+
*
|
|
17
|
+
* @param config - The configuration to use when joining the voice channel
|
|
18
|
+
*/
|
|
19
|
+
export function createJoinVoiceChannelPayload(config: JoinConfig) {
|
|
20
|
+
return {
|
|
21
|
+
op: GatewayOpcodes.VoiceStateUpdate,
|
|
22
|
+
// eslint-disable-next-line id-length
|
|
23
|
+
d: {
|
|
24
|
+
guild_id: config.guildId,
|
|
25
|
+
channel_id: config.channelId,
|
|
26
|
+
self_deaf: config.selfDeaf,
|
|
27
|
+
self_mute: config.selfMute,
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Voice Connections
|
|
33
|
+
const groups = new Map<string, Map<string, VoiceConnection>>();
|
|
34
|
+
groups.set('default', new Map());
|
|
35
|
+
|
|
36
|
+
function getOrCreateGroup(group: string) {
|
|
37
|
+
const existing = groups.get(group);
|
|
38
|
+
if (existing) return existing;
|
|
39
|
+
const map = new Map<string, VoiceConnection>();
|
|
40
|
+
groups.set(group, map);
|
|
41
|
+
return map;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Retrieves the map of group names to maps of voice connections. By default, all voice connections
|
|
46
|
+
* are created under the 'default' group.
|
|
47
|
+
*
|
|
48
|
+
* @returns The group map
|
|
49
|
+
*/
|
|
50
|
+
export function getGroups() {
|
|
51
|
+
return groups;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Retrieves all the voice connections under the 'default' group.
|
|
56
|
+
*
|
|
57
|
+
* @param group - The group to look up
|
|
58
|
+
* @returns The map of voice connections
|
|
59
|
+
*/
|
|
60
|
+
export function getVoiceConnections(group?: 'default'): Map<string, VoiceConnection>;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Retrieves all the voice connections under the given group name.
|
|
64
|
+
*
|
|
65
|
+
* @param group - The group to look up
|
|
66
|
+
* @returns The map of voice connections
|
|
67
|
+
*/
|
|
68
|
+
export function getVoiceConnections(group: string): Map<string, VoiceConnection> | undefined;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Retrieves all the voice connections under the given group name. Defaults to the 'default' group.
|
|
72
|
+
*
|
|
73
|
+
* @param group - The group to look up
|
|
74
|
+
* @returns The map of voice connections
|
|
75
|
+
*/
|
|
76
|
+
export function getVoiceConnections(group = 'default') {
|
|
77
|
+
return groups.get(group);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Finds a voice connection with the given guild id and group. Defaults to the 'default' group.
|
|
82
|
+
*
|
|
83
|
+
* @param guildId - The guild id of the voice connection
|
|
84
|
+
* @param group - the group that the voice connection was registered with
|
|
85
|
+
* @returns The voice connection, if it exists
|
|
86
|
+
*/
|
|
87
|
+
export function getVoiceConnection(guildId: string, group = 'default') {
|
|
88
|
+
return getVoiceConnections(group)?.get(guildId);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function untrackVoiceConnection(voiceConnection: VoiceConnection) {
|
|
92
|
+
return getVoiceConnections(voiceConnection.joinConfig.group)?.delete(voiceConnection.joinConfig.guildId);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function trackVoiceConnection(voiceConnection: VoiceConnection) {
|
|
96
|
+
return getOrCreateGroup(voiceConnection.joinConfig.group).set(voiceConnection.joinConfig.guildId, voiceConnection);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Audio Players
|
|
100
|
+
|
|
101
|
+
// Each audio packet is 20ms long
|
|
102
|
+
const FRAME_LENGTH = 20;
|
|
103
|
+
|
|
104
|
+
let audioCycleInterval: NodeJS.Timeout | undefined;
|
|
105
|
+
let nextTime = -1;
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A list of created audio players that are still active and haven't been destroyed.
|
|
109
|
+
*/
|
|
110
|
+
const audioPlayers: AudioPlayer[] = [];
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Called roughly every 20 milliseconds. Dispatches audio from all players, and then gets the players to prepare
|
|
114
|
+
* the next audio frame.
|
|
115
|
+
*/
|
|
116
|
+
function audioCycleStep() {
|
|
117
|
+
if (nextTime === -1) return;
|
|
118
|
+
|
|
119
|
+
nextTime += FRAME_LENGTH;
|
|
120
|
+
const available = audioPlayers.filter((player) => player.checkPlayable());
|
|
121
|
+
|
|
122
|
+
for (const player of available) {
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
124
|
+
player['_stepDispatch']();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
prepareNextAudioFrame(available);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Recursively gets the players that have been passed as parameters to prepare audio frames that can be played
|
|
132
|
+
* at the start of the next cycle.
|
|
133
|
+
*/
|
|
134
|
+
function prepareNextAudioFrame(players: AudioPlayer[]) {
|
|
135
|
+
const nextPlayer = players.shift();
|
|
136
|
+
|
|
137
|
+
if (!nextPlayer) {
|
|
138
|
+
if (nextTime !== -1) {
|
|
139
|
+
audioCycleInterval = setTimeout(() => audioCycleStep(), Math.max(1, nextTime - Date.now()));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// eslint-disable-next-line @typescript-eslint/dot-notation
|
|
146
|
+
nextPlayer['_stepPrepare']();
|
|
147
|
+
|
|
148
|
+
// setImmediate to avoid long audio player chains blocking other scheduled tasks
|
|
149
|
+
setImmediate(() => prepareNextAudioFrame(players));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Checks whether or not the given audio player is being driven by the data store clock.
|
|
154
|
+
*
|
|
155
|
+
* @param target - The target to test for
|
|
156
|
+
* @returns `true` if it is being tracked, `false` otherwise
|
|
157
|
+
*/
|
|
158
|
+
export function hasAudioPlayer(target: AudioPlayer) {
|
|
159
|
+
return audioPlayers.includes(target);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Adds an audio player to the data store tracking list, if it isn't already there.
|
|
164
|
+
*
|
|
165
|
+
* @param player - The player to track
|
|
166
|
+
*/
|
|
167
|
+
export function addAudioPlayer(player: AudioPlayer) {
|
|
168
|
+
if (hasAudioPlayer(player)) return player;
|
|
169
|
+
audioPlayers.push(player);
|
|
170
|
+
if (audioPlayers.length === 1) {
|
|
171
|
+
nextTime = Date.now();
|
|
172
|
+
setImmediate(() => audioCycleStep());
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return player;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Removes an audio player from the data store tracking list, if it is present there.
|
|
180
|
+
*/
|
|
181
|
+
export function deleteAudioPlayer(player: AudioPlayer) {
|
|
182
|
+
const index = audioPlayers.indexOf(player);
|
|
183
|
+
if (index === -1) return;
|
|
184
|
+
audioPlayers.splice(index, 1);
|
|
185
|
+
if (audioPlayers.length === 0) {
|
|
186
|
+
nextTime = -1;
|
|
187
|
+
if (audioCycleInterval !== undefined) clearTimeout(audioCycleInterval);
|
|
188
|
+
}
|
|
189
|
+
}
|