-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Channel Definition fetching to its own class
- Loading branch information
1 parent
3c34727
commit 08cd640
Showing
4 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
terra/src/main/java/dev/jacksonbailey/wheel/terra/service/ResetCommandsCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
terra/src/test/java/dev/jacksonbailey/wheel/terra/service/ChannelDefinitionFetcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
|
||
} |