impl basic help command

This commit is contained in:
Lee
2024-06-27 16:01:27 +01:00
parent 175c8eba9f
commit a7c3e2d745
15 changed files with 219 additions and 30 deletions

View File

@ -18,6 +18,11 @@ import java.util.Map;
*/
@Getter @Setter
public abstract class BatCommand implements BatCommandExecutor {
/**
* The category of the command
*/
private Category category;
/**
* The name of the command
*/
@ -43,7 +48,13 @@ public abstract class BatCommand implements BatCommandExecutor {
*/
private final Map<String, BatSubCommand> subCommands = new HashMap<>();
public BatCommand(@NonNull String name, @NonNull String description, boolean guildOnly, Permission... permissions) {
/**
* The commands snowflake from Discord
*/
private long commandSnowflake;
public BatCommand(@NonNull Category category, @NonNull String name, @NonNull String description, boolean guildOnly, Permission... permissions) {
this.category = category;
this.name = name;
this.description = description;
this.requiredPermissions = List.of(permissions);
@ -52,12 +63,24 @@ public abstract class BatCommand implements BatCommandExecutor {
.setGuildOnly(guildOnly);
}
public BatCommand(@NonNull Category category, @NonNull String name) {
this(category, name, "No description provided.", false);
}
public BatCommand(@NonNull Category category, @NonNull String name, @NonNull String description) {
this(category, name, description, false);
}
public BatCommand(@NonNull String name, @NonNull String description, boolean guildOnly, Permission... permissions) {
this(Category.GENERAL, name, description, guildOnly, permissions);
}
public BatCommand(@NonNull String name) {
this(name, "No description provided.", false);
this(Category.GENERAL, name, "No description provided.", false);
}
public BatCommand(@NonNull String name, @NonNull String description) {
this(name, description, false);
this(Category.GENERAL, name, description, false);
}
/**

View File

@ -0,0 +1,39 @@
package cc.fascinated.bat.command;
/**
* @author Fascinated (fascinated7)
*/
import lombok.AllArgsConstructor;
import lombok.Getter;
import net.dv8tion.jda.api.entities.emoji.Emoji;
/**
* The category of the command
*/
@AllArgsConstructor @Getter
public enum Category {
GENERAL(Emoji.fromUnicode("U+2699"), "General"),
FUN(Emoji.fromFormatted("U+1F973"), "Fun"),
SERVER(Emoji.fromFormatted("U+1F5A5"), "Server"),
BEAT_SABER(Emoji.fromFormatted("U+1FA84"), "Beat Saber");
/**
* The emoji for the category
*/
private final Emoji emoji;
/**
* The name of the category
*/
private final String name;
public static Category getByName(String name) {
for (Category category : Category.values()) {
if (category.getName().equalsIgnoreCase(name)) {
return category;
}
}
return null;
}
}

View File

@ -0,0 +1,89 @@
package cc.fascinated.bat.command.impl;
import cc.fascinated.bat.Consts;
import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.event.EventListener;
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.service.CommandService;
import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
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.buttons.ButtonStyle;
import net.dv8tion.jda.api.interactions.components.selections.SelectOption;
import net.dv8tion.jda.api.interactions.components.selections.StringSelectMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* @author Fascinated (fascinated7)
*/
@Component
public class HelpCommand extends BatCommand implements EventListener {
private final CommandService commandService;
@Autowired
public HelpCommand(@NonNull CommandService commandService) {
super("help", "View the bots command categories.");
this.commandService = commandService;
}
@Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
String categories = "";
for (Category category : Category.values()) {
long commandCount = commandService.getCommands().values().stream().filter(command -> command.getCategory() == category).count();
categories += "**%s** - %s Commands\n".formatted(category.getName(), commandCount);
}
SelectOption[] options = Arrays.stream(Category.values()).map(category ->
SelectOption.of(category.getName(), category.getName()).withEmoji(category.getEmoji()))
.toArray(SelectOption[]::new);
interaction.replyEmbeds(EmbedUtils.genericEmbed()
.setDescription("Here are the available command categories: \n\n" + categories)
.build()).addComponents(
ActionRow.of(
Button.of(ButtonStyle.LINK, Consts.INVITE_URL, "Invite"),
Button.of(ButtonStyle.LINK, Consts.SUPPORT_INVITE_URL, "Support")
),
ActionRow.of(
StringSelectMenu.create("help-menu")
.addOptions(options)
.build()
)
).queue();
}
@Override
public void onStringSelectInteraction(BatGuild guild, @NonNull BatUser user, @NonNull StringSelectInteractionEvent event) {
Category category = Category.getByName(event.getSelectedOptions().get(0).getValue());
if (category == null) {
event.reply("Invalid category selected.").queue();
return;
}
String commands = "";
for (BatCommand command : commandService.getCommands().values()) {
if (command.getCategory() == category) {
commands += "</%s:%s> - %s\n".formatted(
command.getName(),
command.getCommandSnowflake(),
command.getDescription()
);
}
}
event.editMessageEmbeds(EmbedUtils.genericEmbed()
.setDescription("The available commands in the **%s** category: \n\n%s".formatted(category.getName(), commands))
.build()).queue();
}
}

View File

@ -1,5 +1,6 @@
package cc.fascinated.bat.command.impl;
import cc.fascinated.bat.Consts;
import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.model.BatGuild;
@ -15,8 +16,6 @@ import org.springframework.stereotype.Component;
*/
@Component
public class InviteCommand extends BatCommand {
private static final String INVITE_URL = "https://discord.com/oauth2/authorize?client_id=1254161119975833652&permissions=8&integration_type=0&scope=bot+applications.commands";
public InviteCommand() {
super("invite", "Invite the bot to your server!");
}
@ -24,7 +23,7 @@ public class InviteCommand extends BatCommand {
@Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
interaction.replyEmbeds(EmbedUtils.genericEmbed()
.setDescription("You can invite the bot to your server by clicking [here](%s)".formatted(INVITE_URL))
.setDescription("You can invite the bot to your server by clicking [here](%s)".formatted(Consts.INVITE_URL))
.build())
.setEphemeral(true)
.queue();

View File

@ -1,6 +1,7 @@
package cc.fascinated.bat.command.impl.fun;
import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.WebRequest;
import cc.fascinated.bat.model.BatGuild;
@ -10,7 +11,6 @@ import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.interactions.commands.SlashCommandInteraction;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
/**
@ -19,7 +19,7 @@ import org.springframework.stereotype.Component;
@Component
public class CatCommand extends BatCommand {
public CatCommand() {
super("cat", "Get a random cat image");
super(Category.FUN, "cat", "Get a random cat image");
}
@Override

View File

@ -1,12 +1,12 @@
package cc.fascinated.bat.command.impl.fun;
import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.WebRequest;
import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser;
import cc.fascinated.bat.model.token.dogceo.RandomImage;
import cc.fascinated.bat.model.token.thecatapi.CatImageToken;
import lombok.NonNull;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
@ -19,7 +19,7 @@ import org.springframework.stereotype.Component;
@Component
public class DogCommand extends BatCommand {
public DogCommand() {
super("dog", "Get a random dog image");
super(Category.FUN, "dog", "Get a random dog image");
}
@Override