Search code examples
javaarraylistdiscorddiscord4j

Trying to search for keywords in Discord messages by comparing to Arraylist, using Discord4j wrapper


I'm trying to code a simple Discord bot in Java that responds whenever it detects a keyword stored in a text file. Before I connect to Discord I load the text file into an ArrayList, and when I receive a Discord message I convert the message string into an array and use retainAll() to test for common elements, however when I run the program nothing happens. Here is my code.

        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();

Any pointers in the right direction would be very much appreciated, thanks.


Solution

  • I eventually managed to solve this using Collections.disjoint() to compare the 2 arrays.