Discord Player 是一个强大的框架,用于使用 JavaScript 和 TypeScript 开发 Discord 音乐机器人。它建立在discord-voip 库之上,并提供一套全面的可定制工具,使其成为功能最丰富的框架之一。
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 支持多个作品库,例如:
其中,mediaplex 是推荐的库,因为它为不和谐播放器添加了更多功能,而不仅仅是 libopus 界面。您可以通过运行以下命令来安装 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 安装 ffmpeg,因为从 npm 提取的二进制文件已知不稳定。建议从官方来源安装。
$ 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 ) ) ;
在添加命令之前,如果您希望使用不和谐播放器的挂钩(例如useMainPlayer
),请确保提供命令的上下文。
// execute the command
await command . execute ( interaction ) ;
// execute the command
await player . context . provide ( { guild : interaction . guild } , ( ) => command . execute ( interaction ) ) ;
这使得不和谐玩家能够自动了解当前的公会和队列,从而实现更清晰的代码和无缝集成。这消除了将玩家实例传递给命令或使用诸如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 了解更多信息。