[英]How to play random sound
I'm making a discord bot, and I want it to play a random mp3 file when it joins channel.我正在制作一个 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;
When I type $join in chat, it joins voice channel that I'm in but doesn't play anything.当我在聊天中键入 $join 时,它会加入我所在的语音频道,但不会播放任何内容。
You forgot to add the file path.您忘记添加文件路径。
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.