From 08cd640dd16591d5436e994d981bce88edc90ba4 Mon Sep 17 00:00:00 2001 From: Jackson Bailey Date: Fri, 5 Jul 2024 17:32:12 -0400 Subject: [PATCH] Move Channel Definition fetching to its own class --- .../service/ChannelDefinitionFetcher.java | 18 ++++++ .../terra/service/GetChannelsCommand.java | 9 +-- .../terra/service/ResetCommandsCommand.java | 42 ++++++++++++++ .../service/ChannelDefinitionFetcherTest.java | 55 +++++++++++++++++++ 4 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcher.java create mode 100644 terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ResetCommandsCommand.java create mode 100644 terra/src/test/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcherTest.java diff --git a/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcher.java b/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcher.java new file mode 100644 index 0000000..97b601f --- /dev/null +++ b/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcher.java @@ -0,0 +1,18 @@ +package dev.jacksonbailey.wheel.terra.service; + +import dev.jacksonbailey.wheel.terra.model.ChannelDefinition; +import java.util.Set; +import java.util.stream.Collectors; +import net.dv8tion.jda.api.entities.Guild; +import org.springframework.stereotype.Service; + +@Service +public class ChannelDefinitionFetcher { + + public Set getDefinitionsFromGuild(Guild guild) { + return guild.getChannels().stream() + .map(ChannelDefinition::from) + .collect(Collectors.toSet()); + } + +} diff --git a/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/GetChannelsCommand.java b/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/GetChannelsCommand.java index cdba57b..6711b93 100644 --- a/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/GetChannelsCommand.java +++ b/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/GetChannelsCommand.java @@ -24,8 +24,11 @@ public class GetChannelsCommand extends AbstractCommand { public static String COMMAND_NAME = "get-channels"; - public GetChannelsCommand(JDA jda) { + private final ChannelDefinitionFetcher channelDefinitionFetcher; + + public GetChannelsCommand(JDA jda, ChannelDefinitionFetcher channelDefinitionFetcher) { super(jda); + this.channelDefinitionFetcher = channelDefinitionFetcher; } @Override @@ -42,9 +45,7 @@ public MessageCreateData doCommand(@NotNull SlashCommandInteractionEvent event) var sourceGuild = event.getGuild(); assert sourceGuild != null; // TODO Improve this?? - var channelDefinitions = sourceGuild.getChannels().stream() - .map(ChannelDefinition::from) - .toList(); + var channelDefinitions = channelDefinitionFetcher.getDefinitionsFromGuild(sourceGuild); var builder = new MessageCreateBuilder(); return builder.addContent(channelDefinitions.toString()).build(); diff --git a/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ResetCommandsCommand.java b/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ResetCommandsCommand.java new file mode 100644 index 0000000..d75c82c --- /dev/null +++ b/terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ResetCommandsCommand.java @@ -0,0 +1,42 @@ +package dev.jacksonbailey.wheel.terra.service; + +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import net.dv8tion.jda.api.interactions.commands.build.CommandData; +import net.dv8tion.jda.api.interactions.commands.build.Commands; +import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; +import net.dv8tion.jda.api.utils.messages.MessageCreateData; +import org.jetbrains.annotations.NotNull; + +public class ResetCommandsCommand extends AbstractCommand { + + public ResetCommandsCommand(JDA jda) { + super(jda); + } + + @Override + public CommandData getCommandData() { + return Commands.slash(getCommandName(), "Deletes all registered commands."); + } + + @Override + public MessageCreateData doCommand(@NotNull SlashCommandInteractionEvent event) { + jda.retrieveCommands().queue(commands -> + commands.parallelStream().forEach( + command -> jda.deleteCommandById(command.getIdLong()).queue() + ) + ); + var builder = new MessageCreateBuilder(); + return builder.addContent("Queued for deletion!").build(); + } + + @Override + public String getCommandName() { + return "reset-commands"; + } + + @Override + public boolean isEphemeral() { + return true; + } +} diff --git a/terra/src/test/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcherTest.java b/terra/src/test/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcherTest.java new file mode 100644 index 0000000..005bf2d --- /dev/null +++ b/terra/src/test/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcherTest.java @@ -0,0 +1,55 @@ +package dev.jacksonbailey.wheel.terra.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; + +import dev.jacksonbailey.wheel.terra.model.TextChannelDefinition; +import dev.jacksonbailey.wheel.terra.model.VoiceChannelDefinition; +import java.util.List; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.channel.ChannelType; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; +import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import org.junit.jupiter.api.Test; + +class ChannelDefinitionFetcherTest { + + private ChannelDefinitionFetcher channelDefinitionFetcher = new ChannelDefinitionFetcher(); + + @Test + void test() { + var event = mock(SlashCommandInteractionEvent.class); + var guild = mock(Guild.class); + given(event.getGuild()).willReturn(guild); + var textChannel = mock(TextChannel.class); + var textChannelName = "text channel"; + given(textChannel.getName()).willReturn(textChannelName); + given(textChannel.getType()).willReturn(ChannelType.TEXT); + var voiceChannel = mock(VoiceChannel.class); + var voiceChannelName = "voice channel"; + given(voiceChannel.getName()).willReturn(voiceChannelName); + given(voiceChannel.getType()).willReturn(ChannelType.VOICE); + List channels = List.of(textChannel, voiceChannel); + given(guild.getChannels()).willReturn(channels); + + var definitions = channelDefinitionFetcher.getDefinitionsFromGuild(guild); + + assertAll( + () -> assertThat(definitions).anySatisfy(definition -> assertAll( + () -> assertThat(definition).isInstanceOf(TextChannelDefinition.class), + () -> assertEquals(textChannelName, definition.name()) + )), + () -> assertThat(definitions).anySatisfy(definition -> assertAll( + () -> assertThat(definition).isInstanceOf(VoiceChannelDefinition.class), + () -> assertEquals(voiceChannelName, definition.name()) + )), + () -> assertThat(definitions).size().isEqualTo(2) + ); + } + +}