From e5db36d558e589d58788999a114a3fee7f039359 Mon Sep 17 00:00:00 2001 From: SebClem Date: Thu, 23 Jun 2022 18:27:56 +0200 Subject: [PATCH] :hammer: Add endpoint --- .../Api/Controllers/AudioController.java | 10 +++++++ .../java/net/Broken/Api/Data/Music/Add.java | 4 +++ .../net/Broken/Api/Services/AudioService.java | 27 +++++++++++++------ .../Broken/Audio/GuildAudioBotService.java | 24 ++++++++++++----- 4 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 src/main/java/net/Broken/Api/Data/Music/Add.java diff --git a/src/main/java/net/Broken/Api/Controllers/AudioController.java b/src/main/java/net/Broken/Api/Controllers/AudioController.java index 0cbb69f..ee7bce0 100644 --- a/src/main/java/net/Broken/Api/Controllers/AudioController.java +++ b/src/main/java/net/Broken/Api/Controllers/AudioController.java @@ -1,5 +1,6 @@ package net.Broken.Api.Controllers; +import net.Broken.Api.Data.Music.Add; import net.Broken.Api.Data.Music.Connect; import net.Broken.Api.Data.Music.Status; import net.Broken.Api.Security.Data.JwtPrincipal; @@ -9,6 +10,8 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; +import java.util.concurrent.ExecutionException; + @RestController @RequestMapping("/api/v2/audio") @CrossOrigin(origins = "*", maxAge = 3600) @@ -70,4 +73,11 @@ public class AudioController { JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal(); return audioService.stop(guildId, principal.user().getDiscordId()); } + + @PostMapping("/{guildId}/add") + @PreAuthorize("isInGuild(#guildId) && canInteractWithVoiceChannel(#guildId)") + public ResponseEntity add(@PathVariable String guildId, @RequestBody Add body, Authentication authentication) throws ExecutionException, InterruptedException { + JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal(); + return audioService.add(guildId, principal.user().getDiscordId(), body); + } } diff --git a/src/main/java/net/Broken/Api/Data/Music/Add.java b/src/main/java/net/Broken/Api/Data/Music/Add.java new file mode 100644 index 0000000..c48c56d --- /dev/null +++ b/src/main/java/net/Broken/Api/Data/Music/Add.java @@ -0,0 +1,4 @@ +package net.Broken.Api.Data.Music; + +public record Add(String url) { +} diff --git a/src/main/java/net/Broken/Api/Services/AudioService.java b/src/main/java/net/Broken/Api/Services/AudioService.java index cfa4842..e3a5708 100644 --- a/src/main/java/net/Broken/Api/Services/AudioService.java +++ b/src/main/java/net/Broken/Api/Services/AudioService.java @@ -1,10 +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; +import net.Broken.Api.Data.Music.*; import net.Broken.Audio.GuildAudioBotService; import net.Broken.Audio.UserAudioTrack; import net.Broken.MainBot; @@ -19,6 +16,8 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import java.util.concurrent.ExecutionException; + @Service public class AudioService { @@ -90,31 +89,43 @@ public class AudioService { } - public ResponseEntity pause(String guildId, String userId){ + public ResponseEntity pause(String guildId, String userId) { Guild guild = MainBot.jda.getGuildById(guildId); GuildAudioBotService.getInstance(guild).pause(); Status status = getGuildAudioStatus(guildId, userId); return new ResponseEntity<>(status, HttpStatus.OK); } - public ResponseEntity resume(String guildId, String userId){ + public ResponseEntity resume(String guildId, String userId) { Guild guild = MainBot.jda.getGuildById(guildId); GuildAudioBotService.getInstance(guild).resume(); Status status = getGuildAudioStatus(guildId, userId); return new ResponseEntity<>(status, HttpStatus.OK); } - public ResponseEntity skip(String guildId, String userId){ + public ResponseEntity skip(String guildId, String userId) { Guild guild = MainBot.jda.getGuildById(guildId); GuildAudioBotService.getInstance(guild).skipTrack(); Status status = getGuildAudioStatus(guildId, userId); return new ResponseEntity<>(status, HttpStatus.OK); } - public ResponseEntity stop(String guildId, String userId){ + public ResponseEntity stop(String guildId, String userId) { Guild guild = MainBot.jda.getGuildById(guildId); GuildAudioBotService.getInstance(guild).stop(); Status status = getGuildAudioStatus(guildId, userId); return new ResponseEntity<>(status, HttpStatus.OK); } + + public ResponseEntity add(String guildId, String userId, Add body) throws ExecutionException, InterruptedException { + Guild guild = MainBot.jda.getGuildById(guildId); + boolean success = GuildAudioBotService.getInstance(guild).loadAndPlaySync(body.url(), userId); + if (success) { + Status status = getGuildAudioStatus(guildId, userId); + return new ResponseEntity<>(status, HttpStatus.OK); + } else { + return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST); + } + + } } diff --git a/src/main/java/net/Broken/Audio/GuildAudioBotService.java b/src/main/java/net/Broken/Audio/GuildAudioBotService.java index 759a1a8..2c4bde2 100644 --- a/src/main/java/net/Broken/Audio/GuildAudioBotService.java +++ b/src/main/java/net/Broken/Audio/GuildAudioBotService.java @@ -21,10 +21,10 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.awt.*; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; +import java.util.*; import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; public class GuildAudioBotService { @@ -42,6 +42,8 @@ public class GuildAudioBotService { private final Guild guild; private final Logger logger = LogManager.getLogger(); + private final Map addStatus = new HashMap<>(); + private Message lastMessageWithButton; private GuildAudioBotService(Guild guild) { @@ -112,35 +114,43 @@ public class GuildAudioBotService { }); } - public void loadAndPlayAuto(String trackUrl) { + public boolean loadAndPlaySync(String trackUrl, String userId) throws ExecutionException, InterruptedException { + Member member = guild.getMemberById(userId); VoiceChannel playedChanel = guild.getAudioManager().getConnectedChannel(); - audioPlayerManager.loadItemOrdered(guildAudioManager, trackUrl, new AudioLoadResultHandler() { + final String uuid = UUID.randomUUID().toString(); + Future future = audioPlayerManager.loadItemOrdered(guildAudioManager, trackUrl, new AudioLoadResultHandler() { @Override public void trackLoaded(AudioTrack track) { logger.info("[" + guild + "] Auto add " + track.getInfo().title + " to playlist."); - UserAudioTrack userAudioTrack = new UserAudioTrack(MainBot.jda.getSelfUser(), track); + UserAudioTrack userAudioTrack = new UserAudioTrack(member.getUser(), track); play(guild, playedChanel, guildAudioManager, userAudioTrack, true); + addStatus.put(uuid, true); } @Override public void playlistLoaded(AudioPlaylist playlist) { AudioTrack track = playlist.getTracks().get(0); logger.info("[" + guild + "] Auto add " + track.getInfo().title + " to playlist."); - UserAudioTrack userAudioTrack = new UserAudioTrack(MainBot.jda.getSelfUser(), track); + UserAudioTrack userAudioTrack = new UserAudioTrack(member.getUser(), track); play(guild, playedChanel, guildAudioManager, userAudioTrack, true); + addStatus.put(uuid, true); } @Override public void noMatches() { logger.warn("[" + guild + "] Track not found: " + trackUrl); + addStatus.put(uuid, false); } @Override public void loadFailed(FriendlyException exception) { logger.error("[" + guild + "] Cant load media!"); logger.error(exception.getMessage()); + addStatus.put(uuid, false); } }); + future.get(); + return addStatus.remove(uuid); }