use of com.freya02.botcommands.api.application.slash.GuildSlashEvent in project BotCommands by freya022.
the class SlashCommandInfo method execute.
public boolean execute(BContext context, SlashCommandInteractionEvent event, Consumer<Throwable> throwableConsumer) throws Exception {
List<Object> objects = new ArrayList<>(commandParameters.size() + 1) {
{
if (guildOnly) {
add(new GuildSlashEvent(context, event));
} else {
add(new GlobalSlashEventImpl(context, event));
}
}
};
int optionIndex = 0;
final List<String> optionNames = event.getGuild() != null ? getLocalizedOptions(event.getGuild()) : null;
for (final SlashCommandParameter parameter : commandParameters) {
final ApplicationOptionData applicationOptionData = parameter.getApplicationOptionData();
final Object obj;
if (parameter.isOption()) {
String optionName = optionNames == null ? applicationOptionData.getEffectiveName() : optionNames.get(optionIndex);
if (optionName == null) {
throw new IllegalArgumentException(String.format("Option name #%d (%s) could not be resolved for %s", optionIndex, applicationOptionData.getEffectiveName(), Utils.formatMethodShort(getMethod())));
}
optionIndex++;
final OptionMapping optionMapping = event.getOption(optionName);
if (optionMapping == null) {
if (parameter.isOptional()) {
if (parameter.isPrimitive()) {
objects.add(0);
} else {
objects.add(null);
}
continue;
} else {
throw new RuntimeException("Slash parameter couldn't be resolved for method " + Utils.formatMethodShort(commandMethod) + " at parameter " + applicationOptionData.getEffectiveName() + " (localized '" + optionName + "')");
}
}
obj = parameter.getResolver().resolve(context, this, event, optionMapping);
if (obj == null) {
event.replyFormat(context.getDefaultMessages(event.getGuild()).getSlashCommandUnresolvableParameterMsg(), applicationOptionData.getEffectiveName(), parameter.getBoxedType().getSimpleName()).setEphemeral(true).queue();
// Not a warning, could be normal if the user did not supply a valid string for user-defined resolvers
LOGGER.trace("The parameter '{}' of value '{}' could not be resolved into a {}", applicationOptionData.getEffectiveName(), optionMapping.getAsString(), parameter.getBoxedType().getSimpleName());
return false;
}
if (!parameter.getBoxedType().isAssignableFrom(obj.getClass())) {
event.replyFormat(context.getDefaultMessages(event.getGuild()).getSlashCommandInvalidParameterTypeMsg(), applicationOptionData.getEffectiveName(), parameter.getBoxedType().getSimpleName(), obj.getClass().getSimpleName()).setEphemeral(true).queue();
LOGGER.error("The parameter '{}' of value '{}' is not a valid type (expected a {})", applicationOptionData.getEffectiveName(), optionMapping.getAsString(), parameter.getBoxedType().getSimpleName());
return false;
}
} else {
obj = parameter.getCustomResolver().resolve(context, this, event);
}
// For some reason using an array list instead of a regular array
// magically unboxes primitives when passed to Method#invoke
objects.add(obj);
}
applyCooldown(event);
getMethodRunner().invoke(objects.toArray(), throwableConsumer);
return true;
}
use of com.freya02.botcommands.api.application.slash.GuildSlashEvent in project BotCommands by freya022.
the class PaginatorCommand method run.
@JDASlashCommand(name = "paginator")
public void run(GuildSlashEvent event) {
final List<EmbedBuilder> embedBuilders = new ArrayList<>();
// Let's suppose you generated embeds like in JDA-U, so you'd have a collection of embeds to present
for (int i = 0; i < 5; i++) {
embedBuilders.add(new EmbedBuilder().setTitle("Page #" + (i + 1)));
}
final Paginator paginator = new PaginatorBuilder().setConstraints(InteractionConstraints.ofUsers(event.getUser())).useDeleteButton(false).setMaxPages(5).setPaginatorSupplier((instance, messageBuilder, components, page) -> embedBuilders.get(page).build()).build();
// You must send the paginator as a message
event.reply(paginator.get()).setEphemeral(true).queue();
}
use of com.freya02.botcommands.api.application.slash.GuildSlashEvent in project BotCommands by freya022.
the class ApplicationCommandsBuilder method processSlashCommand.
private void processSlashCommand(ApplicationCommand applicationCommand, Method method) {
if (method.getAnnotation(JDASlashCommand.class).guildOnly()) {
if (!ReflectionUtils.hasFirstParameter(method, GlobalSlashEvent.class) && !ReflectionUtils.hasFirstParameter(method, GuildSlashEvent.class))
throw new IllegalArgumentException("Slash command at " + Utils.formatMethodShort(method) + " must have a GuildSlashEvent or GlobalSlashEvent as first parameter");
if (!ReflectionUtils.hasFirstParameter(method, GuildSlashEvent.class)) {
// If type is correct but guild specialization isn't used
LOGGER.warn("Guild-only slash command {} uses GlobalSlashEvent, consider using GuildSlashEvent to remove warnings related to guild stuff's nullability", Utils.formatMethodShort(method));
}
} else {
if (!ReflectionUtils.hasFirstParameter(method, GlobalSlashEvent.class))
throw new IllegalArgumentException("Slash command at " + Utils.formatMethodShort(method) + " must have a GlobalSlashEvent as first parameter");
}
final SlashCommandInfo info = new SlashCommandInfo(context, applicationCommand, method);
LOGGER.debug("Adding slash command path {} for method {}", info.getPath(), Utils.formatMethodShort(method));
context.addSlashCommand(info);
}
use of com.freya02.botcommands.api.application.slash.GuildSlashEvent in project BotCommands by freya022.
the class SlashInteractiveMenu method interactiveMenu.
@JDASlashCommand(name = "interactive")
public void interactiveMenu(GuildSlashEvent event) {
final InteractiveMenu menu = new InteractiveMenuBuilder().addMenu(SelectContent.of("Joy", "This sparks joy", Emoji.fromMarkdown("\uD83D\uDE02")), (interactiveMenu, messageBuilder, components) -> {
components.addComponents(1, Components.dangerButton(buttonEvent -> {
event.getHook().deleteOriginal().queue();
interactiveMenu.cancelTimeout();
interactiveMenu.cleanup(event.getContext());
}).build("Delete"), Components.secondaryButton(buttonEvent -> {
interactiveMenu.setSelectedItem("Grin");
buttonEvent.editMessage(interactiveMenu.get()).queue();
}).build("Go to 'Grin'"));
return new EmbedBuilder().setTitle("This sparks joy").build();
}).addMenu(SelectContent.of("Grin", "This does not spark joy", Emoji.fromMarkdown("\uD83D\uDE00")), (interactiveMenu, messageBuilder, components) -> {
components.addComponents(1, Components.dangerButton(buttonEvent -> {
event.getHook().deleteOriginal().queue();
interactiveMenu.cancelTimeout();
interactiveMenu.cleanup(event.getContext());
}).build("Delete"), Components.secondaryButton(buttonEvent -> {
interactiveMenu.setSelectedItem(0);
buttonEvent.editMessage(interactiveMenu.get()).queue();
}).build("Go to 'Joy'"));
return new EmbedBuilder().setTitle("This does not spark joy").build();
}).setConstraints(InteractionConstraints.ofUsers(event.getUser())).setTimeout(5, TimeUnit.SECONDS, (interactiveMenu, msg) -> {
System.out.println("bru");
interactiveMenu.cleanup(event.getContext());
}).build();
event.reply(menu.get()).setEphemeral(false).queue();
}
use of com.freya02.botcommands.api.application.slash.GuildSlashEvent in project BotCommands by freya022.
the class SlashPaginator method replyPaginator.
private void replyPaginator(GuildSlashEvent event, BContext context, InteractionConstraints constraints) {
final PaginatorBuilder builder = new PaginatorBuilder().setConstraints(constraints).useDeleteButton(true).setTimeout(5, TimeUnit.SECONDS, (paginator, message) -> {
paginator.cleanup(context);
// Remove components on timeout
event.getHook().editOriginalComponents().queue();
// Disable all components on timeout, more expensive
// event.getHook()
// .retrieveOriginal()
// .flatMap(m -> m.editMessageComponents(m.getActionRows().stream().map(ActionRow::asDisabled).toList()))
// .queue();
}).setMaxPages(5).setFirstContent(ButtonContent.withString("←")).setPaginatorSupplier((paginator, messageBuilder, components, page) -> {
components.addComponents(1, Components.primaryButton(btnEvt -> {
// Pages starts at 0
paginator.setPage(2);
btnEvt.editMessage(paginator.get()).queue();
}).build(ButtonContent.withEmoji("Go to page 3", EmojiUtils.resolveJDAEmoji("page_facing_up"))));
components.addComponents(1, Components.primaryButton(btnEvt -> {
// Pages starts at 0
paginator.setPage(4);
btnEvt.editMessage(paginator.get()).queue();
}).build(ButtonContent.withEmoji("Go to page 5", EmojiUtils.resolveJDAEmoji("page_facing_up"))));
return new EmbedBuilder().setTitle(// Pages starts at 0
"Page #" + (page + 1)).build();
});
final Paginator paginator = builder.build();
event.reply(paginator.get()).setEphemeral(false).queue();
}
Aggregations