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();
}
}
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();
}
}
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();
}
}
Aggregations