🔨 Add endpoint
This commit is contained in:
parent
e823075fdc
commit
e5db36d558
@ -1,5 +1,6 @@
|
|||||||
package net.Broken.Api.Controllers;
|
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.Connect;
|
||||||
import net.Broken.Api.Data.Music.Status;
|
import net.Broken.Api.Data.Music.Status;
|
||||||
import net.Broken.Api.Security.Data.JwtPrincipal;
|
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.security.core.Authentication;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/v2/audio")
|
@RequestMapping("/api/v2/audio")
|
||||||
@CrossOrigin(origins = "*", maxAge = 3600)
|
@CrossOrigin(origins = "*", maxAge = 3600)
|
||||||
@ -70,4 +73,11 @@ public class AudioController {
|
|||||||
JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal();
|
JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal();
|
||||||
return audioService.stop(guildId, principal.user().getDiscordId());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
4
src/main/java/net/Broken/Api/Data/Music/Add.java
Normal file
4
src/main/java/net/Broken/Api/Data/Music/Add.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package net.Broken.Api.Data.Music;
|
||||||
|
|
||||||
|
public record Add(String url) {
|
||||||
|
}
|
@ -1,10 +1,7 @@
|
|||||||
package net.Broken.Api.Services;
|
package net.Broken.Api.Services;
|
||||||
|
|
||||||
import net.Broken.Api.Data.Guild.Channel;
|
import net.Broken.Api.Data.Guild.Channel;
|
||||||
import net.Broken.Api.Data.Music.Connect;
|
import net.Broken.Api.Data.Music.*;
|
||||||
import net.Broken.Api.Data.Music.PlayBackInfo;
|
|
||||||
import net.Broken.Api.Data.Music.Status;
|
|
||||||
import net.Broken.Api.Data.Music.TrackInfo;
|
|
||||||
import net.Broken.Audio.GuildAudioBotService;
|
import net.Broken.Audio.GuildAudioBotService;
|
||||||
import net.Broken.Audio.UserAudioTrack;
|
import net.Broken.Audio.UserAudioTrack;
|
||||||
import net.Broken.MainBot;
|
import net.Broken.MainBot;
|
||||||
@ -19,6 +16,8 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class AudioService {
|
public class AudioService {
|
||||||
|
|
||||||
@ -117,4 +116,16 @@ public class AudioService {
|
|||||||
Status status = getGuildAudioStatus(guildId, userId);
|
Status status = getGuildAudioStatus(guildId, userId);
|
||||||
return new ResponseEntity<>(status, HttpStatus.OK);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,10 +21,10 @@ import org.apache.logging.log4j.LogManager;
|
|||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
public class GuildAudioBotService {
|
public class GuildAudioBotService {
|
||||||
|
|
||||||
@ -42,6 +42,8 @@ public class GuildAudioBotService {
|
|||||||
private final Guild guild;
|
private final Guild guild;
|
||||||
private final Logger logger = LogManager.getLogger();
|
private final Logger logger = LogManager.getLogger();
|
||||||
|
|
||||||
|
private final Map<String, Boolean> addStatus = new HashMap<>();
|
||||||
|
|
||||||
private Message lastMessageWithButton;
|
private Message lastMessageWithButton;
|
||||||
|
|
||||||
private GuildAudioBotService(Guild guild) {
|
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();
|
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
|
@Override
|
||||||
public void trackLoaded(AudioTrack track) {
|
public void trackLoaded(AudioTrack track) {
|
||||||
logger.info("[" + guild + "] Auto add " + track.getInfo().title + " to playlist.");
|
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);
|
play(guild, playedChanel, guildAudioManager, userAudioTrack, true);
|
||||||
|
addStatus.put(uuid, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void playlistLoaded(AudioPlaylist playlist) {
|
public void playlistLoaded(AudioPlaylist playlist) {
|
||||||
AudioTrack track = playlist.getTracks().get(0);
|
AudioTrack track = playlist.getTracks().get(0);
|
||||||
logger.info("[" + guild + "] Auto add " + track.getInfo().title + " to playlist.");
|
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);
|
play(guild, playedChanel, guildAudioManager, userAudioTrack, true);
|
||||||
|
addStatus.put(uuid, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void noMatches() {
|
public void noMatches() {
|
||||||
logger.warn("[" + guild + "] Track not found: " + trackUrl);
|
logger.warn("[" + guild + "] Track not found: " + trackUrl);
|
||||||
|
addStatus.put(uuid, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void loadFailed(FriendlyException exception) {
|
public void loadFailed(FriendlyException exception) {
|
||||||
logger.error("[" + guild + "] Cant load media!");
|
logger.error("[" + guild + "] Cant load media!");
|
||||||
logger.error(exception.getMessage());
|
logger.error(exception.getMessage());
|
||||||
|
addStatus.put(uuid, false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
future.get();
|
||||||
|
return addStatus.remove(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user