【问题标题】:Pulling emoji URL from .json gives undefined从 .json 中提取 emoji URL 给出未定义
【发布时间】:2021-04-17 11:29:24
【问题描述】:

所以我正在开发一个机器人,并且我有以下代码。我正在尝试从消息的args[1] 中获取颜色名称,并使用颜色名称来确定要抓取哪个表情符号链接,并使用以下代码从机器人中的 json 中提取它-

const store = require(‘./../../items.json’);

// inside module.exports -
const item = args[0];
const col = args[1];

let name = store[item].name;
let price = store[item].price;
let desc = store[item].description;

let emote = store[item].col // trying to grab the emote link from the variable

当放入嵌入时,它为表情变量返回“未定义”。下面的代码是json位(在这种情况下,item变量是fish)-

{
   “fish”: {
      “name”: “fish”,
      “price”: “$1”,
      “description”: “<description here>“,

      “white”: “https://<white fish emoji link here>”,
      “pink”: “https://<pink fish emoji link here”
    }
}

我在多个不和谐服务器中询问过,并在网上四处寻找答案。

【问题讨论】:

  • 您是否记录了变量以查看它们是否与您认为的一样?

标签: javascript json discord.js


【解决方案1】:

您应该按照您的方式使用 if else 语句,您无法使用变量获取对象值,您可以检查 col === pink 然后给出表情值 x 如果 col === blue 给出表情值y 等等 像这样


if(col === 'pink'){
let emote = store[item].pink
}

编辑使用 let 代替 const

【讨论】:

  • 这记录为“无效的表单正文”,因为我试图将 items.json 中的表情符号 url 设置为缩略图,但它仍然没有使其未定义
  • 我能看到代码吗?错误是你只是复制了我的代码吗?
  • 它是说它必须是http,https格式,因为它是一个缩略图,但是链接是cdn.discordapp.com/emojis/<emoji ID>.png?v=1链接,所以变量在https格式
  • 是的,让我向您展示目前的错误。我没有直接复制你的代码,没有
  • (node:28255) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.thumbnail.url: Scheme "0" 不受支持。方案必须是 ('http', 'https') 之一。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-06
  • 1970-01-01
  • 2018-09-16
  • 2019-01-12
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多