Reputation: 117
I am trying to play an audio file but, for some reason it's not playing anything, it's firing end
event instead of start
event, right after the bot is connected to voice channel.
client.on('message', message => {
if(message.content.startsWith('!play')) {
if(!message.member.voiceChannel) return message.channel.send('connect to voice channel first');
message.member.voiceChannel.join()
.then(connection => {
console.log("Joined voice channel!");
const dispatcher = connection.playFile(require("path").join(__dirname, './myfile.mp3'));
dispatcher.on('start', () => { //not working
dispatcher.setVolume(0.70);
console.log("Playing");
});
dispatcher.on('error', (err) => console.log(err)); //no errors
dispatcher.on('end', end => { //working fine
console.log("Finished");
console.log("End: " + end);
message.member.voiceChannel.leave()
});
});
}});
Upvotes: 3
Views: 6162
Reputation: 544
Just wanted to mention that for me neoney's answer worked, but it threw an error on connection.playFile, what finally ended up working for me was:
const dispatcher = connection.play(require("path").join(__dirname, './myfile.mp3'));
Note that you can also use (__dirname, '../myFolderName/myfile.mp3') in the parenthesis to go out of the current folder and play from within another named folder up one directory.
For more detail on the complete setup, there is a decent video/channel here by CodeLyon: https://www.youtube.com/watch?v=q0lsD7U0JSI&t=917s
Upvotes: 1
Reputation: 117
I removed ffmpeg-binaries
from node-modules
and installed ffmpeg
using sudo apt
and it's working fine now. The problem was that, i had both of these libraries installed.
Upvotes: 2
Reputation: 96
As stated in the docs: https://discord.js.org/#/docs/main/stable/class/VoiceConnection?scrollTo=playFile the path to the file has to be an absolute path.
You can use the module path
(no need to download) and the global __dirname to get the absolute path.
const dispatcher = connection.playFile(require("path").join(__dirname, './myfile.mp3'));
Upvotes: 5