mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
chore: consistency/prettier (#3852)
* chore: consistency/prettier * chore: rebase * chore: rebase * chore: include typings * fix: include typings file in prettier lint-staged
This commit is contained in:
@@ -1,20 +1,23 @@
|
||||
# Introduction to Voice
|
||||
|
||||
Voice in discord.js can be used for many things, such as music bots, recording or relaying audio.
|
||||
|
||||
In discord.js, you can use voice by connecting to a `VoiceChannel` to obtain a `VoiceConnection`, where you can start streaming and receiving audio.
|
||||
|
||||
To get started, make sure you have:
|
||||
* FFmpeg - `npm install ffmpeg-static`
|
||||
* an opus encoder, choose one from below:
|
||||
* `npm install @discordjs/opus` (better performance)
|
||||
* `npm install opusscript`
|
||||
* a good network connection
|
||||
|
||||
- FFmpeg - `npm install ffmpeg-static`
|
||||
- an opus encoder, choose one from below:
|
||||
- `npm install @discordjs/opus` (better performance)
|
||||
- `npm install opusscript`
|
||||
- a good network connection
|
||||
|
||||
The preferred opus engine is @discordjs/opus, as it performs significantly better than opusscript. When both are available, discord.js will automatically choose @discordjs/opus.
|
||||
Using opusscript is only recommended for development environments where @discordjs/opus is tough to get working.
|
||||
For production bots, using @discordjs/opus should be considered a necessity, especially if they're going to be running on multiple servers.
|
||||
|
||||
## Joining a voice channel
|
||||
|
||||
The example below reacts to a message and joins the sender's voice channel, catching any errors. This is important
|
||||
as it allows us to obtain a `VoiceConnection` that we can start to stream audio with.
|
||||
|
||||
@@ -41,10 +44,12 @@ client.on('message', async message => {
|
||||
```
|
||||
|
||||
## Streaming to a Voice Channel
|
||||
|
||||
In the previous example, we looked at how to join a voice channel in order to obtain a `VoiceConnection`. Now that we
|
||||
have obtained a voice connection, we can start streaming audio to it.
|
||||
|
||||
### Introduction to playing on voice connections
|
||||
|
||||
The most basic example of playing audio over a connection would be playing a local file:
|
||||
|
||||
```js
|
||||
@@ -70,7 +75,7 @@ We can also pass in options when we first play the stream:
|
||||
|
||||
```js
|
||||
const dispatcher = connection.play('/home/discord/audio.mp3', {
|
||||
volume: 0.5
|
||||
volume: 0.5,
|
||||
});
|
||||
```
|
||||
|
||||
@@ -81,9 +86,7 @@ Discord.js allows you to play a lot of things:
|
||||
```js
|
||||
// ReadableStreams, in this example YouTube audio
|
||||
const ytdl = require('ytdl-core');
|
||||
connection.play(ytdl(
|
||||
'https://www.youtube.com/watch?v=ZlAU_w7-Xp8',
|
||||
{ filter: 'audioonly' }));
|
||||
connection.play(ytdl('https://www.youtube.com/watch?v=ZlAU_w7-Xp8', { filter: 'audioonly' }));
|
||||
|
||||
// Files on the internet
|
||||
connection.play('http://www.sample-videos.com/audio/mp3/wave.mp3');
|
||||
@@ -96,11 +99,11 @@ New to v12 is the ability to play OggOpus and WebmOpus streams with much better
|
||||
|
||||
```js
|
||||
connection.play(fs.createReadStream('./media.webm'), {
|
||||
type: 'webm/opus'
|
||||
type: 'webm/opus',
|
||||
});
|
||||
|
||||
connection.play(fs.createReadStream('./media.ogg'), {
|
||||
type: 'ogg/opus'
|
||||
type: 'ogg/opus',
|
||||
});
|
||||
```
|
||||
|
||||
@@ -119,7 +122,7 @@ broadcast.on('subscribe', dispatcher => {
|
||||
|
||||
broadcast.on('unsubscribe', dispatcher => {
|
||||
console.log('Channel unsubscribed from broadcast :(');
|
||||
})
|
||||
});
|
||||
```
|
||||
|
||||
`broadcast` is an instance of `VoiceBroadcast`, which has the same `play` method you are used to with regular VoiceConnections:
|
||||
@@ -133,4 +136,5 @@ connection.play(broadcast);
|
||||
It's important to note that the `dispatcher` stored above is a `BroadcastDispatcher` - it controls all the dispatcher subscribed to the broadcast, e.g. setting the volume of this dispatcher affects the volume of all subscribers.
|
||||
|
||||
## Voice Receive
|
||||
|
||||
coming soon™
|
||||
|
||||
Reference in New Issue
Block a user