如何播放随机声音

时间:2020-05-25 19:24:09

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

我正在制作一个不和谐的机器人,我希望它加入频道后会播放随机的mp3文件。

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

当我在聊天中键入$ join时,它会加入我所在的语音通道,但不会播放任何内容。

1 个答案:

答案 0 :(得分:1)

您忘记添加文件路径。

case "join":
message.delete({ timeout: 5000 })
const voiceChannel = message.member.voice.channel
if (voiceChannel) {
  const connection = await voiceChannel.join()
  const files = fs.readdirSync("./sounds/")
  const randFile = files[Math.floor(Math.random() * files.length)]
  const dispatcher = connection.play(`./sounds/${randFile}`) // Obviously change `.mp3` to the file extension of your sound files.
} else {
  message.reply("you need to be in a voice channel!").then(message => message.delete({ timeout: 5000 }))
}
break;
相关问题