簡體   English   中英

如何播放隨機聲音

[英]How to play random sound

我正在制作一個 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 時,它會加入我所在的語音頻道,但不會播放任何內容。

您忘記添加文件路徑。

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM