From 3f5225bef1d953900e012e893a761902f91fd80c Mon Sep 17 00:00:00 2001 From: SebClem Date: Fri, 10 Jun 2022 16:55:31 +0200 Subject: [PATCH] :hammer: Add textchannel and voicechannel endpoint --- .../Api/Controllers/GuildController.java | 20 ++++++++++++++---- .../Api/Controllers/HelloController.java | 1 + .../java/net/Broken/Api/Data/Channel.java | 4 ++++ .../net/Broken/Api/Services/GuildService.java | 21 +++++++++++++++++++ src/main/resources/log4j2.xml | 2 +- 5 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 src/main/java/net/Broken/Api/Data/Channel.java diff --git a/src/main/java/net/Broken/Api/Controllers/GuildController.java b/src/main/java/net/Broken/Api/Controllers/GuildController.java index 7c4efdd..8e9e20b 100644 --- a/src/main/java/net/Broken/Api/Controllers/GuildController.java +++ b/src/main/java/net/Broken/Api/Controllers/GuildController.java @@ -2,15 +2,14 @@ package net.Broken.Api.Controllers; import net.Broken.Api.Data.Guild; import net.Broken.Api.Data.InviteLink; +import net.Broken.Api.Data.Channel; import net.Broken.Api.Security.Data.JwtPrincipal; import net.Broken.Api.Services.GuildService; import net.Broken.MainBot; import net.dv8tion.jda.api.Permission; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -36,4 +35,17 @@ public class GuildController { String link = MainBot.jda.getInviteUrl(Permission.ADMINISTRATOR); return new InviteLink(link); } + + + @GetMapping("/{guildId}/voiceChannels") + @PreAuthorize("isInGuild(#guildId)") + public List getVoiceChannels(@PathVariable String guildId){ + return guildService.getVoiceChannel(String.valueOf(guildId)); + } + + @GetMapping("/{guildId}/textChannels") + @PreAuthorize("isInGuild(#guildId)") + public List getTextChannels(@PathVariable String guildId){ + return guildService.getTextChannel(String.valueOf(guildId)); + } } diff --git a/src/main/java/net/Broken/Api/Controllers/HelloController.java b/src/main/java/net/Broken/Api/Controllers/HelloController.java index c7a42d6..bb3ecba 100644 --- a/src/main/java/net/Broken/Api/Controllers/HelloController.java +++ b/src/main/java/net/Broken/Api/Controllers/HelloController.java @@ -5,6 +5,7 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityRequirements; import net.Broken.Api.Security.Data.JwtPrincipal; import net.Broken.DB.Entity.UserEntity; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; diff --git a/src/main/java/net/Broken/Api/Data/Channel.java b/src/main/java/net/Broken/Api/Data/Channel.java new file mode 100644 index 0000000..69ac82d --- /dev/null +++ b/src/main/java/net/Broken/Api/Data/Channel.java @@ -0,0 +1,4 @@ +package net.Broken.Api.Data; + +public record Channel(String id, String name) { +} diff --git a/src/main/java/net/Broken/Api/Services/GuildService.java b/src/main/java/net/Broken/Api/Services/GuildService.java index d5d3582..d263044 100644 --- a/src/main/java/net/Broken/Api/Services/GuildService.java +++ b/src/main/java/net/Broken/Api/Services/GuildService.java @@ -1,7 +1,9 @@ package net.Broken.Api.Services; import net.Broken.Api.Data.Guild; +import net.Broken.Api.Data.Channel; import net.Broken.DB.Entity.UserEntity; +import net.Broken.MainBot; import net.Broken.Tools.CacheTools; import net.dv8tion.jda.api.entities.User; import org.springframework.stereotype.Service; @@ -22,4 +24,23 @@ public class GuildService { return guildList; } + public List getVoiceChannel(String guildId){ + net.dv8tion.jda.api.entities.Guild guild = MainBot.jda.getGuildById(guildId); + + List voiceChannels = new ArrayList<>(); + for(net.dv8tion.jda.api.entities.VoiceChannel voiceChannel : guild.getVoiceChannels()){ + voiceChannels.add(new Channel(voiceChannel.getId(), voiceChannel.getName())); + } + return voiceChannels; + } + + public List getTextChannel(String guildId){ + net.dv8tion.jda.api.entities.Guild guild = MainBot.jda.getGuildById(guildId); + List voiceChannels = new ArrayList<>(); + for(net.dv8tion.jda.api.entities.TextChannel textChannel : guild.getTextChannels()){ + voiceChannels.add(new Channel(textChannel.getId(), textChannel.getName())); + } + return voiceChannels; + } + } diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index d683f75..228b4d9 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ - + [%d{HH:mm:ss.SSS}]%highlight{[%-5level]}{FATAL=red blink, ERROR=red, WARN=bright yellow , INFO=blue, DEBUG=bright black, TRACE=cyan}[%-30.30c{1.}]: %highlight{%msg%n}{FATAL=red blink, ERROR=red, WARN=bright yellow , INFO=blue, DEBUG=bright black, TRACE=cyan}