【问题标题】:How to add thumbnails to Discord embeds?如何将缩略图添加到 Discord 嵌入?
【发布时间】:2020-09-20 02:13:19
【问题描述】:

我正在创建一个 Discord 机器人,它显示来自 Minecraft 服务器 Hypixel 的玩家统计数据。我也在尝试添加他们玩家的头像,作为机器人发送的嵌入中的缩略图。为了获得我使用的头像Crafatar。这是我正在使用的代码,但是缩略图没有显示在嵌入中。我相信这与我使用带有变量的 URL 的事实有关,因为我尝试使用常规 URL 并且它工作正常。

.setThumbnail(`https://crafatar.com/avatars/${uuid}.jpg`)

变量uuid 在我的代码中被声明并分配给一个值。

编辑 1

这是我从中获取 uuid 变量的代码。我看不出它有什么问题,但是,我对 JavaScript 不是特别熟练。

var uuid = '';
getId(args[1]).then(id => {
  uuid = id;
})

getId是下面定义的函数,如下所示:

function getId(playername) {
  return fetch(`https://api.mojang.com/users/profiles/minecraft/${playername}`)
    .then(data => data.json())
    .then(player => player.id);
}

这使用 Mojang API 将作为命令参数输入的玩家显示名称转换为他们的 UUID。

【问题讨论】:

  • 从这里看起来很好。你确定uuid 定义正确吗?如果你这样做 console.log(uuid) 会打印什么?
  • 我已经用更多细节编辑了我的答案。我尝试添加 console.log 并没有打印任何内容,这意味着您是对的,但我不确定为什么。

标签: discord.js


【解决方案1】:

从您发布的内容来看,问题似乎在于您正确使用 Promises 来设置 uuid 的值,但在设置嵌入之前您并没有等待它被设置缩略图。
您应该使用Promise.then()(就像您在其他地方所做的那样)或async/await 等待它。
这是一个例子:

// Using .then():
getId(args[1]).then(uuid => {
  let embed = new MessageEmbed()

  // You have to set the thumbnail after you get the actual ID:
  embed.setThumbnail(`https://crafatar.com/avatars/${uuid}.jpg`)

  // You can now send the embed
})

// Using async/await:
let uuid = await getId(args[1])
let embed = new MessageEmbed()
embed.setThumbnail(`https://crafatar.com/avatars/${uuid}.jpg`)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-21
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-09-25
    • 2021-08-13
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多