From c0146fa0c3af26b9b6e21e8ab504a4df5582591a Mon Sep 17 00:00:00 2001 From: SebClem Date: Wed, 22 Jun 2022 19:52:51 +0200 Subject: [PATCH] :hammer: Add connect api command --- .../Broken/Api/Controllers/AudioController.java | 9 +++++++++ .../java/net/Broken/Api/Data/Music/Connect.java | 4 ++++ .../CustomMethodSecurityExpressionRoot.java | 17 +++++++++++++++++ .../net/Broken/Api/Services/AudioService.java | 17 +++++++++++++++-- 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/main/java/net/Broken/Api/Data/Music/Connect.java diff --git a/src/main/java/net/Broken/Api/Controllers/AudioController.java b/src/main/java/net/Broken/Api/Controllers/AudioController.java index 53f6790..daea520 100644 --- a/src/main/java/net/Broken/Api/Controllers/AudioController.java +++ b/src/main/java/net/Broken/Api/Controllers/AudioController.java @@ -1,8 +1,10 @@ package net.Broken.Api.Controllers; +import net.Broken.Api.Data.Music.Connect; import net.Broken.Api.Data.Music.Status; import net.Broken.Api.Security.Data.JwtPrincipal; import net.Broken.Api.Services.AudioService; +import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; @@ -25,4 +27,11 @@ public class AudioController { JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal(); return audioService.getGuildAudioStatus(guildId, principal.user().getDiscordId()); } + + + @PostMapping("/{guildId}/connect") + @PreAuthorize("isInGuild(#guildId) && canInteractWithVoiceChannel(#guildId, #body)") + public ResponseEntity connect(@PathVariable String guildId, @RequestBody Connect body){ + return audioService.connect(guildId, body); + } } diff --git a/src/main/java/net/Broken/Api/Data/Music/Connect.java b/src/main/java/net/Broken/Api/Data/Music/Connect.java new file mode 100644 index 0000000..112d307 --- /dev/null +++ b/src/main/java/net/Broken/Api/Data/Music/Connect.java @@ -0,0 +1,4 @@ +package net.Broken.Api.Data.Music; + +public record Connect(String channelId) { +} diff --git a/src/main/java/net/Broken/Api/Security/Expression/CustomMethodSecurityExpressionRoot.java b/src/main/java/net/Broken/Api/Security/Expression/CustomMethodSecurityExpressionRoot.java index d54e148..894a4fb 100644 --- a/src/main/java/net/Broken/Api/Security/Expression/CustomMethodSecurityExpressionRoot.java +++ b/src/main/java/net/Broken/Api/Security/Expression/CustomMethodSecurityExpressionRoot.java @@ -1,11 +1,13 @@ package net.Broken.Api.Security.Expression; +import net.Broken.Api.Data.Music.Connect; import net.Broken.Api.Security.Data.JwtPrincipal; import net.Broken.MainBot; import net.Broken.Tools.CacheTools; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.VoiceChannel; import okhttp3.Cache; import org.springframework.security.access.expression.SecurityExpressionRoot; import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations; @@ -41,6 +43,21 @@ public class CustomMethodSecurityExpressionRoot ); } + public boolean canInteractWithVoiceChannel(String guildId, Connect connectPayload){ + JwtPrincipal jwtPrincipal = (JwtPrincipal) authentication.getPrincipal(); + Guild guild = MainBot.jda.getGuildById(guildId); + Member member = guild.getMemberById(jwtPrincipal.user().getDiscordId()); + VoiceChannel channel = guild.getVoiceChannelById(connectPayload.channelId()); + if( channel == null){ + return false; + } + + return (member.hasPermission(channel, Permission.VOICE_CONNECT) + || member.getVoiceState() != null + && member.getVoiceState().getChannel() == channel) + && member.hasPermission(channel, Permission.VOICE_SPEAK); + } + @Override public void setFilterObject(Object filterObject) { this.filterObject = filterObject; diff --git a/src/main/java/net/Broken/Api/Services/AudioService.java b/src/main/java/net/Broken/Api/Services/AudioService.java index 310db68..540a302 100644 --- a/src/main/java/net/Broken/Api/Services/AudioService.java +++ b/src/main/java/net/Broken/Api/Services/AudioService.java @@ -1,6 +1,7 @@ package net.Broken.Api.Services; import net.Broken.Api.Data.Guild.Channel; +import net.Broken.Api.Data.Music.Connect; import net.Broken.Api.Data.Music.PlayBackInfo; import net.Broken.Api.Data.Music.Status; import net.Broken.Api.Data.Music.TrackInfo; @@ -12,6 +13,8 @@ import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.GuildVoiceState; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.VoiceChannel; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @Service @@ -37,9 +40,9 @@ public class AudioService { // -> AND He can speak in this voice channel boolean canInteract = (member.hasPermission(channel, Permission.VOICE_CONNECT) || member.getVoiceState() != null - || member.getVoiceState().getChannel() == channel) + && member.getVoiceState().getChannel() == channel) && member.hasPermission(channel, Permission.VOICE_SPEAK); - + AudioM audioM = AudioM.getInstance(guild); boolean stopped = audioM.getGuildAudioPlayer().player.getPlayingTrack() == null; PlayBackInfo playBackInfo; @@ -65,4 +68,14 @@ public class AudioService { } return new Status(false, null, null, null, null); } + + public ResponseEntity connect(String guildId, Connect body){ + Guild guild = MainBot.jda.getGuildById(guildId); + AudioM audioM = AudioM.getInstance(guild); + VoiceChannel voiceChannel = guild.getVoiceChannelById(body.channelId()); + audioM.getGuildAudioPlayer(); + guild.getAudioManager().openAudioConnection(voiceChannel); + audioM.setPlayedChanel(voiceChannel); + return new ResponseEntity<>("OK", HttpStatus.OK); + } }