【问题标题】:Getting Directory Not Found When Not Looking For That Directory不查找目录时未找到目录
【发布时间】:2021-02-22 11:07:41
【问题描述】:

我正在尝试从如下所示的数组中显示随机图像:

[
    {
        "name": "weedle",
        "image": "https://c0.klipartz.com/pngpicture/478/959/sticker-png-pokemon-red-and-blue-pokemon-x-and-y-weedle-beedrill-others-tail-pokemon-evolution-pokemon-red-and-blue-pokemon-x-and-y.png"
    },
    {
        "name": "caterpie",
        "image": "https://img.favpng.com/2/19/21/pok-mon-diamond-and-pearl-pok-mon-sun-and-moon-pok-mon-tcg-online-caterpie-png-favpng-znPm71z43VHMdHAMhCqitLD5w_t.jpg"
    }
]

这是应该随机存储其中一个的行。我什至不确定我想要做的事情是否可行:

const pokemon = require('./pokemon.json');
const pokemonImage = new Discord.MessageAttachment(pokemon[0].image[Math.floor(Math.random() * pokemon.length)]);

在尝试此操作并尝试发送消息时,我收到此错误:

(node:8880) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, stat 'C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\h'

我不知道为什么它在我不搜索目录“h”时尝试搜索它。

如果我删除“pokemon[0].image”中的“image”,我会收到以下错误:

(node:1520) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'path' of undefined
    at findName (C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\node_modules\discord.js\src\structures\APIMessage.js:290:17)
    at Function.resolveFile (C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\node_modules\discord.js\src\structures\APIMessage.js:306:31)
    at C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\node_modules\discord.js\src\structures\APIMessage.js:241:72
    at Array.map (<anonymous>)
    at APIMessage.resolveFiles (C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\node_modules\discord.js\src\structures\APIMessage.js:241:46)
    at TextChannel.send (C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:172:46)
    at Client.<anonymous> (C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\index.js:33:50)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\xaguy\Desktop\PokeCord (My Version)\DiscordBot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)

【问题讨论】:

  • 我的一位帮助我维护我的其他机器人的朋友在尝试显示图像时遇到了第二个错误。当我这样做时,它运行良好,当他使用相同的代码时,他得到“未定义的路径”。我假设这个问题主要与此有关。补充一下,我给他发了机器人的所有文件。我还将文件复制并粘贴到新文件夹中,以作为新机器人启动。我以前做过,到目前为止没有任何问题。

标签: javascript node.js json discord discord.js


【解决方案1】:

让我们深入了解这里发生了什么:

const pokemonImage = new Discord.MessageAttachment(pokemon[0].image[Math.floor(Math.random() * pokemon.length)]);

你从pokemon中的这个数组开始:

[
    {
        "name": "weedle",
        "image": "https://c0.klipartz.com/pngpicture/478/959/sticker-png-pokemon-red-and-blue-pokemon-x-and-y-weedle-beedrill-others-tail-pokemon-evolution-pokemon-red-and-blue-pokemon-x-and-y.png"
    },
    {
        "name": "caterpie",
        "image": "https://img.favpng.com/2/19/21/pok-mon-diamond-and-pearl-pok-mon-sun-and-moon-pok-mon-tcg-online-caterpie-png-favpng-znPm71z43VHMdHAMhCqitLD5w_t.jpg"
    }
]

你通过pokemon[0]获得第一个元素:

{
        "name": "weedle",
        "image": "https://c0.klipartz.com/pngpicture/478/959/sticker-png-pokemon-red-and-blue-pokemon-x-and-y-weedle-beedrill-others-tail-pokemon-evolution-pokemon-red-and-blue-pokemon-x-and-y.png"
    }

然后是image 属性:

"https://c0.klipartz.com/pngpicture/478/959/sticker-png-pokemon-red-and-blue-pokemon-x-and-y-weedle-beedrill-others-tail-pokemon-evolution-pokemon-red-and-blue-pokemon-x-and-y.png"

然后您使用[Math.floor(Math.random() * pokemon.length)] 来获取该字符串中随机索引处的字符,根据口袋妖怪数组的长度严重偏向前面。在发布的错误中,该随机字符是h。 discord 库尝试将h 解析为相对于工作目录的文件,当然失败了。

我想你想随机选择一个口袋妖怪然后获取图像,这更像是这样。

const pokemonImage = new Discord.MessageAttachment(pokemon[Math.floor(Math.random() * pokemon.length)].image)

【讨论】:

  • 哇,这是一个完美的崩溃。如果我自己这样做,我会立即意识到。谢谢你。现在它可能只是运气不好,因为它是 50/50,但它似乎只显示第二张图像。我已经测试了 13 次,每次都是毛毛虫出现。这只是运气不好还是有其他问题?
  • @QuazArxx 我测试了随机数的生成,并在几百次内获得每个数字,另一个运行 100k,这似乎是一个巧合。 jsfiddle.net/tnmuw710/1
  • 感谢您的帮助。你知道为什么我也得到了未定义的路径吗?也许这个推理可以帮助我解决为什么当我告诉他要输入什么时我朋友的代码不起作用。
  • @QuazArxx 这值得单独提问。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2020-10-21
相关资源
最近更新 更多