Show only channel where the user can conect in music web page

This commit is contained in:
SebClem 2021-03-06 16:13:15 +01:00
parent 1dd98ae419
commit 25374821e1
3 changed files with 48 additions and 58 deletions

View File

@ -29,9 +29,6 @@ public class GuildPreferenceEntity {
private boolean dailyMadame;
@ElementCollection
private List<String> visibleVoiceChanel;
private boolean autoVoice;
private String autoVoiceChannelID;
private String autoVoiceChannelTitle;
@ -45,7 +42,6 @@ public class GuildPreferenceEntity {
boolean defaultRole,
String defaultRoleId,
boolean dailyMadame,
ArrayList<String> visibleVoiceChanel,
boolean autoVoice,
String autoVoiceChannelID,
String autoVoiceChannelTitle) {
@ -57,7 +53,6 @@ public class GuildPreferenceEntity {
this.defaultRole = defaultRole;
this.defaultRoleId = defaultRoleId;
this.dailyMadame = dailyMadame;
this.visibleVoiceChanel = visibleVoiceChanel;
this.autoVoice = autoVoice;
this.autoVoiceChannelID = autoVoiceChannelID;
this.autoVoiceChannelTitle = autoVoiceChannelTitle;
@ -68,12 +63,8 @@ public class GuildPreferenceEntity {
public static GuildPreferenceEntity getDefault(Guild guild) {
ArrayList<String> voice = new ArrayList<>();
for (VoiceChannel voiceChannel : guild.getVoiceChannels()) {
voice.add(voiceChannel.getId());
}
return new GuildPreferenceEntity(guild.getId(), false, false, "Welcome to this awesome server @name! ", " ", false, " ", true, voice, false, " ", " ");
return new GuildPreferenceEntity(guild.getId(), false, false, "Welcome to this awesome server @name! ", " ", false, " ", true, false, " ", " ");
}
public Integer getId() {
@ -148,14 +139,6 @@ public class GuildPreferenceEntity {
this.dailyMadame = dailyMadame;
}
public List<String> getVisibleVoiceChanel() {
return visibleVoiceChanel;
}
public void setVisibleVoiceChanel(List<String> visibleVoiceChanel) {
this.visibleVoiceChanel = visibleVoiceChanel;
}
public boolean isAutoVoice() {
return autoVoice;
}

View File

@ -7,6 +7,7 @@ import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot;
import net.Broken.RestApi.Data.*;
import net.Broken.Tools.CacheTools;
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
import net.Broken.Tools.UserManager.Stats.UserStatsUtils;
import net.Broken.Tools.UserManager.UserUtils;
@ -14,8 +15,8 @@ import net.Broken.audio.AudioM;
import net.Broken.audio.GetVoiceChanels;
import net.Broken.audio.Youtube.SearchResult;
import net.Broken.audio.Youtube.YoutubeSearchRework;
import net.Broken.audio.Youtube.YoutubeTools;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.VoiceChannel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -36,10 +37,9 @@ import java.util.List;
@RestController
@RequestMapping("/api/music/")
public class MusicWebAPIController {
Logger logger = LogManager.getLogger();
private final
UserRepository userRepository;
Logger logger = LogManager.getLogger();
UserUtils userUtils = UserUtils.getInstance();
@Autowired
@ -173,19 +173,39 @@ public class MusicWebAPIController {
}
@RequestMapping(value = "/getChanel", method = RequestMethod.GET)
public ResponseEntity<List<ChanelData>> getChanel(@RequestParam(value = "guild") String guildId) { //TODO security issue ???!!!
Guild guild = MainBot.jda.getGuildById(guildId);
if (guild == null) {
logger.warn("Request whit no guild!");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
public ResponseEntity<List<ChanelData>> getChanel(@CookieValue(name = "token", defaultValue = "") String token, @RequestParam(value = "guild") String guildId) {
if (token != null && !token.isEmpty()) {
try {
UserEntity user = userUtils.getUserWithApiToken(userRepository, token);
Guild guild = MainBot.jda.getGuildById(guildId);
if (guild == null) {
logger.warn("Request whit no guild!");
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
Member member = guild.getMember(CacheTools.getJdaUser(user));
if (member == null) {
member = guild.retrieveMember(CacheTools.getJdaUser(user)).complete();
if (member == null) {
logger.warn("Can't find member " + user.getName() + " for guild " + guild.getName() + ", User not in guild ?");
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}
logger.trace("getPlaylist for " + guild.getName());
List<ChanelData> temp = new ArrayList<>();
for (VoiceChannel aChanel : GetVoiceChanels.find(guild, member)) {
temp.add(new ChanelData(aChanel.getName(), aChanel.getId(), aChanel.getPosition()));
}
return new ResponseEntity<>(temp, HttpStatus.OK);
} catch (UnknownTokenException e) {
logger.warn("Get Chanel without token!");
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
} else {
logger.trace("getPlaylist for " + guild.getName());
logger.warn("Get Chanel without token!");
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
List<ChanelData> temp = new ArrayList<>();
for (VoiceChannel aChanel : GetVoiceChanels.find(guild)) {
temp.add(new ChanelData(aChanel.getName(), aChanel.getId(), aChanel.getPosition()));
}
return new ResponseEntity<>(temp, HttpStatus.OK);
}
@RequestMapping(value = "/search", method = RequestMethod.GET)
@ -211,7 +231,6 @@ public class MusicWebAPIController {
} else {
logger.warn("Search without token!");
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
}
}

View File

@ -2,8 +2,10 @@ package net.Broken.audio;
import net.Broken.DB.Entity.GuildPreferenceEntity;
import net.Broken.Tools.SettingsUtils;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.GuildChannel;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.VoiceChannel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -16,38 +18,24 @@ import java.util.List;
* Used to find general voice channels
*/
public class GetVoiceChanels {
private static Logger logger = LogManager.getLogger();
private static final Logger logger = LogManager.getLogger();
/**
* Search for 🤖 char on category name, if this category can't be find, auto create it
* Search for all voice channel where the user can connect
*
* @param guild Current guild
* @return General Category
*/
public static List<VoiceChannel> find(Guild guild){
SettingsUtils settingsUtils = SettingsUtils.getInstance();
GuildPreferenceEntity pref = settingsUtils.getPreference(guild);
public static List<VoiceChannel> find(Guild guild, Member member) {
ArrayList<VoiceChannel> list = new ArrayList<>();
List<String> chanels = pref.getVisibleVoiceChanel();
if(chanels == null || chanels.size() == 0){
pref = settingsUtils.setDefaultVoiceChannels(guild, pref);
chanels = pref.getVisibleVoiceChanel();
}
boolean needClean = false;
for(String prefChan : chanels){
VoiceChannel voice = guild.getVoiceChannelById(prefChan);
if(voice != null)
list.add(voice);
else
needClean = true;
}
if(needClean){
logger.debug("Need Clean.");
settingsUtils.cleanVisibleVoicePref(guild, pref);
VoiceChannel afk = guild.getAfkChannel();
GuildPreferenceEntity pref = SettingsUtils.getInstance().getPreference(guild);
String autoVoice = pref.getAutoVoiceChannelID();
for (VoiceChannel channel : guild.getVoiceChannels()) {
if (channel != afk && !channel.getId().equals(autoVoice) && member.getPermissions(channel).contains(Permission.VOICE_CONNECT)) {
list.add(channel);
}
}
list.sort(Comparator.comparingInt(GuildChannel::getPositionRaw));