【发布时间】:2020-11-08 21:30:47
【问题描述】:
我正在开发一个 Discord 机器人,我正在尝试找到一种方法让它播放 YouTube 上的整个播放列表或能够单独添加链接。用于此的资产是 Node.js、ESLint、ytdl-core、@discord/opus 和 ffmpeg(来自 this site)。
相关代码如下:
const ytdl = require('ytdl-core');
const { prefix, token } = require('./config.json');
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (message.content === '!play') {
if (message.channel.type !== 'text') return;
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.reply('please join a voice channel first!');
}
voiceChannel.join().then(connection => {
const stream = ytdl('https://www.youtube.com/watch?v=1snEYPg8TXs');
const dispatcher = connection.play(stream);
dispatcher.on('finish', () => voiceChannel.leave());
});
发布的代码没有问题,但正如您所见,它只允许一个 YouTube 视频。我对 ytdl 或任何相关函数一无所知,所以我尝试制作一个 var playlist = [etc.],但它根本不起作用,因为显然它已设置为期待链接。播放列表链接仅在完成动作之前播放第一首歌曲。
有什么建议吗?
【问题讨论】:
标签: node.js audio youtube discord