🔨 Add endpoint

This commit is contained in:
SebClem 2022-06-23 18:27:56 +02:00
parent e823075fdc
commit e5db36d558
Signed by: sebclem
GPG Key ID: 5A4308F6A359EA50
4 changed files with 50 additions and 15 deletions

View File

@ -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<Status> 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);
}
}

View File

@ -0,0 +1,4 @@
package net.Broken.Api.Data.Music;
public record Add(String url) {
}

View File

@ -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<Status> pause(String guildId, String userId){
public ResponseEntity<Status> 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<Status> resume(String guildId, String userId){
public ResponseEntity<Status> 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<Status> skip(String guildId, String userId){
public ResponseEntity<Status> 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<Status> stop(String guildId, String userId){
public ResponseEntity<Status> 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<Status> 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);
}
}
}

View File

@ -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<String, Boolean> 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<Void> 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);
}