discord-player 6.0.0-dev.4 → 6.0.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/README.md +75 -297
- package/dist/index.d.ts +1909 -1722
- package/dist/index.js +305 -63
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +301 -65
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -7,267 +7,123 @@ It provides easy set of customizable tools to develop Discord Music bots.
|
|
|
7
7
|
[](https://npmjs.com/discord-player)
|
|
8
8
|
[](https://androz2091.fr/discord)
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
10
|
+
# Why Discord Player?
|
|
11
|
+
|
|
12
|
+
- Beginner friendly, easy to understand
|
|
13
|
+
- TypeScript support
|
|
14
|
+
- Quick and easy to set up
|
|
15
|
+
- Wide range of player management features
|
|
16
|
+
- 64+ built-in audio filter presets
|
|
17
|
+
- Highly customizable
|
|
18
|
+
- Automatic queue management
|
|
19
|
+
- Query caching support
|
|
20
|
+
- Wide range of extendable sources via Extractors API
|
|
21
|
+
- Object oriented
|
|
22
|
+
- Built in stats tracker
|
|
17
23
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
```sh
|
|
21
|
-
$ npm install --save @discordjs/opus # Native (best performance)
|
|
24
|
+
## Installation
|
|
22
25
|
|
|
23
|
-
|
|
24
|
-
$ npm install --save opusscript # WASM (near native performance)
|
|
25
|
-
```
|
|
26
|
+
## Before you start
|
|
26
27
|
|
|
27
|
-
|
|
28
|
+
Discord Player requires Discord.js 14.0 or higher. PLease make sure you have a compatible version using `npm list discord.js` in your terminal. If you're using an earlier version please update it. The [Discord.JS Guide](https://discordjs.guide/) has resources to help with that.
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
$ npm install --save ytdl-core
|
|
30
|
+
#### Main Library
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
$ npm install
|
|
32
|
+
```bash
|
|
33
|
+
$ npm install discord-player # main library
|
|
34
|
+
$ npm install @discord-player/extractor # extractors provider
|
|
34
35
|
```
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
> Discord Player recognizes `@discord-player/extractor` and loads it automatically by default.
|
|
37
38
|
|
|
38
|
-
|
|
39
|
-
- Node Module (FFMPEG): **[https://npmjs.com/package/ffmpeg-static](https://npmjs.com/package/ffmpeg-static)**
|
|
40
|
-
- Avconv: **[https://libav.org/download](https://libav.org/download)**
|
|
39
|
+
#### Opus Library
|
|
41
40
|
|
|
42
|
-
|
|
41
|
+
Discord Player is a high level framework for Discord VoIP. Discord only accepts opus packets, thus you need to install opus library. You can install any of these:
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
- Lavalink compatible 15 band equalizer 🎚️
|
|
48
|
-
- Digital biquad filters support
|
|
49
|
-
- Digital Signal Processing utilities
|
|
50
|
-
- Lightweight ☁️
|
|
51
|
-
- Custom extractors support 🌌
|
|
52
|
-
- Multiple sources support ✌
|
|
53
|
-
- Play in multiple servers at the same time 🚗
|
|
54
|
-
- Does not inject anything to discord.js or your discord.js client 💉
|
|
55
|
-
- Allows you to have full control over what is going to be streamed 👑
|
|
56
|
-
|
|
57
|
-
## [Documentation](https://discord-player.js.org)
|
|
58
|
-
|
|
59
|
-
## Getting Started
|
|
60
|
-
|
|
61
|
-
First of all, you will need to register slash commands:
|
|
62
|
-
|
|
63
|
-
```js
|
|
64
|
-
const { REST } = require('@discordjs/rest');
|
|
65
|
-
const { Routes, ApplicationCommandOptionType } = require('discord.js');
|
|
66
|
-
|
|
67
|
-
const commands = [
|
|
68
|
-
{
|
|
69
|
-
name: 'play',
|
|
70
|
-
description: 'Plays a song!',
|
|
71
|
-
options: [
|
|
72
|
-
{
|
|
73
|
-
name: 'query',
|
|
74
|
-
type: ApplicationCommandOptionType.String,
|
|
75
|
-
description: 'The song you want to play',
|
|
76
|
-
required: true
|
|
77
|
-
}
|
|
78
|
-
]
|
|
79
|
-
}
|
|
80
|
-
];
|
|
81
|
-
|
|
82
|
-
const rest = new REST({ version: '10' }).setToken('BOT_TOKEN');
|
|
83
|
-
|
|
84
|
-
(async () => {
|
|
85
|
-
try {
|
|
86
|
-
console.log('Started refreshing application [/] commands.');
|
|
87
|
-
|
|
88
|
-
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID), { body: commands });
|
|
89
|
-
|
|
90
|
-
console.log('Successfully reloaded application [/] commands.');
|
|
91
|
-
} catch (error) {
|
|
92
|
-
console.error(error);
|
|
93
|
-
}
|
|
94
|
-
})();
|
|
43
|
+
```bash
|
|
44
|
+
$ npm install @discordjs/opus
|
|
45
|
+
$ npm install opusscript
|
|
95
46
|
```
|
|
96
47
|
|
|
97
|
-
|
|
48
|
+
#### FFmpeg or Avconv
|
|
98
49
|
|
|
99
|
-
|
|
100
|
-
const { Client } = require('discord.js');
|
|
101
|
-
const client = new Discord.Client({
|
|
102
|
-
intents: ['Guilds', 'GuildVoiceStates']
|
|
103
|
-
});
|
|
104
|
-
const { Player } = require('discord-player');
|
|
50
|
+
FFmpeg or Avconv is required for media transcoding. You can get it from [https://ffmpeg.org](https://www.ffmpeg.org/download.html) or by installing it from npm:
|
|
105
51
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// add the start and finish event so when a song will be played this message will be sent
|
|
110
|
-
player.events.on('playerStart', (queue, track) => queue.metadata.channel.send(`🎶 | Now playing **${track.title}**!`));
|
|
111
|
-
player.events.on('playerFinish', (queue, track) => queue.metadata.channel.send(`🎶 | Now playing **${track.title}**!`));
|
|
112
|
-
|
|
113
|
-
client.once('ready', () => {
|
|
114
|
-
console.log("I'm ready !");
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
client.on('interactionCreate', async (interaction) => {
|
|
118
|
-
if (!interaction.isChatInputCommand()) return;
|
|
119
|
-
|
|
120
|
-
// /play track:Despacito
|
|
121
|
-
// will play "Despacito" in the voice channel
|
|
122
|
-
if (interaction.commandName === 'play') {
|
|
123
|
-
const voiceChannel = interaction.member.voice.channelId;
|
|
124
|
-
if (!voiceChannel) return await interaction.reply({ content: 'You are not in a voice channel!', ephemeral: true });
|
|
125
|
-
if (interaction.guild.members.me.voice.channelId && voiceChannel !== interaction.guild.members.me.voice.channelId)
|
|
126
|
-
return await interaction.reply({ content: 'You are not in my voice channel!', ephemeral: true });
|
|
127
|
-
await interaction.deferReply({ ephemeral: true });
|
|
128
|
-
const query = interaction.options.getString('query');
|
|
129
|
-
|
|
130
|
-
try {
|
|
131
|
-
const res = await player.play(voiceChannel, query, {
|
|
132
|
-
nodeOptions: {
|
|
133
|
-
metadata: {
|
|
134
|
-
channel: interaction.channel
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
return await interaction.followUp({ content: `⏱️ | Loading track **${res.track.title}**!` });
|
|
140
|
-
} catch(e) {
|
|
141
|
-
return await interaction.followUp({ content: `Could not play: ${e.message}`, ephemeral: true });
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
client.login('BOT_TOKEN');
|
|
52
|
+
```bash
|
|
53
|
+
$ npm install ffmpeg-static
|
|
147
54
|
```
|
|
148
55
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
Polluting client like this could be a bad idea:
|
|
56
|
+
You can get avconv from [https://libav.org/download](https://libav.org/download).
|
|
152
57
|
|
|
153
|
-
|
|
154
|
-
client.player = player;
|
|
155
|
-
```
|
|
58
|
+
#### Streaming Library
|
|
156
59
|
|
|
157
|
-
|
|
60
|
+
You also need to install streaming library if you want to add support for youtube playback. You can install one of these libraries:
|
|
158
61
|
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
|
|
62
|
+
```bash
|
|
63
|
+
$ npm install ytdl-core
|
|
64
|
+
$ npm install play-dl
|
|
65
|
+
$ npm install @distube/ytdl-core
|
|
162
66
|
```
|
|
163
67
|
|
|
164
|
-
|
|
165
|
-
you want without polluting client.
|
|
166
|
-
|
|
167
|
-
## Supported sources
|
|
168
|
-
|
|
169
|
-
By default, discord-player **does not support anything** (including search operation and streaming). Luckily, discord-player supports the following sources with the help of [@discord-player/extractor](https://npm.im/@discord-player/extractor) which comes pre-installed with discord-player:
|
|
68
|
+
Done with all these? Let's write a simple music bot then.
|
|
170
69
|
|
|
171
|
-
|
|
172
|
-
- Raw attachments (backed by `attachment extractor`)
|
|
173
|
-
- Spotify (backed by `ysa extractor`)
|
|
174
|
-
- Apple Music (backed by `ysa extractor`)
|
|
175
|
-
- YouTube (backed by `ysa extractor`)
|
|
176
|
-
- Vimeo (backed by `vimeo extractor`)
|
|
177
|
-
- Reverbnation (backed by `reverbnation extractor`)
|
|
178
|
-
- SoundCloud (backed by `soundcloud extractor`)
|
|
70
|
+
### Setup
|
|
179
71
|
|
|
180
|
-
|
|
181
|
-
Disabling youtube streaming completely would be as easy as:
|
|
72
|
+
Let's create a master player instance.
|
|
182
73
|
|
|
183
74
|
```js
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
blockStreamFrom: [
|
|
189
|
-
// now your bot will no longer be able to use
|
|
190
|
-
// youtube extractor to play audio even if the track was
|
|
191
|
-
// extracted from youtube
|
|
192
|
-
YouTubeExtractor.identifier
|
|
193
|
-
],
|
|
194
|
-
blockExtractors: [
|
|
195
|
-
// this will block the listed extractors from being
|
|
196
|
-
// able to query metadata (aka search results parsing)
|
|
197
|
-
// This example disables youtube search, spotify bridge
|
|
198
|
-
// and apple music bridge
|
|
199
|
-
YouTubeExtractor.identifier
|
|
200
|
-
]
|
|
75
|
+
const { Player } = require('discord-player');
|
|
76
|
+
const client = new Discord.Client({
|
|
77
|
+
// Make sure you have 'GuildVoiceStates' intent enabled
|
|
78
|
+
intents: ['GuildVoiceStates' /* Other intents */]
|
|
201
79
|
});
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
Likewise, You can also force a specific extractor to resolve your search query. This is useful in some cases where you don't want to use other sources.
|
|
205
|
-
|
|
206
|
-
You can do so by using `ext:<EXTRACTOR_IDENTIFIER>` in `searchEngine` value. Example:
|
|
207
|
-
|
|
208
|
-
```js
|
|
209
|
-
import { SoundCloudExtractor } from '@discord-player/extractor';
|
|
210
80
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
searchEngine: SoundCloudExtractor.identifier
|
|
214
|
-
});
|
|
81
|
+
// this is the entrypoint for discord-player based application
|
|
82
|
+
const player = new Player(client);
|
|
215
83
|
```
|
|
216
84
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
Discord Player provides an **Extractor API** that enables you to use your custom stream extractor with it. Some packages have been made by the community to add new features using this API.
|
|
220
|
-
|
|
221
|
-
## Audio Filters
|
|
222
|
-
|
|
223
|
-
Discord Player supports various audio filters. There are 4 types of audio filters in discord-player.
|
|
85
|
+
> **Did You Know?** _Discord Player is by default a singleton._
|
|
224
86
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
The most common and powerful method is FFmpeg. It supports a lot of audio filters. To set ffmpeg filter, you can do:
|
|
87
|
+
Now, let's add some event listeners:
|
|
228
88
|
|
|
229
89
|
```js
|
|
230
|
-
|
|
90
|
+
// this event is emitted whenever discord-player starts to play a track
|
|
91
|
+
player.events.on('playerStart', (queue, track) => {
|
|
92
|
+
// we will later define queue.metadata object while creating the queue
|
|
93
|
+
queue.metadata.channel.send(`Started playing **${track.title}**!`);
|
|
94
|
+
});
|
|
231
95
|
```
|
|
232
96
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
##### Equalizer
|
|
236
|
-
|
|
237
|
-
This equalizer is very similar to Lavalink's 15 Band Equalizer. To use this, you can do:
|
|
97
|
+
Let's write the command part. You can define the command as you desire. We will only check the command handler part:
|
|
238
98
|
|
|
239
99
|
```js
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
]);
|
|
245
|
-
```
|
|
246
|
-
|
|
247
|
-
There is no delay between filter transition when using equalizer.
|
|
248
|
-
|
|
249
|
-
##### Biquad
|
|
100
|
+
async function execute(interaction) {
|
|
101
|
+
const channel = interaction.message.member.voice.channel;
|
|
102
|
+
if (!channel) return interaction.reply('You are not connected to a voice channel!'); // make sure we have a voice channel
|
|
103
|
+
const query = interaction.options.getString('query', true); // we need input/query to play
|
|
250
104
|
|
|
251
|
-
|
|
105
|
+
// let's defer the interaction as things can take time to process
|
|
106
|
+
await interaction.deferReply();
|
|
252
107
|
|
|
253
|
-
|
|
254
|
-
|
|
108
|
+
try {
|
|
109
|
+
const { track } = await player.play(channel, query, {
|
|
110
|
+
nodeOptions: {
|
|
111
|
+
// nodeOptions are the options for guild node (aka your queue in simple word)
|
|
112
|
+
metadata: interaction // we can access this metadata object using queue.metadata later on
|
|
113
|
+
}
|
|
114
|
+
});
|
|
255
115
|
|
|
256
|
-
|
|
257
|
-
|
|
116
|
+
return interaction.followUp(`**${track.title}** enqueued!`);
|
|
117
|
+
} catch (e) {
|
|
118
|
+
// let's return error if something failed
|
|
119
|
+
return interaction.followUp(`Something went wrong: ${e}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
258
122
|
```
|
|
259
123
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
#### Mini Audio Filters
|
|
124
|
+
That's all it takes to build your own music bot.
|
|
263
125
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
```js
|
|
267
|
-
queue.filters.filters.setFilters(['8D']);
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
There is no delay between filters transition using this filter.
|
|
126
|
+
#### Check out the [Documentation](https://discord-player.js.org) for more info.
|
|
271
127
|
|
|
272
128
|
## Example bots made with Discord Player
|
|
273
129
|
|
|
@@ -282,81 +138,3 @@ These bots are made by the community, they can help you build your own!
|
|
|
282
138
|
- [Music-bot](https://github.com/ZerioDev/Music-bot) by [ZerioDev](https://github.com/ZerioDev)
|
|
283
139
|
- [AtlantaBot](https://github.com/Androz2091/AtlantaBot) by [Androz2091](https://github.com/Androz2091) (**outdated**)
|
|
284
140
|
- [Discord-Music](https://github.com/inhydrox/discord-music) by [inhydrox](https://github.com/inhydrox) (**outdated**)
|
|
285
|
-
|
|
286
|
-
### Use youtube cookies
|
|
287
|
-
|
|
288
|
-
Using youtube cookies helps you to prevent frequent ratelimits.
|
|
289
|
-
|
|
290
|
-
```js
|
|
291
|
-
const player = new Player(client, {
|
|
292
|
-
ytdlOptions: {
|
|
293
|
-
requestOptions: {
|
|
294
|
-
headers: {
|
|
295
|
-
cookie: 'YOUR_YOUTUBE_COOKIE'
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
});
|
|
300
|
-
```
|
|
301
|
-
|
|
302
|
-
> Note: The above option is also passed to `ytdl-core` but not `play-dl`. Follow [this instruction](https://github.com/play-dl/play-dl/blob/1ae7ba8fcea8b93293af5de9e19eca3c2a491804/instructions/README.md) for play-dl config.
|
|
303
|
-
|
|
304
|
-
### Use custom proxies
|
|
305
|
-
|
|
306
|
-
```js
|
|
307
|
-
const HttpsProxyAgent = require('https-proxy-agent');
|
|
308
|
-
|
|
309
|
-
// Remove "user:pass@" if you don't need to authenticate to your proxy.
|
|
310
|
-
const proxy = 'http://user:pass@111.111.111.111:8080';
|
|
311
|
-
const agent = HttpsProxyAgent(proxy);
|
|
312
|
-
|
|
313
|
-
const player = new Player(client, {
|
|
314
|
-
ytdlOptions: {
|
|
315
|
-
requestOptions: { agent }
|
|
316
|
-
}
|
|
317
|
-
});
|
|
318
|
-
```
|
|
319
|
-
|
|
320
|
-
> You may also create a simple proxy server and forward requests through it.
|
|
321
|
-
> See **[https://github.com/http-party/node-http-proxy](https://github.com/http-party/node-http-proxy)** for more info.
|
|
322
|
-
|
|
323
|
-
## Stream Hooks
|
|
324
|
-
|
|
325
|
-
### onBeforeCreateStream
|
|
326
|
-
|
|
327
|
-
Discord Player by default uses registered extractors to stream audio. If you need to override what needs to be streamed, you can use this hook.
|
|
328
|
-
|
|
329
|
-
```js
|
|
330
|
-
const fs = require('fs');
|
|
331
|
-
|
|
332
|
-
// other code
|
|
333
|
-
const queue = player.nodes.create(..., {
|
|
334
|
-
...,
|
|
335
|
-
async onBeforeCreateStream(track, source, _queue) {
|
|
336
|
-
if (track.title === 'some title') {
|
|
337
|
-
return fs.createReadStream('./playThisInstead.mp3');
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
`<GuildQueue>.onBeforeCreateStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
|
|
344
|
-
streams. `source` here will be a track source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and sent to `onAfterCreateStream` hook.
|
|
345
|
-
|
|
346
|
-
### onAfterCreateStream
|
|
347
|
-
|
|
348
|
-
This hook can be used to post-process pcm stream. This is the final step before creating audio resource. Example:
|
|
349
|
-
|
|
350
|
-
```js
|
|
351
|
-
const queue = player.nodes.create(..., {
|
|
352
|
-
...,
|
|
353
|
-
async onAfterCreateStream(pcmStream, queue) {
|
|
354
|
-
// return opus encoded stream
|
|
355
|
-
const encoder = new OpusEncoder();
|
|
356
|
-
return {
|
|
357
|
-
stream: encoder.encode(pcmStream),
|
|
358
|
-
type: StreamType.Opus
|
|
359
|
-
};
|
|
360
|
-
}
|
|
361
|
-
});
|
|
362
|
-
```
|