node.js - 如何让 Discord Bot 播放 YouTube URL

标签 node.js ffmpeg discord discord.js bots

我是新手,我想知道是否有办法让我的机器人播放特定的 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))
 };

最佳答案

您需要使用一个名为 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", () =>

关于node.js - 如何让 Discord Bot 播放 YouTube URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66507956/

相关文章:

node.js - 在 MongoDB Node JS Rest API 中使用 ObjectId 类时出现类型错误

node.js - puppet :命令 npm 丢失

node.js - 使用 Redis 的 NodeJs 缓存

ffmpeg - 添加章节的问题 - FFmpeg

discord - 如何回复发送给机器人的私信?

javascript - Discord 机器人对一个事件多次响应

javascript - 'mocha' 不被识别为内部或外部命令 - mocha 不会自动安装 supertest

node.js - 使用 fluent-ffmpeg 添加背景音乐

php - 使用 ffmpeg 命令将 avi 转换为 mp4、webm 和 flv

python - on_reaction_add 自定义表情符号