impl basic help command
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 36s

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

@ -0,0 +1,9 @@
package cc.fascinated.bat;
/**
* @author Fascinated (fascinated7)
*/
public class Consts {
public static final String INVITE_URL = "https://discord.com/oauth2/authorize?client_id=1254161119975833652&permissions=8&integration_type=0&scope=bot+applications.commands";
public static final String SUPPORT_INVITE_URL = "https://discord.gg/invite/yjj2U3ctEG";
}

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

@ -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;
}
}

@ -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();
}
}

@ -1,5 +1,6 @@
package cc.fascinated.bat.command.impl; package cc.fascinated.bat.command.impl;
import cc.fascinated.bat.Consts;
import cc.fascinated.bat.command.BatCommand; import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
@ -15,8 +16,6 @@ import org.springframework.stereotype.Component;
*/ */
@Component @Component
public class InviteCommand extends BatCommand { 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() { public InviteCommand() {
super("invite", "Invite the bot to your server!"); super("invite", "Invite the bot to your server!");
} }
@ -24,7 +23,7 @@ public class InviteCommand extends BatCommand {
@Override @Override
public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) { public void execute(BatGuild guild, @NonNull BatUser user, @NonNull MessageChannel channel, Member member, @NonNull SlashCommandInteraction interaction) {
interaction.replyEmbeds(EmbedUtils.genericEmbed() 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()) .build())
.setEphemeral(true) .setEphemeral(true)
.queue(); .queue();

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

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

@ -8,6 +8,7 @@ import cc.fascinated.bat.model.token.beatsaber.scoresaber.ScoreSaberScoreToken;
import lombok.NonNull; import lombok.NonNull;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
/** /**
@ -47,4 +48,12 @@ public interface EventListener {
* @param user the user that sent the message * @param user the user that sent the message
*/ */
default void onGuildMessageReceive(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull MessageReceivedEvent event) {} default void onGuildMessageReceive(@NonNull BatGuild guild, @NonNull BatUser user, @NonNull MessageReceivedEvent event) {}
/**
* Called when a user selects a string
*
* @param guild the guild that the string was selected in
* @param user the user that selected the string
*/
default void onStringSelectInteraction(BatGuild guild, @NonNull BatUser user, @NonNull StringSelectInteractionEvent event) {}
} }

@ -1,7 +1,10 @@
package cc.fascinated.bat.features; package cc.fascinated.bat.features;
import lombok.AllArgsConstructor; import cc.fascinated.bat.command.BatCommand;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.service.CommandService;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -23,18 +26,13 @@ public abstract class Feature {
private final Category category; private final Category category;
/** /**
* The category of the feature * Registers the command for the feature
*
* @param commandService The command service
* @param command The command to register
*/ */
@AllArgsConstructor @Getter public void registerCommand(@NonNull CommandService commandService, @NonNull BatCommand command) {
public enum Category { command.setCategory(category);
GENERAL("General"), commandService.registerCommand(command);
MODERATION("Moderation"),
SERVER("Server"),
BEAT_SABER("Beat Saber");
/**
* The name of the category
*/
private final String name;
} }
} }

@ -1,5 +1,6 @@
package cc.fascinated.bat.features.afk; package cc.fascinated.bat.features.afk;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.afk.command.AfkCommand; import cc.fascinated.bat.features.afk.command.AfkCommand;
import cc.fascinated.bat.service.CommandService; import cc.fascinated.bat.service.CommandService;
@ -15,6 +16,6 @@ public class AfkFeature extends Feature {
public AfkFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService) { public AfkFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService) {
super("AFK", Category.GENERAL); super("AFK", Category.GENERAL);
commandService.registerCommand(context.getBean(AfkCommand.class)); registerCommand(commandService, context.getBean(AfkCommand.class));
} }
} }

@ -1,5 +1,6 @@
package cc.fascinated.bat.features.autorole; package cc.fascinated.bat.features.autorole;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.autorole.command.AutoRoleCommand; import cc.fascinated.bat.features.autorole.command.AutoRoleCommand;
import cc.fascinated.bat.service.CommandService; import cc.fascinated.bat.service.CommandService;

@ -1,5 +1,6 @@
package cc.fascinated.bat.features.birthday; package cc.fascinated.bat.features.birthday;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.features.Feature; import cc.fascinated.bat.features.Feature;
import cc.fascinated.bat.features.birthday.command.BirthdayCommand; import cc.fascinated.bat.features.birthday.command.BirthdayCommand;
import cc.fascinated.bat.features.birthday.profile.BirthdayProfile; import cc.fascinated.bat.features.birthday.profile.BirthdayProfile;
@ -22,7 +23,7 @@ public class BirthdayFeature extends Feature {
super("Birthday", Category.GENERAL); super("Birthday", Category.GENERAL);
this.guildService = guildService; this.guildService = guildService;
commandService.registerCommand(context.getBean(BirthdayCommand.class)); registerCommand(commandService, context.getBean(BirthdayCommand.class));
} }
/** /**

@ -1,5 +1,6 @@
package cc.fascinated.bat.features.scoresaber; package cc.fascinated.bat.features.scoresaber;
import cc.fascinated.bat.command.Category;
import cc.fascinated.bat.common.DateUtils; import cc.fascinated.bat.common.DateUtils;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.common.NumberUtils; import cc.fascinated.bat.common.NumberUtils;
@ -27,9 +28,9 @@ public class ScoreSaberFeature extends Feature {
public ScoreSaberFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService) { public ScoreSaberFeature(@NonNull ApplicationContext context, @NonNull CommandService commandService) {
super("ScoreSaber", Category.BEAT_SABER); super("ScoreSaber", Category.BEAT_SABER);
commandService.registerCommand(context.getBean(ScoreSaberCommand.class)); registerCommand(commandService, context.getBean(ScoreSaberCommand.class));
commandService.registerCommand(context.getBean(UserFeedCommand.class)); registerCommand(commandService, context.getBean(UserFeedCommand.class));
commandService.registerCommand(context.getBean(NumberOneFeedCommand.class)); registerCommand(commandService, context.getBean(NumberOneFeedCommand.class));
} }
/** /**

@ -6,6 +6,7 @@ import cc.fascinated.bat.command.BatSubCommand;
import cc.fascinated.bat.common.EmbedUtils; import cc.fascinated.bat.common.EmbedUtils;
import cc.fascinated.bat.model.BatGuild; import cc.fascinated.bat.model.BatGuild;
import cc.fascinated.bat.model.BatUser; import cc.fascinated.bat.model.BatUser;
import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
@ -13,6 +14,7 @@ import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.Command;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.DependsOn;
@ -27,7 +29,7 @@ import java.util.Map;
* @author Fascinated (fascinated7) * @author Fascinated (fascinated7)
*/ */
@Service @Service
@Log4j2 @Log4j2 @Getter
@DependsOn("discordService") @DependsOn("discordService")
public class CommandService extends ListenerAdapter { public class CommandService extends ListenerAdapter {
/** /**
@ -84,7 +86,10 @@ public class CommandService extends ListenerAdapter {
}); });
// Register all commands // Register all commands
jda.updateCommands().addCommands(commands.values().stream().map(BatCommand::getCommandData).toList()).complete(); List<Command> discordCommands = jda.updateCommands().addCommands(commands.values().stream().map(BatCommand::getCommandData).toList()).complete();
for (Command discordCommand : discordCommands) {
commands.get(discordCommand.getName()).setCommandSnowflake(discordCommand.getIdLong());
}
log.info("Registered all slash commands in {}ms", System.currentTimeMillis() - before); log.info("Registered all slash commands in {}ms", System.currentTimeMillis() - before);
} }

@ -7,6 +7,7 @@ import lombok.NonNull;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberRemoveEvent;
import net.dv8tion.jda.api.events.interaction.component.StringSelectInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter; import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -89,4 +90,17 @@ public class EventService extends ListenerAdapter {
listener.onGuildMessageReceive(guild, user, event); listener.onGuildMessageReceive(guild, user, event);
} }
} }
@Override
public void onStringSelectInteraction(StringSelectInteractionEvent event) {
if (event.getUser().isBot()) {
return;
}
BatGuild guild = event.getGuild() != null ? guildService.getGuild(event.getGuild().getId()) : null;
BatUser user = userService.getUser(event.getUser().getId());
for (EventListener listener : LISTENERS) {
listener.onStringSelectInteraction(guild, user, event);
}
}
} }