Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在discord.js中获取最新消息?_Javascript_Node.js_Discord.js - Fatal编程技术网

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

Javascript 如何在discord.js中获取最新消息?,javascript,node.js,discord.js,Javascript,Node.js,Discord.js,每次在特定通道中发送消息时,我都希望将其打印到控制台(使用console.log)。我还将使用npm安装颜色给它上色。我到处去,甚至在堆栈溢出上,但我似乎找不到任何信息。我正在编写一个学术碗帮助机器人。下面是我尝试过的代码(我在堆栈溢出上发现了这一点) message.fetch({limit:1})。然后(messages=>{ 让lastMessage=message.first(); if(message.channel.lastMessage='channel id'){ 日志(last

每次在特定通道中发送消息时,我都希望将其打印到控制台(使用console.log)。我还将使用
npm安装颜色给它上色。我到处去,甚至在堆栈溢出上,但我似乎找不到任何信息。我正在编写一个学术碗帮助机器人。下面是我尝试过的代码(我在堆栈溢出上发现了这一点)

message.fetch({limit:1})。然后(messages=>{
让lastMessage=message.first();
if(message.channel.lastMessage='channel id'){
日志(lastMessage.red);
}
}) 
(请注意,当我说
'channel-id'
时,我指的是频道的实际id。)

我得到的错误是
消息。首先
不是一件事

如何修复此错误,以及如何在discord.js中获取最新消息?

编辑:我得到的确切错误是:

(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.
以下是对该问题第三条评论的编辑(按最早的评论排序):

message.channel.fetch({limit:1})。然后(messages=>{
让lastMessage=message.channel.first();
if(message.channel.lastMessage='channel id'){
日志(lastMessage.red);
}
})

我假设
message.fetch
需要是
message.channel.fetch
使用
message.channel.messages.fetch()
而不是
message.channel.fetch()

我没有在discord.js文档中找到
消息。first
函数,因此我不确定它是否有效。但是,您并不真正需要该函数来获取消息。fetch函数已经为您完成了这项工作

在您的情况下,选项
limit:1
将只返回最近的消息,这是您用来触发提取的命令。如果要获取最新消息而不是命令,则应改用
limit:2
,稍后在对象中删除命令。
fetch函数将返回一个包含消息id和内容的对象。

它仍然会给出错误信息。我认为这与
消息有关。首先
。我还尝试了
消息。首先
,但仍然不起作用。但是,是的,我认为你键入的内容解决了我一半的问题,谢谢。好吧,17分钟后,我在一篇堆栈溢出的帖子中写道:我认为message.first不推荐使用。我使用的是
channel.lastMessage
,它似乎有效。如果将
message.first()
更改为
message.channel.first()
(节点:13124)未经处理的PromiserEjectionWarning:TypeError:message.channel.first不是一个函数,它不适用于我一直放在问题底部的代码(为了启用格式化)它很可能已被弃用,那么我猜,我假设它正在使用
message.channel.fetch
&
message.channel.lastMessage