use of net.badbird5907.jdacommand.provider.Provider 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.provider.Provider 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();
}
}
use of net.badbird5907.jdacommand.provider.Provider in project JDACommand by Badbird-5907.
the class JDACommand method init.
private void init() {
instance = this;
System.out.println("Initializing JDACommand...");
try {
jda.addEventListener(new CommandListener());
for (Provider provider : new Provider[] { new BooleanContextProvider(), new ChannelContextProvider(), new CommandContextProvider(), new EventContextProvider(), new GuildContextProvider(), new IntContextProvider(), new LongContextProvider(), new MemberContextProvider(), new RoleContextProvider(), new StringContextProvider(), new UserContextProvider() }) {
registerProvider(provider);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations