Skip to content

Commit

Permalink
Move Channel Definition fetching to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonBailey committed Jul 5, 2024
1 parent 3c34727 commit 08cd640
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<ChannelDefinition> getDefinitionsFromGuild(Guild guild) {
return guild.getChannels().stream()
.map(ChannelDefinition::from)
.collect(Collectors.toSet());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<GuildChannel> 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)
);
}

}

0 comments on commit 08cd640

Please sign in to comment.