Getting started
2. Add a source
Play direct audio URLs with a small source plugin.
You are reading the v8 docsDev
A source plugin turns a query into tracks and tells the Player where to find the audio. This one accepts direct links to MP3, Ogg, and WAV files.
Create src/direct-media.ts:
import { definePlugin, Track } from 'discord-player';
const audioUrl = /^https?:\/\/.+\.(mp3|ogg|wav)(\?.*)?$/i;
export const directMedia = definePlugin({
id: 'example.direct-media',
setup(ctx) {
// Claim queries that look like audio links.
ctx.on('search.query.match', ({ query }) => (audioUrl.test(query) ? { score: 100 } : null));
// Describe the track for the queue.
ctx.on('search.query.resolve', ({ query }) => ({
playlist: null,
tracks: [
new Track({
title: new URL(query).pathname.split('/').pop() || 'Audio file',
url: query,
}),
],
}));
// Return the audio location when the track plays.
ctx.on('track.stream', ({ track }) => ({ url: track.url }));
},
});How a query becomes audio
| Operation | When it runs | What it returns |
|---|---|---|
search.query.match | A string query is searched | A score, or null when the plugin does not handle the query |
search.query.resolve | The plugin won the match | The tracks to queue |
track.stream | A track is about to play | The URL or file for Remedia to open |
Validate URLs in production
This plugin plays any matching link. Before you accept URLs from users, restrict allowed hosts, check redirects, and block private network addresses.
To support real providers, see source plugins.
Continue reading
3. Play a track