fix interactions
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m19s

This commit is contained in:
Lee
2024-07-09 23:02:12 +01:00
parent f1bc2b2aaa
commit 96e7518f72
9 changed files with 319 additions and 212 deletions

View File

@ -5,6 +5,8 @@ import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.CommandInfo;
import cc.fascinated.bat.common.DescriptionBuilder;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.InteractionBuilder;
import cc.fascinated.bat.common.StringUtils;
import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.features.logging.LogCategory;
import cc.fascinated.bat.features.logging.LogProfile;
@ -17,9 +19,11 @@ import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import net.dv8tion.jda.api.interactions.components.ActionRow;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.interactions.components.selections.SelectOption;
import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
@ -33,62 +37,36 @@ import java.util.List;
public class ListSubCommand extends BatCommand implements EventListener {
@Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction event) {
List<Button> buttons = new ArrayList<>();
buttons.add(Button.primary("logs-home", "Home").withEmoji(Emojis.HOME_EMOJI));
for (LogCategory category : LogCategory.values()) {
buttons.add(Button.primary("logs:list:%s".formatted(category.name().toLowerCase()), category.getName()).withEmoji(category.getEmoji()));
}
event.replyEmbeds(getHelpMessage()).addComponents(ActionRow.of(buttons)).queue();
}
@Override
public void onButtonInteraction(BatGuild guild, @NonNull BatUser user, @NonNull ButtonInteractionEvent event) {
if (guild == null || event.getMember() == null) {
return;
}
// Not a log button
if (!event.getComponentId().startsWith("logs")) {
return;
}
// No permissions
if (!event.getMember().hasPermission(this.getInfo().getPermissions())) {
event.reply("%s, you cannot use this button.".formatted(user.getDiscordUser().getAsMention()))
.setEphemeral(true)
.queue();
return;
}
// Home
if (event.getComponentId().equals("logs-home")) {
event.editMessageEmbeds(getHelpMessage()).queue();
return;
}
// Category
String[] split = event.getComponentId().split(":");
if (split.length != 3) {
return;
}
LogProfile profile = guild.getLogProfile();
LogCategory category = LogCategory.getLogCategory(split[2].toUpperCase());
if (category == null) {
return;
InteractionBuilder interactionBuilder = new InteractionBuilder();
interactionBuilder.addStringSelect("Home", "Return Home", Emojis.HOME_EMOJI, (buttonEvent) -> {
buttonEvent.editMessageEmbeds(getHelpMessage()).queue();
});
for (LogCategory category : LogCategory.values()) {
List<LogType> logEvents = LogType.getLogTypesByCategory(category.getName());
DescriptionBuilder description = new DescriptionBuilder(null);
description.appendLine("Log channels for the `%s` category".formatted(category.getName()), false);
description.emptyLine();
for (LogType logType : logEvents) {
if (logType.getCategory() != category) {
continue;
}
TextChannel logChannel = profile.getLogChannel(logType);
description.appendLine("%s: %s".formatted(logType.getName(), logChannel == null ? "Not Set" : logChannel.getAsMention()), true);
}
interactionBuilder.addStringSelect(
category.getName(),
"View log events in the %s category".formatted(category.getName()),
category.getEmoji(),
(buttonEvent) -> buttonEvent.editMessageEmbeds(EmbedUtils.genericEmbed().setDescription(description.build()).build()).queue()
);
}
List<LogType> logEvents = LogType.getLogTypesByCategory(category.getName());
DescriptionBuilder description = new DescriptionBuilder(null);
description.appendLine("Log channels for the `%s` category".formatted(category.getName()), false);
description.emptyLine();
for (LogType logType : logEvents) {
if (logType.getCategory() != category) {
continue;
}
TextChannel logChannel = profile.getLogChannel(logType);
description.appendLine("%s: %s".formatted(logType.getName(), logChannel == null ? "Not Set" : logChannel.getAsMention()), true);
}
event.editMessageEmbeds(EmbedUtils.genericEmbed().setDescription(description.build()).build()).queue();
event.replyEmbeds(getHelpMessage()).addComponents(interactionBuilder.build()).queue();
}
/**
@ -106,7 +84,7 @@ public class ListSubCommand extends BatCommand implements EventListener {
To remove a log channel, it's the same as setting it,
but with `/logs remove` instead of `/logs set`
\s
*Use the buttons below to see the log events for each category*
*Use the menu below to see the log events for each category*
""", false).build()).build();
}
}