Search in sources :

Example 1 with CommandWrapper

use of net.badbird5907.jdacommand.util.object.CommandWrapper 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 CommandWrapper

use of net.badbird5907.jdacommand.util.object.CommandWrapper in project JDACommand by Badbird-5907.

the class CommandListener method onSlashCommandInteraction.

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent e) {
    try {
        String command = e.getName();
        System.out.println("Command: " + e.getName());
        JDACommand.getCommandMap().forEach((name, pair) -> {
            if (e.getName().equalsIgnoreCase(name)) {
                // TODO custom error messages
                Command c = pair.getCommand();
                if (c.disable())
                    return;
                if (c.botOwnerOnly()) {
                    if (!JDACommand.getInstance().isOwner(e.getUser())) {
                        return;
                    }
                }
                if (c.serverOwnerOnly())
                    if (e.getChannelType() == ChannelType.TEXT && !e.getMember().isOwner())
                        return;
                if (c.dmsOnly())
                    if (e.getChannelType() != ChannelType.PRIVATE)
                        return;
                if (c.adminOnly())
                    if (e.getChannelType() == ChannelType.TEXT && !e.getMember().getPermissions().contains(Permission.ADMINISTRATOR))
                        return;
                if (c.permission().length != 0) {
                    if (e.getChannelType() == ChannelType.TEXT) {
                        Permission[] permissions = c.permission();
                        EnumSet<Permission> set = e.getMember().getPermissions();
                        for (Permission permission : permissions) {
                            if (!set.contains(permission)) {
                                return;
                            }
                        }
                    } else
                        return;
                }
                CommandWrapper wrapper = JDACommand.getCommandMap().get(command.toLowerCase());
                e.deferReply().queue(c1 -> {
                }, t -> {
                });
                CommandManager.process(pair.getMethod(), e, wrapper.getObject(), c);
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Command(net.badbird5907.jdacommand.annotation.Command) Permission(net.dv8tion.jda.api.Permission) CommandWrapper(net.badbird5907.jdacommand.util.object.CommandWrapper)

Example 3 with CommandWrapper

use of net.badbird5907.jdacommand.util.object.CommandWrapper 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

CommandWrapper (net.badbird5907.jdacommand.util.object.CommandWrapper)3 ParameterContext (net.badbird5907.jdacommand.context.ParameterContext)2 Parameter (java.lang.reflect.Parameter)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Command (net.badbird5907.jdacommand.annotation.Command)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 Permission (net.dv8tion.jda.api.Permission)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