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()
});
});
}});
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()
});
});
}});
Share
Improve this question
edited Oct 14, 2019 at 14:29
Blayke
asked Oct 14, 2019 at 13:31
BlaykeBlayke
1173 silver badges11 bronze badges
3 Answers
Reset to default 5As stated in the docs: https://discord.js/#/docs/main/stable/class/VoiceConnection?scrollTo=playFile the path to the file has to be an absolute path.
To solve this issue:
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'));
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.
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 plete setup, there is a decent video/channel here by CodeLyon: https://www.youtube./watch?v=q0lsD7U0JSI&t=917s