随机播放声音

问题描述 投票:0回答:2

我正在制作一个Discord机器人,如果我在聊天中输入$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.

javascript node.js bots discord.js fs
2个回答
1
投票

你有没有读过 文件的 readFileSync()? 唯一的操作系统 readFileSync() 成功地返回一个目录路径的数据是在FreeBSD上。

相反,您要做的是抓取一个目录路径上的文件列表;为此您可以使用 fs.readdirSync():

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

1
投票

fs.readFileSync("./sounds/") 是用来读取文件内容的,你可能在找的是 fs.readdirSync("./sounds/") 它给你一个目录中的文件数组。

© www.soinside.com 2019 - 2024. All rights reserved.