Compare commits
2 Commits
1b3bd15aaf
...
c0146fa0c3
Author | SHA1 | Date | |
---|---|---|---|
c0146fa0c3 | |||
699346f1ce |
@ -1,8 +1,10 @@
|
|||||||
package net.Broken.Api.Controllers;
|
package net.Broken.Api.Controllers;
|
||||||
|
|
||||||
|
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;
|
||||||
import net.Broken.Api.Services.AudioService;
|
import net.Broken.Api.Services.AudioService;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
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.*;
|
||||||
@ -25,4 +27,11 @@ public class AudioController {
|
|||||||
JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal();
|
JwtPrincipal principal = (JwtPrincipal) authentication.getPrincipal();
|
||||||
return audioService.getGuildAudioStatus(guildId, principal.user().getDiscordId());
|
return audioService.getGuildAudioStatus(guildId, principal.user().getDiscordId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@PostMapping("/{guildId}/connect")
|
||||||
|
@PreAuthorize("isInGuild(#guildId) && canInteractWithVoiceChannel(#guildId, #body)")
|
||||||
|
public ResponseEntity<String> connect(@PathVariable String guildId, @RequestBody Connect body){
|
||||||
|
return audioService.connect(guildId, body);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
4
src/main/java/net/Broken/Api/Data/Music/Connect.java
Normal file
4
src/main/java/net/Broken/Api/Data/Music/Connect.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package net.Broken.Api.Data.Music;
|
||||||
|
|
||||||
|
public record Connect(String channelId) {
|
||||||
|
}
|
@ -1,11 +1,13 @@
|
|||||||
package net.Broken.Api.Security.Expression;
|
package net.Broken.Api.Security.Expression;
|
||||||
|
|
||||||
|
import net.Broken.Api.Data.Music.Connect;
|
||||||
import net.Broken.Api.Security.Data.JwtPrincipal;
|
import net.Broken.Api.Security.Data.JwtPrincipal;
|
||||||
import net.Broken.MainBot;
|
import net.Broken.MainBot;
|
||||||
import net.Broken.Tools.CacheTools;
|
import net.Broken.Tools.CacheTools;
|
||||||
import net.dv8tion.jda.api.Permission;
|
import net.dv8tion.jda.api.Permission;
|
||||||
import net.dv8tion.jda.api.entities.Guild;
|
import net.dv8tion.jda.api.entities.Guild;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
|
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||||
import okhttp3.Cache;
|
import okhttp3.Cache;
|
||||||
import org.springframework.security.access.expression.SecurityExpressionRoot;
|
import org.springframework.security.access.expression.SecurityExpressionRoot;
|
||||||
import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations;
|
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
|
@Override
|
||||||
public void setFilterObject(Object filterObject) {
|
public void setFilterObject(Object filterObject) {
|
||||||
this.filterObject = filterObject;
|
this.filterObject = filterObject;
|
||||||
|
@ -1,6 +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.PlayBackInfo;
|
import net.Broken.Api.Data.Music.PlayBackInfo;
|
||||||
import net.Broken.Api.Data.Music.Status;
|
import net.Broken.Api.Data.Music.Status;
|
||||||
import net.Broken.Api.Data.Music.TrackInfo;
|
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.GuildVoiceState;
|
||||||
import net.dv8tion.jda.api.entities.Member;
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -28,15 +31,18 @@ public class AudioService {
|
|||||||
// -> He can view the voice channel
|
// -> He can view the voice channel
|
||||||
// -> OR He can *not* view the voice channel, but he is connected to this 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)
|
boolean canView = member.hasPermission(channel, Permission.VIEW_CHANNEL)
|
||||||
|| (member.getVoiceState() != null && member.getVoiceState().getChannel() == channel);
|
|| (member.getVoiceState() != null
|
||||||
|
&& member.getVoiceState().getChannel() == channel);
|
||||||
if (canView) {
|
if (canView) {
|
||||||
// The user can interact with the audio if:
|
// The user can interact with the audio if:
|
||||||
// -> He can connect to this voice channel
|
// -> He can connect to this voice channel
|
||||||
|
// -> OR he is connected to this voice channel
|
||||||
// -> AND He can speak in 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)
|
||||||
boolean canInteract = member.hasPermission(channel, Permission.VOICE_CONNECT, Permission.VOICE_SPEAK)
|
|| member.getVoiceState() != null
|
||||||
&& member.getVoiceState() != null
|
&& member.getVoiceState().getChannel() == channel)
|
||||||
&& member.getVoiceState().getChannel() == channel;
|
&& member.hasPermission(channel, Permission.VOICE_SPEAK);
|
||||||
|
|
||||||
AudioM audioM = AudioM.getInstance(guild);
|
AudioM audioM = AudioM.getInstance(guild);
|
||||||
boolean stopped = audioM.getGuildAudioPlayer().player.getPlayingTrack() == null;
|
boolean stopped = audioM.getGuildAudioPlayer().player.getPlayingTrack() == null;
|
||||||
PlayBackInfo playBackInfo;
|
PlayBackInfo playBackInfo;
|
||||||
@ -62,4 +68,14 @@ public class AudioService {
|
|||||||
}
|
}
|
||||||
return new Status(false, null, null, null, null);
|
return new Status(false, null, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ResponseEntity<String> 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user