【问题标题】:Play random sound播放随机声音
【发布时间】:2020-09-12 09:32:38
【问题描述】:

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


    【解决方案1】:

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

    相反,您似乎正在尝试获取目录路径中的文件列表;为此,您可以使用fs.readdirSync():

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

    【讨论】:

    • 感谢您的帮助,这似乎已解决,但现在当我输入 $join 时,它会加入我的语音频道并且不播放任何内容。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多