Discordbot 不使用 ytdl-core 播放音乐
Discordbot doesn't play music with ytdl-core
我在 Discord 中输入了播放音乐的命令,机器人加入了,但几秒钟后就断开了与语音频道的连接,没有播放音乐。几天前它工作得很好,但现在不行了,我找不到代码中的错误。我安装了 ffmpeg 和 opusscript,所以这不会是错误。我希望有人能帮助我:
const Discord = require('discord.js');
const ytdl = require('ytdl-core');
const bot = new Discord.Client();
const prefix = "$";
const token = "";
function play(connection, message){
var server = servers[message.guild.id];
server.dispatcher = connection.playStream(ytdl(server.queue[0], {filter: "audioonly"}));
server.queue.shift();
server.dispatcher.on("end", function() {
if(server.queue[0]) play(connection, message);
else connection.disconnect();
});
}
var servers = {};
bot.on("ready", function(){
console.log("Ready");
});
bot.on("message", function(message) {
if(message.author.equals(bot.user)) return;
if(!message.content.startsWith(prefix)) return;
var args = message.content.substring(prefix.length).split(" ");
switch(args[0].toLowerCase()){
case "play":
if(!args[1]){
message.channel.send("Please add a link");
return;
}
if(!message.member.voiceChannel){
message.channel.send("You have to be in a Voice Channel");
return;
}
if(!servers[message.guild.id]) servers[message.guild.id] = {
queue: []
};
var server = servers[message.guild.id];
server.queue.push(args[1]);
if(!message.guild.voiceConnection) message.member.voiceChannel.join().then(function(connection){
play(connection, message);
});
break;
default:
message.channel.send("Unknown Command");
break;
}
});
bot.login(token);
Re-watch the video 并确保你正确复制了他的代码,如果你已经进入项目文件夹中的终端,请输入以下内容:
npm install
这将安装所有缺少的依赖项。
如果您没有安装节点,请执行此操作(只需快速 google 搜索即可)
如果它仍然无法正常工作,我不知道如何修复它,但考虑到您已经在使用其他人的代码,here is another option它比 Aeirety 音乐机器人要好得多。它具有以下功能:
- 播放 |: 播放来自 YouTube 的音频。
- skip [number]: 使用skip [some number]跳过一首或多首歌曲,
- 队列:显示当前队列。
- 暂停:暂停音乐播放。
- resume:恢复音乐播放。
- volume:在1到200之间调整播放音量。
- leave:清空歌曲队列,离开频道
- clearqueue:清除歌曲队列。
安装也简单看看再考虑吧
我知道这是一个老问题,但对于将来遇到这种不幸的人来说,您所要做的就是重新安装ytdl-core模块和npm install --save ytdl-core
。是的,就是这么简单和愚蠢...
祝你好运!
我在 Discord 中输入了播放音乐的命令,机器人加入了,但几秒钟后就断开了与语音频道的连接,没有播放音乐。几天前它工作得很好,但现在不行了,我找不到代码中的错误。我安装了 ffmpeg 和 opusscript,所以这不会是错误。我希望有人能帮助我:
const Discord = require('discord.js');
const ytdl = require('ytdl-core');
const bot = new Discord.Client();
const prefix = "$";
const token = "";
function play(connection, message){
var server = servers[message.guild.id];
server.dispatcher = connection.playStream(ytdl(server.queue[0], {filter: "audioonly"}));
server.queue.shift();
server.dispatcher.on("end", function() {
if(server.queue[0]) play(connection, message);
else connection.disconnect();
});
}
var servers = {};
bot.on("ready", function(){
console.log("Ready");
});
bot.on("message", function(message) {
if(message.author.equals(bot.user)) return;
if(!message.content.startsWith(prefix)) return;
var args = message.content.substring(prefix.length).split(" ");
switch(args[0].toLowerCase()){
case "play":
if(!args[1]){
message.channel.send("Please add a link");
return;
}
if(!message.member.voiceChannel){
message.channel.send("You have to be in a Voice Channel");
return;
}
if(!servers[message.guild.id]) servers[message.guild.id] = {
queue: []
};
var server = servers[message.guild.id];
server.queue.push(args[1]);
if(!message.guild.voiceConnection) message.member.voiceChannel.join().then(function(connection){
play(connection, message);
});
break;
default:
message.channel.send("Unknown Command");
break;
}
});
bot.login(token);
Re-watch the video 并确保你正确复制了他的代码,如果你已经进入项目文件夹中的终端,请输入以下内容:
npm install
这将安装所有缺少的依赖项。 如果您没有安装节点,请执行此操作(只需快速 google 搜索即可)
如果它仍然无法正常工作,我不知道如何修复它,但考虑到您已经在使用其他人的代码,here is another option它比 Aeirety 音乐机器人要好得多。它具有以下功能:
- 播放 |: 播放来自 YouTube 的音频。
- skip [number]: 使用skip [some number]跳过一首或多首歌曲,
- 队列:显示当前队列。
- 暂停:暂停音乐播放。
- resume:恢复音乐播放。
- volume:在1到200之间调整播放音量。
- leave:清空歌曲队列,离开频道
- clearqueue:清除歌曲队列。
安装也简单看看再考虑吧
我知道这是一个老问题,但对于将来遇到这种不幸的人来说,您所要做的就是重新安装ytdl-core模块和npm install --save ytdl-core
。是的,就是这么简单和愚蠢...
祝你好运!