【问题标题】:How do I send the latest message ID with Express?如何使用 Express 发送最新的消息 ID?
【发布时间】:2020-09-07 16:18:04
【问题描述】:

我有以下代码,当我运行它时,我得到了这个错误:

TypeError: Cannot read property 'first' of undefined

我在 discord.js v12.0.2 和 express v4.16.2
这是我的代码:

app.get("/last", function(req, res) {
  res.header("Access-Control-Allow-Origin", "*")
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")

  res.send(String(client.channels.cache.get('540281078213246998').messages.fetch({ limit: 1 }).messages.first().id))
})

我正在尝试在我的 Discord 频道中获取最后一条消息的 ID。

【问题讨论】:

    标签: node.js express discord.js


    【解决方案1】:

    当您使用MessageManager.fetch() 时,它会返回一个通过MessageCollection 消息解析的Promise;这意味着您必须:

    • 等待承诺实现
    • 检查是否已返回集合(在您的情况下绝不应该发生这种情况,但无论如何我会介绍它)
    • 从消息中获取 ID

    以下是我的处理方法:

    client.channels.cache.get('540281078213246998').messages.fetch({ limit: 1 }).then(msg => {
      if (msg instanceof Map) // If the function returned a Collection...
        msg = msg.first() // ...set it to the first value.
    
      res.send(msg.id)
    })
    

    【讨论】:

      【解决方案2】:

      容易解决, messageCollection#fetch 返回promise,所以你需要放在那里 await 或使用 .then 函数。

      【讨论】:

        猜你喜欢
        • 2016-05-29
        • 1970-01-01
        • 2014-06-03
        • 2021-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-12
        • 1970-01-01
        相关资源
        最近更新 更多