【问题标题】:How to play random sound如何播放随机声音
【发布时间】:2020-09-12 11:59:20
【问题描述】:

我正在制作一个不和谐的机器人,我希望它在加入频道时播放随机的 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】:

您忘记添加文件路径。

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;

【讨论】:

  • 你使用的是什么版本的 discord.js?
  • 应该是v12(我4月份下载的),不知道怎么查,node.js是v12.16.2
  • 你安装@discordjs/opus了吗?
  • 文件扩展名已经在 readdirSync 的数组中
  • 是的,我当然感谢所有帮助过我的人,但您更需要即时的实时支持。不幸的是,我没有足够的代表来支持您的回答,再次感谢大家。
猜你喜欢
  • 2020-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多