【发布时间】: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