当我尝试播放我的 Music Discord Bot 时,它不播放音乐。它使用 ytdl-core 和 ffmpeg 我的代码是:
const Discord = require('discord.js');
const bot = new Discord.Client
const ytdl = require("ytdl-core")
const token = 'API TOKEN'
const PREFIX = '?';
var version = '1.2';
var servers = {};
bot.on('ready', () =>{
console.log('This bot is online!' + version);
})
bot.on('message', message => {
let args = message.content.substring(PREFIX.length).split(" ");
switch(args[0]){
case 'play':
function play(connection, message){
var server = servers[message.guild.id];
server.dispatcher = connection.play(ytdl(server.queue[0], {filter: "audioonly"}))
server.queue.shift();
server.dispatcher.on("end", function(){
if(server.queue[0]){
play(connection, message);
}else {
connection.disconnect();
}
});
}
if(!args[1]){
message.channel.send("You need to provide a link!")
return;
}
if(!message.member.voice.channel){
message.channel.send("You must be in a Voice Channel to play the bot!")
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.voice) message.member.voice.channel.join().then(function(connection){
play(connection, message);
})
break;
}
});
bot.login(token);
每当我尝试播放歌曲时,都会发生此错误:
(node:5180) UnhandledPromiseRejectionWarning: Error: FFmpeg/avconv not found! at Function.getInfo (C:\Users\picar\Desktop\DiscordMusicBot\node_modules\prism-media\src\core\FFmpeg.js:130:11) at Function.create (C:\Users\picar\Desktop\DiscordMusicBot\node_modules\prism-media\src\core\FFmpeg.js:143:38) at new FFmpeg (C:\Users\picar\Desktop\DiscordMusicBot\node_modules\prism-media\src\core\FFmpeg.js:44:27) at AudioPlayer.playUnknown (C:\Users\picar\Desktop\DiscordMusicBot\node_modules\discord.js\src\client\voice\player\BasePlayer.js:47:20) at VoiceConnection.play (C:\Users\picar\Desktop\DiscordMusicBot\node_modules\discord.js\src\client\voice\util\PlayInterface.js:71:28) at play (C:\Users\picar\Desktop\DiscordMusicBot\index.js:29:48) at C:\Users\picar\Desktop\DiscordMusicBot\index.js:66:17 at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:5180) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:5180) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code
请帮忙。
最佳答案
你安装了FFmpeg吗?或者 ffmpeg-static ?
尝试执行npm i ffmpeg ffmpeg-static
,它应该更新所有内容,如果它改变了任何内容,请告诉我们!
还要尝试安装 npm i @discordjs/opus
因为需要在 Discord 中播放音频:)
关于javascript - 当我尝试播放 Music Discord Bot 时,它不播放音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62124328/