如何播放随机的声音

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

我正在做一个Discord机器人,我想让它在加入频道时随机播放一个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时,它加入了我所在的语音频道,但没有播放任何内容。

javascript node.js random discord.js fs
1个回答
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;
© www.soinside.com 2019 - 2024. All rights reserved.