package me.bramhaag.discordselfbot.commands.base;

import lombok.NonNull;
import me.bramhaag.discordselfbot.Bot;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageEmbed;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

public class CommandHandler {

    private Map<String, CommandData> commands = new HashMap<>();

    public void register(@NonNull Object... objects) {
        Arrays.stream(objects).forEach(this::register);
    }

    private void register(@NonNull Object object) {
        Arrays.stream(object.getClass().getMethods()).forEach(method -> {
            if(!method.isAnnotationPresent(Command.class)) return;

            Command info = method.getAnnotation(Command.class);
            CommandData commandData = new CommandData(method, info, object);

            commands.put(info.name(), commandData);
            Arrays.stream(info.aliases()).forEach(alias -> commands.put(alias, commandData));
        });
    }

    public void executeCommand(@NonNull MessageReceivedEvent event) {
        Message message = event.getMessage();

        String content = message.getRawContent();
        String[] parts = content.split(" ");

        String name = parts[0].substring(Bot.PREFIX.length()).trim();
        String[] args = new String[parts.length - 1];

        for(int i = 1; i < parts.length; i++) {
            args[i - 1] = parts[i].trim();
        }

        commands.forEach((command, data) -> {
            if (!name.equalsIgnoreCase(command)) return;

            Command info = data.getAnnotation();
            if (info.minArgs() != -1 && info.minArgs() < args.length) {
                //TODO do something
                return;
            }

            try {
                Object output = data.getMethod().invoke(data.getExecutor(), message, args);

                if(output == null)
                    return;

                if(output instanceof Message) {
                    message.editMessage((Message) output).queue();
                    return;
                }

                if(output instanceof MessageEmbed) {
                    message.editMessage((MessageEmbed) output).queue();
                }
            } catch (IllegalAccessException | InvocationTargetException e) {
                //TODO handle exception
                e.printStackTrace();
            }
        });
    }
}

@@ -106,16 +106,102 @@
 ot.Bot;%0A
+import net.dv8tion.jda.core.Permission;%0Aimport net.dv8tion.jda.core.entities.Channel;%0A
 import n
@@ -2298,49 +2298,375 @@
     
-message.editMessage((MessageEmbed) output
+MessageEmbed messageEmbed = (MessageEmbed) output;%0A                    if(message.getGuild().getMember(message.getAuthor()).hasPermission((Channel)message.getChannel(), Permission.MESSAGE_EMBED_LINKS))%0A                        message.editMessage(messageEmbed).queue();%0A                    else%0A                        message.editMessage(messageEmbed.getDescription()
 ).qu

Send text instead of embeds when missing permissions