🔨 Add music status api enpoint
This commit is contained in:
parent
9f1a3f737b
commit
054b8beef1
@ -0,0 +1,28 @@
|
|||||||
|
package net.Broken.Api.Controllers;
|
||||||
|
|
||||||
|
import net.Broken.Api.Data.Music.Status;
|
||||||
|
import net.Broken.Api.Security.Data.JwtPrincipal;
|
||||||
|
import net.Broken.Api.Services.AudioService;
|
||||||
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
import org.springframework.security.core.Authentication;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/v2/music")
|
||||||
|
@CrossOrigin(origins = "*", maxAge = 3600)
|
||||||
|
public class MusicController {
|
||||||
|
|
||||||
|
public final AudioService audioService;
|
||||||
|
|
||||||
|
public MusicController(AudioService audioService) {
|
||||||
|
this.audioService = audioService;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@GetMapping("/{guildId}/status")
|
||||||
|
@PreAuthorize("isInGuild(#guildId)")
|
||||||
|
public Status getMusicStatus(@PathVariable String guildId, Authentication authentication){
|
||||||
|
JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal();
|
||||||
|
return audioService.getGuildAudioStatus(guildId, principal.user().getDiscordId());
|
||||||
|
}
|
||||||
|
}
|
12
src/main/java/net/Broken/Api/Data/Music/PlayBackInfo.java
Normal file
12
src/main/java/net/Broken/Api/Data/Music/PlayBackInfo.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package net.Broken.Api.Data.Music;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public record PlayBackInfo(
|
||||||
|
Boolean paused,
|
||||||
|
Boolean stopped,
|
||||||
|
Long progress,
|
||||||
|
TrackInfo trackInfo
|
||||||
|
) {
|
||||||
|
}
|
14
src/main/java/net/Broken/Api/Data/Music/Status.java
Normal file
14
src/main/java/net/Broken/Api/Data/Music/Status.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package net.Broken.Api.Data.Music;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||||
|
import net.Broken.Api.Data.Guild.Channel;
|
||||||
|
|
||||||
|
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||||
|
public record Status(
|
||||||
|
Boolean connected,
|
||||||
|
Channel channel,
|
||||||
|
Boolean canView,
|
||||||
|
Boolean canInteract,
|
||||||
|
PlayBackInfo playBackInfo
|
||||||
|
) {
|
||||||
|
}
|
17
src/main/java/net/Broken/Api/Data/Music/TrackInfo.java
Normal file
17
src/main/java/net/Broken/Api/Data/Music/TrackInfo.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package net.Broken.Api.Data.Music;
|
||||||
|
|
||||||
|
import com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
|
||||||
|
import net.Broken.Api.Data.UserInfo;
|
||||||
|
import net.Broken.audio.UserAudioTrack;
|
||||||
|
|
||||||
|
public record TrackInfo(UserInfo submitter, AudioTrackInfo detail) {
|
||||||
|
public TrackInfo(UserInfo submitter, AudioTrackInfo detail) {
|
||||||
|
this.submitter = submitter;
|
||||||
|
this.detail = detail;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TrackInfo(UserAudioTrack userAudioTrack) {
|
||||||
|
this(new UserInfo(userAudioTrack.getSubmittedUser().getId(), userAudioTrack.getSubmittedUser().getName(), userAudioTrack.getSubmittedUser().getAvatarUrl()),
|
||||||
|
userAudioTrack.getAudioTrack().getInfo());
|
||||||
|
}
|
||||||
|
}
|
8
src/main/java/net/Broken/Api/Data/UserInfo.java
Normal file
8
src/main/java/net/Broken/Api/Data/UserInfo.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package net.Broken.Api.Data;
|
||||||
|
|
||||||
|
public record UserInfo(
|
||||||
|
String id,
|
||||||
|
String username,
|
||||||
|
String avatar
|
||||||
|
) {
|
||||||
|
}
|
65
src/main/java/net/Broken/Api/Services/AudioService.java
Normal file
65
src/main/java/net/Broken/Api/Services/AudioService.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package net.Broken.Api.Services;
|
||||||
|
|
||||||
|
import net.Broken.Api.Data.Guild.Channel;
|
||||||
|
import net.Broken.Api.Data.Music.PlayBackInfo;
|
||||||
|
import net.Broken.Api.Data.Music.Status;
|
||||||
|
import net.Broken.Api.Data.Music.TrackInfo;
|
||||||
|
import net.Broken.MainBot;
|
||||||
|
import net.Broken.audio.AudioM;
|
||||||
|
import net.Broken.audio.UserAudioTrack;
|
||||||
|
import net.dv8tion.jda.api.Permission;
|
||||||
|
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.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class AudioService {
|
||||||
|
public Status getGuildAudioStatus(String guildId, String userId) {
|
||||||
|
Guild guild = MainBot.jda.getGuildById(guildId);
|
||||||
|
Member member = guild.getMemberById(userId);
|
||||||
|
GuildVoiceState voiceState = guild.getSelfMember().getVoiceState();
|
||||||
|
|
||||||
|
if (voiceState != null) {
|
||||||
|
VoiceChannel channel = voiceState.getChannel();
|
||||||
|
if (channel != null) {
|
||||||
|
// The user can view the audio status if:
|
||||||
|
// -> He can view the voice channel
|
||||||
|
// -> OR He can *not* view the voice channel, but he is connected to this voice channel
|
||||||
|
boolean canView = member.hasPermission(channel, Permission.VIEW_CHANNEL)
|
||||||
|
|| (member.getVoiceState() != null && member.getVoiceState().getChannel() == channel);
|
||||||
|
if (canView) {
|
||||||
|
// The user can interact with the audio if:
|
||||||
|
// -> He can connect to this voice channel
|
||||||
|
// -> AND He can speak in this voice channel
|
||||||
|
// -> AND He is connected to this voice channel
|
||||||
|
boolean canInteract = member.hasPermission(channel, Permission.VOICE_CONNECT, Permission.VOICE_SPEAK)
|
||||||
|
&& member.getVoiceState() != null
|
||||||
|
&& member.getVoiceState().getChannel() == channel;
|
||||||
|
AudioM audioM = AudioM.getInstance(guild);
|
||||||
|
boolean stopped = audioM.getGuildAudioPlayer().player.getPlayingTrack() == null;
|
||||||
|
PlayBackInfo playBackInfo;
|
||||||
|
if (!stopped) {
|
||||||
|
boolean paused = audioM.getGuildAudioPlayer().player.isPaused();
|
||||||
|
long position = audioM.getGuildAudioPlayer().player.getPlayingTrack().getPosition();
|
||||||
|
UserAudioTrack userAudioTrack = audioM.getGuildAudioPlayer().scheduler.getCurrentPlayingTrack();
|
||||||
|
|
||||||
|
playBackInfo = new PlayBackInfo(paused, false, position, new TrackInfo(userAudioTrack));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
playBackInfo = new PlayBackInfo(false, true, null, null);
|
||||||
|
}
|
||||||
|
Channel channelApi = new Channel(channel.getId(), channel.getName());
|
||||||
|
return new Status(true, channelApi, true, canInteract, playBackInfo);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return new Status(true, null, false, false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new Status(false, null, null, null, null);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user