简体   繁体   English

如何在 discord.js 中获取最新消息?

[英]How do I get the most recent message in discord.js?

Every time a message is sent in a specific channel, I want to print it to the console (with console.log).每次在特定频道中发送消息时,我都想将其打印到控制台(使用 console.log)。 I am also going to color it with npm install colors .我还将使用npm install colors为它着色。 I go everywhere, even on Stack Overflow, but I cannot seem to find any information.我到处走走,甚至在 Stack Overflow 上,但我似乎找不到任何信息。 I am coding a Scholastic Bowl-helping bot.我正在编写一个 Scholastic Bowl 帮助机器人。 Below is the code I have tried (I found this on Stack Overflow.)下面是我试过的代码(我在 Stack Overflow 上找到了这个。)

message.fetch({ limit: 1 }).then(messages => {
  let lastMessage = message.first();
    if (message.channel.lastMessage = 'channel-id'){
      console.log(lastMessage.red);
  }
}) 

(Note that when I say 'channel-id' I mean the actual ID of the channel.) (请注意,当我说'channel-id'我指的是频道的实际 ID。)

The error I am getting is that message.first is not a thing.我得到的错误是message.first不是一回事。

How do I fix this error, and how can I get the most recent message in discord.js?如何修复此错误,以及如何在 discord.js 中获取最新消息?

Edit: The exact error I got is this:编辑:我得到的确切错误是:

(node:12352) UnhandledPromiseRejectionWarning: TypeError: messages.first is not a function
    at C:\Users\[user redacted]\Desktop\SchoBot\index.js:57:32
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:12352) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:12352) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Below is the edit for the 3rd comment on this question (sorted by oldest):以下是对该问题的第 3 条评论的编辑(按最旧排序):

  message.channel.fetch({ limit: 1 }).then(messages => {
    let lastMessage = message.channel.first();
      if (message.channel.lastMessage = 'channel-id'){
        console.log(lastMessage.red);
    }
})

Use message.channel.messages.fetch() instead of message.channel.fetch() .使用message.channel.messages.fetch()而不是message.channel.fetch()

I didn't find the message.first function in the discord.js documentation, so I am not sure if it works.我在 discord.js 文档中没有找到message.first函数,所以我不确定它是否有效。 But you don't really need that function to fetch a message.但是您实际上并不需要该函数来获取消息。 The fetch function already did that for you. fetch 函数已经为你做到了。

In your case, the option limit: 1 will only return the most recent message, which is the command you use to trigger the fetch.在您的情况下,选项limit: 1将只返回最近的消息,这是您用来触发提取的命令。 If you want to fetch the most recent message but not your command, you should use limit: 2 instead and remove your command in the object later.如果你想获取最新的消息而不是你的命令,你应该使用limit: 2并稍后在对象中删除你的命令。 The fetch function will return an object containing message id and the content. fetch 函数将返回一个包含消息 ID 和内容的对象。

我假设message.fetch需要是message.channel.fetch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM