[AB-xxx] changing custom command auto complete to be a contains with ignored case

adding auto complete to delete custom commands
This commit is contained in:
Sheldan
2025-02-16 22:53:18 +01:00
parent 8544c0c2b1
commit 99c4c3e59c
7 changed files with 56 additions and 14 deletions

View File

@@ -11,12 +11,20 @@ import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService; import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.interaction.slash.SlashCommandConfig; import dev.sheldan.abstracto.core.interaction.slash.SlashCommandConfig;
import dev.sheldan.abstracto.core.interaction.slash.SlashCommandPrivilegeLevels; import dev.sheldan.abstracto.core.interaction.slash.SlashCommandPrivilegeLevels;
import dev.sheldan.abstracto.core.interaction.slash.parameter.SlashCommandAutoCompleteService;
import dev.sheldan.abstracto.core.interaction.slash.parameter.SlashCommandParameterService; import dev.sheldan.abstracto.core.interaction.slash.parameter.SlashCommandParameterService;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserManagementService;
import dev.sheldan.abstracto.core.utils.ContextUtils; import dev.sheldan.abstracto.core.utils.ContextUtils;
import dev.sheldan.abstracto.customcommand.config.CustomCommandFeatureDefinition; import dev.sheldan.abstracto.customcommand.config.CustomCommandFeatureDefinition;
import dev.sheldan.abstracto.customcommand.config.CustomCommandSlashCommandNames; import dev.sheldan.abstracto.customcommand.config.CustomCommandSlashCommandNames;
import dev.sheldan.abstracto.customcommand.service.management.CustomCommandManagementService;
import dev.sheldan.abstracto.customcommand.service.management.CustomCommandService; import dev.sheldan.abstracto.customcommand.service.management.CustomCommandService;
import java.util.ArrayList;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@@ -39,9 +47,21 @@ public class DeleteCustomCommand extends AbstractConditionableCommand {
@Autowired @Autowired
private CustomCommandService customCommandService; private CustomCommandService customCommandService;
@Autowired
private CustomCommandManagementService customCommandManagementService;
@Autowired @Autowired
private InteractionService interactionService; private InteractionService interactionService;
@Autowired
private SlashCommandAutoCompleteService slashCommandAutoCompleteService;
@Autowired
private ServerManagementService serverManagementService;
@Autowired
private UserManagementService userManagementService;
@Override @Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) { public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
String name = slashCommandParameterService.getCommandOption(CUSTOM_COMMAND_NAME_PARAMETER, event, String.class); String name = slashCommandParameterService.getCommandOption(CUSTOM_COMMAND_NAME_PARAMETER, event, String.class);
@@ -54,12 +74,32 @@ public class DeleteCustomCommand extends AbstractConditionableCommand {
.thenApply(interactionHook -> CommandResult.fromSuccess()); .thenApply(interactionHook -> CommandResult.fromSuccess());
} }
@Override
public List<String> performAutoComplete(CommandAutoCompleteInteractionEvent event) {
if(slashCommandAutoCompleteService.matchesParameter(event.getFocusedOption(), CUSTOM_COMMAND_NAME_PARAMETER)) {
String input = event.getFocusedOption().getValue();
if(ContextUtils.isUserCommand(event)) {
AUser user = userManagementService.loadUser(event.getUser().getIdLong());
return customCommandManagementService.getUserCustomCommandsContaining(input, user)
.stream().map(customCommand -> customCommand.getName().toLowerCase())
.toList();
} else {
AServer server = serverManagementService.loadServer(event.getGuild());
return customCommandManagementService.getCustomCommandsContaining(input, server)
.stream().map(customCommand -> customCommand.getName().toLowerCase())
.toList();
}
}
return new ArrayList<>();
}
@Override @Override
public CommandConfiguration getConfiguration() { public CommandConfiguration getConfiguration() {
Parameter commandNameParameter = Parameter Parameter commandNameParameter = Parameter
.builder() .builder()
.name(CUSTOM_COMMAND_NAME_PARAMETER) .name(CUSTOM_COMMAND_NAME_PARAMETER)
.templated(true) .templated(true)
.supportsAutoComplete(true)
.type(String.class) .type(String.class)
.build(); .build();

View File

@@ -69,12 +69,12 @@ public class GetCustomCommand extends AbstractConditionableCommand {
if(slashCommandAutoCompleteService.matchesParameter(event.getFocusedOption(), CUSTOM_COMMAND_NAME_PARAMETER)) { if(slashCommandAutoCompleteService.matchesParameter(event.getFocusedOption(), CUSTOM_COMMAND_NAME_PARAMETER)) {
String input = event.getFocusedOption().getValue(); String input = event.getFocusedOption().getValue();
if(ContextUtils.isNotUserCommand(event)) { if(ContextUtils.isNotUserCommand(event)) {
return customCommandService.getCustomCommandsStartingWith(input, event.getGuild()) return customCommandService.getCustomCommandsContaining(input, event.getGuild())
.stream() .stream()
.map(CustomCommand::getName) .map(CustomCommand::getName)
.toList(); .toList();
} else { } else {
return customCommandService.getUserCustomCommandsStartingWith(input, event.getUser()) return customCommandService.getUserCustomCommandsContaining(input, event.getUser())
.stream() .stream()
.map(CustomCommand::getName) .map(CustomCommand::getName)
.toList(); .toList();

View File

@@ -20,4 +20,6 @@ public interface CustomCommandRepository extends JpaRepository<CustomCommand, Lo
List<CustomCommand> findByCreatorUserAndUserSpecific(AUser user, Boolean userSpecific); List<CustomCommand> findByCreatorUserAndUserSpecific(AUser user, Boolean userSpecific);
List<CustomCommand> findByNameStartsWithIgnoreCaseAndServer(String prefix, AServer server); List<CustomCommand> findByNameStartsWithIgnoreCaseAndServer(String prefix, AServer server);
List<CustomCommand> findByNameStartsWithIgnoreCaseAndCreatorUserAndUserSpecific(String prefix, AUser aUser, Boolean userSpecific); List<CustomCommand> findByNameStartsWithIgnoreCaseAndCreatorUserAndUserSpecific(String prefix, AUser aUser, Boolean userSpecific);
List<CustomCommand> findByNameContainingIgnoreCaseAndServer(String name, AServer server);
List<CustomCommand> findByNameContainingIgnoreCaseAndCreatorUserAndUserSpecific(String name, AUser user, Boolean userSpecific);
} }

View File

@@ -95,14 +95,14 @@ public class CustomCommandServiceBean implements CustomCommandService {
} }
@Override @Override
public List<CustomCommand> getCustomCommandsStartingWith(String prefix, Guild guild) { public List<CustomCommand> getCustomCommandsContaining(String name, Guild guild) {
AServer server = serverManagementService.loadServer(guild); AServer server = serverManagementService.loadServer(guild);
return customCommandManagementService.getCustomCommandsStartingWith(prefix, server); return customCommandManagementService.getCustomCommandsContaining(name, server);
} }
@Override @Override
public List<CustomCommand> getUserCustomCommandsStartingWith(String prefix, User user) { public List<CustomCommand> getUserCustomCommandsContaining(String name, User user) {
AUser aUser = userManagementService.loadOrCreateUser(user.getIdLong()); AUser aUser = userManagementService.loadOrCreateUser(user.getIdLong());
return customCommandManagementService.getUserCustomCommandsStartingWith(prefix, aUser); return customCommandManagementService.getUserCustomCommandsContaining(name, aUser);
} }
} }

View File

@@ -84,13 +84,13 @@ public class CustomCommandManagementServiceBean implements CustomCommandManageme
} }
@Override @Override
public List<CustomCommand> getCustomCommandsStartingWith(String prefix, AServer server) { public List<CustomCommand> getCustomCommandsContaining(String name, AServer server) {
return repository.findByNameStartsWithIgnoreCaseAndServer(prefix, server); return repository.findByNameContainingIgnoreCaseAndServer(name, server);
} }
@Override @Override
public List<CustomCommand> getUserCustomCommandsStartingWith(String prefix, AUser aUser) { public List<CustomCommand> getUserCustomCommandsContaining(String prefix, AUser aUser) {
return repository.findByNameStartsWithIgnoreCaseAndCreatorUserAndUserSpecific(prefix, aUser, true); return repository.findByNameContainingIgnoreCaseAndCreatorUserAndUserSpecific(prefix, aUser, true);
} }
} }

View File

@@ -18,6 +18,6 @@ public interface CustomCommandManagementService {
void deleteCustomCommand(String name, AUser user); void deleteCustomCommand(String name, AUser user);
List<CustomCommand> getCustomCommands(AServer server); List<CustomCommand> getCustomCommands(AServer server);
List<CustomCommand> getUserCustomCommands(AUser aUser); List<CustomCommand> getUserCustomCommands(AUser aUser);
List<CustomCommand> getCustomCommandsStartingWith(String prefix, AServer server); List<CustomCommand> getCustomCommandsContaining(String prefix, AServer server);
List<CustomCommand> getUserCustomCommandsStartingWith(String prefix, AUser aUser); List<CustomCommand> getUserCustomCommandsContaining(String prefix, AUser aUser);
} }

View File

@@ -16,6 +16,6 @@ public interface CustomCommandService {
List<CustomCommand> getUserCustomCommands(User user); List<CustomCommand> getUserCustomCommands(User user);
CustomCommand getCustomCommand(String name, Guild guild); CustomCommand getCustomCommand(String name, Guild guild);
CustomCommand getUserCustomCommand(String name, User user); CustomCommand getUserCustomCommand(String name, User user);
List<CustomCommand> getCustomCommandsStartingWith(String prefix, Guild guild); List<CustomCommand> getCustomCommandsContaining(String name, Guild guild);
List<CustomCommand> getUserCustomCommandsStartingWith(String prefix, User user); List<CustomCommand> getUserCustomCommandsContaining(String name, User user);
} }