最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

discord.js无法播放音频文件

IT培训 admin 4浏览 0评论

discord.js无法播放音频文件

我正在尝试播放音频文件,但是由于某种原因,它没有播放任何内容,它是在机器人连接到语音通道后立即触发end事件而不是start事件。

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()
            });
        });

}});
回答如下:

如文档所述:https://discord.js/#/docs/main/stable/class/VoiceConnection?scrollTo=playFile文件的路径必须为绝对路径

解决此问题:

您可以使用模块path(无需下载)和全局__dirname来获取绝对路径。

const dispatcher = connection.playFile(require("path").join(__dirname, './myfile.mp3'));

discord.js无法播放音频文件

我正在尝试播放音频文件,但是由于某种原因,它没有播放任何内容,它是在机器人连接到语音通道后立即触发end事件而不是start事件。

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()
            });
        });

}});
回答如下:

如文档所述:https://discord.js/#/docs/main/stable/class/VoiceConnection?scrollTo=playFile文件的路径必须为绝对路径

解决此问题:

您可以使用模块path(无需下载)和全局__dirname来获取绝对路径。

const dispatcher = connection.playFile(require("path").join(__dirname, './myfile.mp3'));

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论