Custom operations
Add your own typed operations and choose how they dispatch.
You can add operations to the registry. This is rarely needed. Source plugins normally use the built-in operations.
Define an operation
Declare the operation's types, then register its policy on the Player:
import type { Operation } from 'discord-player';
declare module '@discord-player/plugin-host' {
interface OperationMap {
'track.audit': Operation<{ sourceId: string }, { accepted: boolean }, 'first'>;
}
}
player.plugins.defineOperation('track.audit', 'first');Plugins can then handle it, and you can run it:
ctx.on('track.audit', ({ sourceId }) => ({ accepted: sourceId.length > 0 }));
const result = await player.plugins.execute('track.audit', { sourceId: 'abc' });Pass guildId to route an invocation for a guild. Handlers then receive that guild's queue, and guildIds handler options apply:
ctx.on('track.audit', (_, { queue }) => ({ accepted: queue !== null }), { guildIds: [guildId] });
await player.plugins.execute('track.audit', { sourceId: 'abc' }, { guildId });A guildId field in the input is ordinary data and does not route the call.
Define the operation before any plugin registers a handler for it.
Choose a policy
| Policy | Use when |
|---|---|
first | One handler should answer |
collect | You want an answer from every handler |
pipe | Handlers transform a value in turn |
broadcast | Handlers only need to be notified |
Keep data portable
Use JSON-compatible data for inputs and outputs if the result might be saved or sent to another process.
Limits
Custom operations run in the plugin runtime where they are registered. Registering an operation on the bot does not automatically expose or forward it through Remedia. The source-host adapter forwards its supported search, related-track, lyrics, and stream operations through explicitly authorized server routes. A source that must work with a remote audio node should return a server-accessible URL from track.stream, or run on a source host.
Continue reading
v7 extractors