package de.blackcraze.grb.listener;

import static de.blackcraze.grb.util.CommandUtils.parseAction;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;
import java.util.Scanner;

import de.blackcraze.grb.commands.Commands;
import de.blackcraze.grb.commands.FileProcessor;
import de.blackcraze.grb.core.BotConfig;
import de.blackcraze.grb.core.Speaker;
import de.blackcraze.grb.util.CommandUtils;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter;

public class MessageListener extends ListenerAdapter {

    public MessageListener() {
    }

    @Override
    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        Message message = event.getMessage();
        String listeningChannel = BotConfig.getConfig(message.getGuild()).CHANNEL;
        String messageChannel = message.getTextChannel().getName();
        if (!listeningChannel.equalsIgnoreCase(messageChannel)) {
            return;
        }

        if (!message.getAttachments().isEmpty()) {
            FileProcessor.ocrImages(message);
            return;
        }

        Optional<Scanner> scannerOptional = CommandUtils.commandParser(message);
        if (!scannerOptional.isPresent()) {
            return;
        }

        Scanner scanner = scannerOptional.get();

        Optional<String> actionOptional = parseAction(scanner);

        Optional<Method> methodOptional = Arrays.stream(Commands.class.getDeclaredMethods())
                .filter(aMethod -> aMethod.getName().equalsIgnoreCase(actionOptional.orElse("help"))).findFirst();

        if (!methodOptional.isPresent()) {
            message.addReaction(Speaker.Reaction.FAILURE).queue();
            return;
        }

        System.out.printf("%s:%s%n", message.getAuthor().getName(), message.getContent());

        try {
            methodOptional.get().invoke(null, scanner, message);
        } catch (Exception e) {
            message.addReaction(Speaker.Reaction.FAILURE).queue();
            e.printStackTrace();
        }

    }

}

@@ -545,16 +545,93 @@
 dEvent;%0A
+import net.dv8tion.jda.core.events.message.priv.PrivateMessageReceivedEvent;%0A
 import n
@@ -2220,10 +2220,1227 @@
 %0A    %7D%0A%0A
+    @Override%0A    public void onPrivateMessageReceived(PrivateMessageReceivedEvent event) %7B%0A        Message message = event.getMessage();%0A%0A        if (!message.getAttachments().isEmpty()) %7B%0A            FileProcessor.ocrImages(message);%0A            return;%0A        %7D%0A%0A        Optional%3CScanner%3E scannerOptional = CommandUtils.commandParser(message);%0A        if (!scannerOptional.isPresent()) %7B%0A            return;%0A        %7D%0A%0A        Scanner scanner = scannerOptional.get();%0A%0A        Optional%3CString%3E actionOptional = parseAction(scanner);%0A%0A        Optional%3CMethod%3E methodOptional = Arrays.stream(Commands.class.getDeclaredMethods())%0A                .filter(aMethod -%3E aMethod.getName().equalsIgnoreCase(actionOptional.orElse(%22help%22))).findFirst();%0A%0A        if (!methodOptional.isPresent()) %7B%0A            message.addReaction(Speaker.Reaction.FAILURE).queue();%0A            return;%0A        %7D%0A%0A        System.out.printf(%22%25s:%25s%25n%22, message.getAuthor().getName(), message.getContent());%0A%0A        try %7B%0A            methodOptional.get().invoke(null, scanner, message);%0A        %7D catch (Exception e) %7B%0A            message.addReaction(Speaker.Reaction.FAILURE).queue();%0A            e.printStackTrace();%0A        %7D%0A%0A    %7D    %0A    %0A
 %7D%0A

Enable private messages.