update help cmd for running outside a guild

This commit is contained in:
Lee 2024-07-05 23:21:28 +01:00
parent 650556079b
commit 4aae21b594
2 changed files with 24 additions and 10 deletions

@ -45,7 +45,7 @@ public class HelpCommand extends BatCommand implements EventListener {
@Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction event) {
event.replyEmbeds(createHomeEmbed()).addComponents(createHomeActions()).queue();
event.replyEmbeds(createHomeEmbed(event.isFromGuild())).addComponents(createHomeActions(event.isFromGuild())).queue();
}
@Override
@ -56,8 +56,8 @@ public class HelpCommand extends BatCommand implements EventListener {
// Get the item
String item = event.getSelectedOptions().get(0).getValue();
if (item.equalsIgnoreCase("home")) {
event.editMessageEmbeds(createHomeEmbed()).queue();
if (item.equalsIgnoreCase("help-home")) {
event.editMessageEmbeds(createHomeEmbed(event.isFromGuild())).queue();
return;
}
@ -72,7 +72,7 @@ public class HelpCommand extends BatCommand implements EventListener {
// Send the category
StringBuilder commands = new StringBuilder();
List<BatCommand> categoryCommands = commandService.getCommandsByCategory(category);
List<BatCommand> categoryCommands = commandService.getCommandsByCategory(category, event.isFromGuild());
if (categoryCommands.isEmpty()) {
commands = new StringBuilder("No commands available in this category.");
} else {
@ -114,10 +114,13 @@ public class HelpCommand extends BatCommand implements EventListener {
*
* @return The home embed
*/
private MessageEmbed createHomeEmbed() {
private MessageEmbed createHomeEmbed(boolean ranInsideGuild) {
StringBuilder categories = new StringBuilder();
for (Category category : Category.values()) {
long commandCount = commandService.getCommandsByCategory(category).size();
if (commandService.getCommandsByCategory(category, ranInsideGuild).isEmpty()) {
continue;
}
long commandCount = commandService.getCommandsByCategory(category, ranInsideGuild).size();
categories.append("➜ %s - **%s Command%s**\n".formatted(
category.getName(),
commandCount,
@ -127,11 +130,12 @@ public class HelpCommand extends BatCommand implements EventListener {
return EmbedUtils.genericEmbed()
.setDescription("""
**Welcome to the Bat Help Menu!**
**Welcome to the Bat Help Menu!**%s
%s
*View our [TOS](%s) and [Privacy Policy](%s) for more information.*
""".formatted(
!ranInsideGuild ? "\n*guild only commands won't be shown here" : "",
categories.toString(),
Consts.TERMS_OF_SERVICE_URL,
Consts.PRIVACY_POLICY_URL
@ -144,10 +148,13 @@ public class HelpCommand extends BatCommand implements EventListener {
*
* @return The layout components
*/
private LayoutComponent[] createHomeActions() {
private LayoutComponent[] createHomeActions(boolean ranInsideGuild) {
List<SelectOption> options = new ArrayList<>();
options.add(SelectOption.of("Home", "help-home").withEmoji(Emoji.fromUnicode("U+1F3E0")));
for (Category category : Category.values()) {
if (commandService.getCommandsByCategory(category, ranInsideGuild).isEmpty()) {
continue;
}
options.add(SelectOption.of(category.getName(), "help-" + category.getName()).withEmoji(category.getEmoji()));
}

@ -134,10 +134,17 @@ public class CommandService extends ListenerAdapter {
* Gets commands that are in a specific category
*
* @param category The category
* @param includeGuildCommands If guild commands should be included
* @return The commands
*/
public List<BatCommand> getCommandsByCategory(Category category) {
return commands.values().stream().filter(command -> command.getInfo().getCategory() == category).toList();
public List<BatCommand> getCommandsByCategory(Category category, boolean includeGuildCommands) {
List<BatCommand> commands = new ArrayList<>();
for (BatCommand command : this.commands.values()) {
if (command.getInfo().getCategory() == category && (includeGuildCommands || !command.getInfo().isGuildOnly())) {
commands.add(command);
}
}
return commands;
}
/**