When I try to play my Music Discord Bot it doesn't play music. It uses ytdl-core and ffmpeg My code is:
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);
Whenever I try to play a song this error happens:
(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
I'm getting very frustrated as the tutorial I'm watching is using a different version of everything.!! Please help.
Have you installed FFmpeg? Or ffmpeg-static?
Try doing npm i ffmpeg ffmpeg-static
, it should update everything, let us know if it changes anything !
Also try to install npm i @discordjs/opus
since it is required to play audio in Discord:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.