问题描述
我正在尝试用 Java 编写一个简单的 discord bot,它会在检测到存储在文本文件中的关键字时做出响应。在我连接到 discord 之前,我将文本文件加载到 ArrayList 中,当我收到 discord 消息时,我将消息字符串转换为数组并使用 retainAll()
来测试公共元素,但是当我运行程序时什么也没有发生.这是我的代码。
Keywords keywords = new Keywords();
ArrayList<String> keywordList = keywords.getKeywords();
final discordClient client = discordClient.create("TOKEN");
final GatewaydiscordClient gateway = client.login().block();
gateway.on(MessageCreateEvent.class).subscribe(event -> {
final Message message = event.getMessage();
boolean commonElements = keywordList.retainAll(Arrays.asList(message.getContent().split("\\s+")));
if (commonElements) {
final MessageChannel channel = message.getChannel().block();
channel.createMessage("Based.").block();
}
});
gateway.ondisconnect().block();
非常感谢任何指向正确方向的指针,谢谢。
解决方法
我最终设法使用 Collections.disjoint()
来比较 2 个数组来解决这个问题。