Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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
Java 如何在discord jda中的最后一条消息之前获取该消息(列表)_Java_Discord - Fatal编程技术网

Java 如何在discord jda中的最后一条消息之前获取该消息(列表)

Java 如何在discord jda中的最后一条消息之前获取该消息(列表),java,discord,Java,Discord,我试图在discord jda中的最后一条消息之前获取该消息 我尝试过这个,但它给了我一个IndexOutOfBounds异常 event.getChannel().getHistory().getRetrievedHistory().get(1) 你能给我解释一下为什么要这么做,并给我指出正确的方向,这样我就能完成我的项目了吗?谢谢。根据getRetrievedHistory()的 使用retrievepass(int)、retrieveFuture(int)和MessageChannel.

我试图在discord jda中的最后一条消息之前获取该消息

我尝试过这个,但它给了我一个IndexOutOfBounds异常

event.getChannel().getHistory().getRetrievedHistory().get(1)
你能给我解释一下为什么要这么做,并给我指出正确的方向,这样我就能完成我的项目了吗?谢谢。

根据
getRetrievedHistory()

使用
retrievepass(int)
retrieveFuture(int)
MessageChannel.getHistoryAround(String,int)
方法从与此MessageHistory对象的不一致中检索到的所有消息的列表,从最新到最旧排序

如果只是使用MessageChannel.getHistory()或类似方法创建,则该值将为空首先必须检索邮件。

因此,您必须使用
retrievepass(2)
获取第二条最新消息:

channel.getHistory().retrievepass(2)
.map(messages->messages.get(1))//这假设通道至少有2条消息
.queue(消息->{//成功回调
System.out.println(“第二条最新消息是:”+message.getContentDisplay();
});
请记住,队列是异步的。这意味着这样的代码无法工作:

MessageHistory=channel.getHistory();
retrievepass(2.queue()//