【问题标题】:Discord.js - Post image from folder ERRORDiscord.js - 从文件夹错误发布图像
【发布时间】:2021-08-27 22:02:36
【问题描述】:

如果有人使用命令 !image,我希望我的机器人以图像响应。 此图像必须是来自名为 gta 的特定文件夹中的随机图像。

我的代码:

client.on("message",message=>{
  if(message.content==('!imagetest')){
    var files = fs.readdirSync('./gta/')
    var chosenFile = files[Math.floor(Math.random() * files.length)] 
    console.log(chosenFile)
    message.channel.send(
      {
        files : [
          chosenFile
        ]
      }
    )
  }
})

错误:

UnhandledPromiseRejectionWarning: E​​rror: ENOENT: no such file or directory, stat '/home/container/d864050.jpg'

它认为应该是/home/container/gta/...(随机图像), 但我不知道该怎么做。

任何帮助将不胜感激!

【问题讨论】:

    标签: random directory discord.js


    【解决方案1】:

    您可以尝试将 chosenFile 拆分为 '/' 并在 '/container/' 和 'd864050.jpg' 之间添加 'gta/'。

    类似这样的:

    ...
    var chosenFile = files[Math.floor(Math.random() * files.length)]
    const arr = chosenFile.split("/")
    var returnMsg = `/${arr[0]}/${arr[1]}/gta/${arr[2]}`
    ...
    

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2021-10-27
      • 1970-01-01
      相关资源
      最近更新 更多