Search in sources :

Example 1 with CommandContext

use of net.badbird5907.jdacommand.context.CommandContext 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

CommandContext (net.badbird5907.jdacommand.context.CommandContext)1 ParameterContext (net.badbird5907.jdacommand.context.ParameterContext)1 CommandPreProcessEvent (net.badbird5907.jdacommand.events.CommandPreProcessEvent)1 Provider (net.badbird5907.jdacommand.provider.Provider)1 CommandWrapper (net.badbird5907.jdacommand.util.object.CommandWrapper)1 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)1