播放随机声音

时间:2020-05-25 17:28:48

标签: javascript node.js bots discord.js fs

我正在制作一个不和谐的机器人,如果我在聊天中键入$ join,我希望该机器人加入我所在的语音通道并播放随机声音。

case"join":
            message.delete( {timeout: 5000})
            const voiceChannel = message.member.voice.channel

            if(voiceChannel) {
                const connection = await voiceChannel.join()
                const soundFile = fs.readFileSync("./sounds/")
                const randFiles = soundFile[Math.floor(Math.random() * randFiles.length)]
                const dispatcher = connection.play(randFiles)
            } else {
                message.reply("you need to be in a voice channel!").then(message => message.delete( {timeout: 5000}))
            }
            break;

我收到此错误:

(node:13932) UnhandledPromiseRejectionWarning: Error: EISDIR: illegal operation on a directory, read
    at Object.readSync (fs.js:524:3)
    at tryReadSync (fs.js:349:20)
    at Object.readFileSync (fs.js:386:19)
    at Client.<anonymous> (C:\Users\PC\Desktop\doge_bot\doge-bot.js:124:38)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:13932) 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:13932) [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.

2 个答案:

答案 0 :(得分:1)

fs.readFileSync("./sounds/")用于读取文件的内容。 您可能正在寻找fs.readdirSync("./sounds/"),该目录会为您提供目录中的一系列文件。

答案 1 :(得分:1)

您阅读过documentation for readFileSync()吗? readFileSync()成功在目录路径上成功返回数据的唯一操作系统是FreeBSD。

相反,您要尝试执行的操作是在目录路径中获取文件列表;为此,您可以使用fs.readdirSync()

const soundFile = fs.readdirSync("./sounds/")