Search in sources :

Example 1 with ParameterContext

use of net.badbird5907.jdacommand.context.ParameterContext in project JDACommand by Badbird-5907.

the class JDACommand method registerCommand.

private void registerCommand(Command command, String name, Method method, Object o) {
    try {
        out.println("Registering command: " + name);
        if (command.disable() || method.isAnnotationPresent(Disable.class))
            return;
        if (!COMMAND_REGEX.matcher(name.toLowerCase()).matches()) {
            throw new IllegalArgumentException("Command name must match regex: " + COMMAND_REGEX.pattern() + " see https://discord.com/developers/docs/interactions/application-commands for more info");
        }
        if (commandMap.containsKey(name) || name.isEmpty()) {
            return;
        }
        List<CommandCreateAction> actions = new ArrayList<>();
        for (Guild guild : jda.getGuilds()) {
            boolean upsert = returnCallBack == null || returnCallBack.shouldUpsertCommand(guild);
            if (upsert) {
                actions.add(guild.upsertCommand(name.toLowerCase(), command.description()));
            }
        }
        Parameter[] params = method.getParameters();
        List<Pair<ParameterContext, Provider<?>>> parameters = new ArrayList<>();
        for (int i = 0; i < params.length; i++) {
            Parameter param = params[i];
            Provider<?> provider = getProvider(param);
            if (provider == null) {
                throw new IllegalArgumentException("Could not find a Parameter Provider for " + param.getType().getName() + " in " + method.getName());
            }
            ParameterContext pCtx = new ParameterContext(params, i, param, param.getDeclaredAnnotations());
            if (provider.getOptionData(pCtx) != null) {
                for (CommandCreateAction action : actions) {
                    OptionData option = provider.getOptionData(pCtx);
                    if (!option.isRequired() && pCtx.isRequired())
                        option.setRequired(true);
                    action.addOptions(option);
                }
            }
            parameters.add(new Pair<>(pCtx, provider));
        }
        CommandWrapper wrapper = new CommandWrapper(command, name.toLowerCase(), method, o, params, parameters);
        for (CommandCreateAction action : actions) {
            action.queue((c) -> {
                System.out.println("Registered command " + c);
                commandMap.put(c.getName(), wrapper);
            });
        }
        System.out.println("Done Registering command " + name);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CommandCreateAction(net.dv8tion.jda.api.requests.restaction.CommandCreateAction) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CommandWrapper(net.badbird5907.jdacommand.util.object.CommandWrapper) Guild(net.dv8tion.jda.api.entities.Guild) OptionData(net.dv8tion.jda.api.interactions.commands.build.OptionData) Parameter(java.lang.reflect.Parameter) ParameterContext(net.badbird5907.jdacommand.context.ParameterContext) Disable(net.badbird5907.jdacommand.annotation.Disable) Pair(net.badbird5907.jdacommand.util.object.Pair)

Example 2 with ParameterContext

use of net.badbird5907.jdacommand.context.ParameterContext in project JDACommand by Badbird-5907.

the class CommandManager method process.

public static void process(Method cmd, SlashCommandInteractionEvent e, Object o, Command command) {
    CommandPreProcessEvent event = new CommandPreProcessEvent(command.name(), e);
    JDACommand.getEventBus().post(event);
    if (event.isCancelled())
        return;
    try {
        CommandWrapper wrapper = JDACommand.getCommandMap().get(command.name().toLowerCase());
        Object[] params = new Object[wrapper.getParams().length];
        CommandContext context = new CommandContext(e.getMember(), e, e.getMessageChannel(), e.getGuild());
        for (Pair<ParameterContext, Provider<?>> parameter : wrapper.getParameters()) {
            try {
                params[parameter.getValue0().getParameterIndex()] = parameter.getValue1().provide(context, parameter.getValue0());
            } catch (Exception ex) {
                if (parameter.getValue1().failOnException()) {
                    System.err.println("Failed to provide parameter " + parameter.getValue0().getParameterIndex() + " for command " + command.name());
                    throw ex;
                } else {
                    params[parameter.getValue0().getParameterIndex()] = parameter.getValue1().provideDefault(context, parameter.getValue0());
                }
            }
        }
        Object r = cmd.invoke(o, params);
        if (r instanceof CommandResult) {
            CommandResult result = (CommandResult) r;
            if (JDACommand.getOverrideCommandResult().get(result) != null) {
                Object obj = JDACommand.getOverrideCommandResult().get(result);
                if (obj instanceof String) {
                    e.reply(String.valueOf(obj)).queue();
                } else {
                    e.replyEmbeds((MessageEmbed) obj).queue();
                }
            } else if ((result != CommandResult.SUCCESS) && (result != CommandResult.OTHER) && result != null) {
                e.reply(result.getMessage()).queue();
            }
        } else if (r instanceof String) {
            e.reply(String.valueOf(r)).queue();
        } else if (r instanceof MessageEmbed) {
            e.replyEmbeds((MessageEmbed) r).queue();
        }
    } catch (Exception illegalAccessException) {
        illegalAccessException.printStackTrace();
    }
}
Also used : MessageEmbed(net.dv8tion.jda.api.entities.MessageEmbed) CommandContext(net.badbird5907.jdacommand.context.CommandContext) CommandPreProcessEvent(net.badbird5907.jdacommand.events.CommandPreProcessEvent) CommandWrapper(net.badbird5907.jdacommand.util.object.CommandWrapper) ParameterContext(net.badbird5907.jdacommand.context.ParameterContext) Provider(net.badbird5907.jdacommand.provider.Provider)

Aggregations

ParameterContext (net.badbird5907.jdacommand.context.ParameterContext)2 CommandWrapper (net.badbird5907.jdacommand.util.object.CommandWrapper)2 Parameter (java.lang.reflect.Parameter)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Disable (net.badbird5907.jdacommand.annotation.Disable)1 CommandContext (net.badbird5907.jdacommand.context.CommandContext)1 CommandPreProcessEvent (net.badbird5907.jdacommand.events.CommandPreProcessEvent)1 Provider (net.badbird5907.jdacommand.provider.Provider)1 Pair (net.badbird5907.jdacommand.util.object.Pair)1 Guild (net.dv8tion.jda.api.entities.Guild)1 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)1 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)1 CommandCreateAction (net.dv8tion.jda.api.requests.restaction.CommandCreateAction)1