【问题标题】:Discord.js Embed Timestamp not able to parse a UNIX timestampDiscord.js 嵌入时间戳无法解析 UNIX 时间戳
【发布时间】:2020-05-27 17:26:46
【问题描述】:

尝试使用 discord.js 将不和谐消息编辑为 RichEmbedded 消息时,我收到错误消息

(node:10860) UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body
embed.timestamp: Could not parse 1581492006141. Should be ISO8601.
    at item.request.gen.end (/rbd/pnpm-volume/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/node_modules/.registry.npmjs.org/discord.js/11.5.1/node_modules/discord.js/src/client/rest/RequestHandlers/Sequential.js:85:15)
    at then (/rbd/pnpm-volume/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/node_modules/.registry.npmjs.org/snekfetch/3.6.4/node_modules/snekfetch/src/index.js:215:21)
    at process._tickCallback (internal/process/next_tick.js:68:7)

我在 glitch.com 上托管该机器人。我正在向频道发送消息,然后将其编辑为包含请求数据的嵌入式消息。

msg.channel.send("Recieving Data").then(response => {
  //Get data
  response.edit({
    embed: {
      description: "Example Data",
      timestamp: msg.createdTimestamp
    }
  })
}).catch(//handle error);

如果我发送嵌入消息而不是编辑以前的消息,它不会出错。我试过解析时间戳但它不接受它。是不是因为上一条消息发送后时间戳无法更改?

【问题讨论】:

  • 在 Discord 中,embed 的时间戳是 ISO8601 时间戳,discord.js 在 createdTimestamp 上是否也使用 ISO8601 时间戳?否则,您必须在时间戳末尾添加 3 个“0”
  • @ShigehiroKamisama 据我了解,createdTimestamp 将 UNIX 时间作为数值(即 1581532642193)给出,正常解析它没有问题,但如果我使用嵌入编辑之前的消息,它会出现问题。

标签: node.js discord discord.js


【解决方案1】:

您必须使用您的时间戳创建一个新日期,Discord 不接受此字段中的时间戳,only ISO8601 date

你可以这样做:

...

msg.channel.send("Recieving Data").then(response => {
  //Get data
  response.edit({
    embed: {
      description: "Example Data",
      timestamp: msg.createdAt
    }
  })
}).catch(//handle error);

...

但是,如果您想要一种快速简便的嵌入设计方法,请尝试使用 leovoel 提供的这个工具,Embed Visualizer 会准确显示嵌入的外观,并会在按下时为您生成代码一个按钮。这样你会看到时间戳字段不接受时间戳。

【讨论】:

【解决方案2】:

您可以编辑嵌入时间戳,但在您的解决方案中,您尝试仅使用时间戳字段编辑 msg。所以你有一个错误。 msg.edit()不要改变部分内容,这个方法会改变完整的消息内容。

如果你嵌入了description, footer, fields。并且您将使用 description 数据字段编辑他,嵌入的其他字段将被删除。

    embed: {
      //Data
      description: "someText"
    }

正确的方法是获取消息内容,嵌入并更改接收数据嵌入时间戳,然后使用新数据编辑消息。

【讨论】:

  • 对不起,我没有费心将描述字段添加到示例代码中,因为我认为它不相关,在我的实际代码中,我确实制作了所有必要的字段。我将在示例中更清楚地说明。
  • 嗯,试试timestamp:new Date(msg.createdTimestamp).toISOString();
  • 这就是魅力,但不知道为什么它之前无法解析它。
猜你喜欢
  • 1970-01-01
  • 2013-03-23
  • 2021-02-12
  • 1970-01-01
  • 2020-09-16
  • 2021-06-07
  • 2013-03-11
  • 2021-06-30
  • 1970-01-01
相关资源
最近更新 更多