Discord Player は、JavaScript と TypeScript を使用して Discord 音楽ボットを開発するための堅牢なフレームワークです。これは discord-voip ライブラリの上に構築されており、カスタマイズ可能なツールの包括的なセットを提供しており、市内で最も機能が豊富なフレームワークの 1 つとなっています。
Eris 互換モードは、
VoiceStateUpdate
ハンドラーをサポートしていません。手動で処理する必要があります。
Discord Player には Discord.js 14.0 以降が必要です。ターミナルでnpm list discord.js
を実行して、互換性のあるバージョンがあることを確認してください。以前のバージョンを使用している場合は、更新してください。 discord.js ガイドには、更新プロセスを支援するリソースが提供されています。
$ npm install --save discord-player # main library
$ npm install --save @discord-player/extractor # extractors provider
Discord Player は
@discord-player/extractor
認識し、デフォルトで自動的にロードします。await player.extractors.loadDefault()
を呼び出すだけです。
Discord は opus パケットのみを受け入れるため、opus ライブラリをインストールする必要があります。 Discord Player は、次のような複数の opus ライブラリをサポートしています。
このうち、mediaplex は、単なる libopus インターフェイスよりも多くの機能を discord-player に追加するため、推奨されるライブラリです。以下を実行して、opus ライブラリをインストールできます。
$ npm install --save mediaplex
# or
$ npm install --save @discordjs/opus
# or
$ npm install --save opusscript
# or
$ npm install --save @evan/opus
# or
$ npm install --save node-opus
メディアのトランスコーディングには FFmpeg または Avconv が必要です。 https://ffmpeg.org または npm 経由で入手できます。
npm から取得したバイナリは不安定であることが知られているため、npm 経由で ffmpeg をインストールすることはお勧めしません。公式ソースからインストールすることをお勧めします。
$ npm install --save ffmpeg-static
# or
$ npm install --save @ffmpeg-installer/ffmpeg
# or
$ npm install --save @node-ffmpeg/node-ffmpeg-installer
# or
$ npm install --save ffmpeg-binaries
FFMPEG_PATH
環境変数を使用して、カスタム パスから ffmpeg をロードします。
メインプレーヤーのインスタンスを作成しましょう。このインスタンスは、すべてのキューとそのコンポーネントを処理し、追跡します。
const { Player } = require ( 'discord-player' ) ;
const client = new Discord . Client ( {
// Make sure you have 'GuildVoiceStates' intent enabled
intents : [ 'GuildVoiceStates' /* Other intents */ ]
} ) ;
// this is the entrypoint for discord-player based application
const player = new Player ( client ) ;
// Now, lets load all the default extractors, except 'YouTubeExtractor'. You can remove the filter if you want to include youtube.
await player . extractors . loadDefault ( ( ext ) => ext !== 'YouTubeExtractor' ) ;
Discord Player は主にイベントベースです。コンテキストとアクションに基づいてさまざまなイベントを発行します。トラックの再生が開始されたときにユーザーに通知する基本的なイベント リスナーを追加しましょう。
// this event is emitted whenever discord-player starts to play a track
player . events . on ( 'playerStart' , ( queue , track ) => {
// we will later define queue.metadata object while creating the queue
queue . metadata . channel . send ( `Started playing ** ${ track . cleanTitle } **!` ) ;
} ) ;
Discord Player では Eris のサポートが限定的です。次のコードを使用して、Eris で Discord Player をセットアップできます。
const { Player , createErisCompat } = require ( 'discord-player' ) ;
const player = new Player ( createErisCompat ( client ) ) ;
discord-player のフック ( useMainPlayer
など) を使用する場合は、コマンドを追加する前に必ずコマンドにコンテキストを指定してください。
// execute the command
await command . execute ( interaction ) ;
// execute the command
await player . context . provide ( { guild : interaction . guild } , ( ) => command . execute ( interaction ) ) ;
これにより、discord プレーヤーは現在のギルドとキューを自動的に認識できるようになり、よりクリーンなコードとシームレスな統合が実現します。これにより、プレーヤー インスタンスをコマンドに渡したり、 client.player = player
ようなハックを使用したりする必要がなくなります。
コマンド部分に移りましょう。要件に応じてコマンドを定義できます。コマンド部分のみに焦点を当てます。
const { useMainPlayer } = require ( 'discord-player' ) ;
export async function execute ( interaction ) {
const player = useMainPlayer ( ) ; // get player instance
const channel = interaction . member . voice . channel ;
if ( ! channel ) return interaction . reply ( 'You are not connected to a voice channel!' ) ; // make sure we have a voice channel
const query = interaction . options . getString ( 'query' , true ) ; // we need input/query to play
// let's defer the interaction as things can take time to process
await interaction . deferReply ( ) ;
try {
const { track } = await player . play ( channel , query , {
nodeOptions : {
// nodeOptions are the options for guild node (aka your queue in simple word)
metadata : interaction // we can access this metadata object using queue.metadata later on
}
} ) ;
return interaction . followUp ( `** ${ track . cleanTitle } ** enqueued!` ) ;
} catch ( e ) {
// let's return error if something failed
return interaction . followUp ( `Something went wrong: ${ e } ` ) ;
}
}
独自の音楽ボットを構築するのに必要なのはこれだけです。その他の機能については、ドキュメントを参照してください。
オープンソースの音楽ボットやエクストラクターなど、Discord Player コミュニティによって構築されたリソースの厳選されたリストを探索してください。詳細については、https://discord-player.js.org/showcase をご覧ください。