首页 > 解决方案 > 如何让 Discord Bot 播放 YouTube URL

问题描述

我是新手,我想知道是否有办法让我的机器人播放特定的 YouTube URL,所以当我输入 s1 时,机器人会加入房间并播放该 URL

 if (message.content == "s1") {
     if (!message.member.voice.channel) return message.reply("You have to be in a VoiceChannel");
     message.member.voice.channel.join().then(VoiceConnection => {
         VoiceConnection.play("https://youtu.be/~~~~").on("finish", () => 
         VoiceConnection.disconnect());
         message.reply("done");
     }).catch(e => console.log(e))
 };

标签: node.jsffmpegdiscorddiscord.jsbots

解决方案


您需要使用一个名为 ytdl 的包来执行此操作:https ://www.npmjs.com/package/ytdl-core

在终端你做npm install ytdl-core@latest

然后添加const ytdl = require('ytdl-core');到您的 js 文件的顶部。

那么你需要改变

VoiceConnection.play("https://youtu.be/~~~~").on("finish", () =>

VoiceConnection.play(ytdl("https://youtu.be/~~~~")).on("finish", () =>

推荐阅读