admin管理员组文章数量:1382622
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
本文标签: javascriptdiscordjs not playing audio fileStack Overflow
版权声明:本文标题:javascript - discord.js not playing audio file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743986077a2571290.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论