node.js - 如何让 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))
};
解决方案
您需要使用一个名为 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", () =>
推荐阅读
- java - Java 8 应用程序无法在 MacOS Catalina 10.15 上的暗模式下正常工作
- eclipse - Nutch/Hadoop:如何配置 url 来跟踪作业?
- jquery - DropDownBox内的Devextreme多选列表
- excel - 从 VBA 运行 DXL
- c# - 无法分配,因为它是一个方法组
- r - 除了使用 odbc、RODBC 等之外,还有其他方法可以从 SQL 数据库中提取大型数据集吗?
- flutter - Flutter : For 循环和 LayoutBuilder
- python - 检查 is_null 以获取 Django 查询集过滤中的字段列表
- python - 过滤列表两次。哪种方式最方便?
- javascript - 在多路径PHP中上传多个文件