Show only channel where the user can conect in music web page
This commit is contained in:
parent
1dd98ae419
commit
25374821e1
@ -29,9 +29,6 @@ public class GuildPreferenceEntity {
|
|||||||
|
|
||||||
private boolean dailyMadame;
|
private boolean dailyMadame;
|
||||||
|
|
||||||
@ElementCollection
|
|
||||||
private List<String> visibleVoiceChanel;
|
|
||||||
|
|
||||||
private boolean autoVoice;
|
private boolean autoVoice;
|
||||||
private String autoVoiceChannelID;
|
private String autoVoiceChannelID;
|
||||||
private String autoVoiceChannelTitle;
|
private String autoVoiceChannelTitle;
|
||||||
@ -45,7 +42,6 @@ public class GuildPreferenceEntity {
|
|||||||
boolean defaultRole,
|
boolean defaultRole,
|
||||||
String defaultRoleId,
|
String defaultRoleId,
|
||||||
boolean dailyMadame,
|
boolean dailyMadame,
|
||||||
ArrayList<String> visibleVoiceChanel,
|
|
||||||
boolean autoVoice,
|
boolean autoVoice,
|
||||||
String autoVoiceChannelID,
|
String autoVoiceChannelID,
|
||||||
String autoVoiceChannelTitle) {
|
String autoVoiceChannelTitle) {
|
||||||
@ -57,7 +53,6 @@ public class GuildPreferenceEntity {
|
|||||||
this.defaultRole = defaultRole;
|
this.defaultRole = defaultRole;
|
||||||
this.defaultRoleId = defaultRoleId;
|
this.defaultRoleId = defaultRoleId;
|
||||||
this.dailyMadame = dailyMadame;
|
this.dailyMadame = dailyMadame;
|
||||||
this.visibleVoiceChanel = visibleVoiceChanel;
|
|
||||||
this.autoVoice = autoVoice;
|
this.autoVoice = autoVoice;
|
||||||
this.autoVoiceChannelID = autoVoiceChannelID;
|
this.autoVoiceChannelID = autoVoiceChannelID;
|
||||||
this.autoVoiceChannelTitle = autoVoiceChannelTitle;
|
this.autoVoiceChannelTitle = autoVoiceChannelTitle;
|
||||||
@ -68,12 +63,8 @@ public class GuildPreferenceEntity {
|
|||||||
|
|
||||||
|
|
||||||
public static GuildPreferenceEntity getDefault(Guild guild) {
|
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() {
|
public Integer getId() {
|
||||||
@ -148,14 +139,6 @@ public class GuildPreferenceEntity {
|
|||||||
this.dailyMadame = dailyMadame;
|
this.dailyMadame = dailyMadame;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getVisibleVoiceChanel() {
|
|
||||||
return visibleVoiceChanel;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setVisibleVoiceChanel(List<String> visibleVoiceChanel) {
|
|
||||||
this.visibleVoiceChanel = visibleVoiceChanel;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isAutoVoice() {
|
public boolean isAutoVoice() {
|
||||||
return autoVoice;
|
return autoVoice;
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import net.Broken.DB.Entity.UserEntity;
|
|||||||
import net.Broken.DB.Repository.UserRepository;
|
import net.Broken.DB.Repository.UserRepository;
|
||||||
import net.Broken.MainBot;
|
import net.Broken.MainBot;
|
||||||
import net.Broken.RestApi.Data.*;
|
import net.Broken.RestApi.Data.*;
|
||||||
|
import net.Broken.Tools.CacheTools;
|
||||||
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
|
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
|
||||||
import net.Broken.Tools.UserManager.Stats.UserStatsUtils;
|
import net.Broken.Tools.UserManager.Stats.UserStatsUtils;
|
||||||
import net.Broken.Tools.UserManager.UserUtils;
|
import net.Broken.Tools.UserManager.UserUtils;
|
||||||
@ -14,8 +15,8 @@ import net.Broken.audio.AudioM;
|
|||||||
import net.Broken.audio.GetVoiceChanels;
|
import net.Broken.audio.GetVoiceChanels;
|
||||||
import net.Broken.audio.Youtube.SearchResult;
|
import net.Broken.audio.Youtube.SearchResult;
|
||||||
import net.Broken.audio.Youtube.YoutubeSearchRework;
|
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.Guild;
|
||||||
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -36,10 +37,9 @@ import java.util.List;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/music/")
|
@RequestMapping("/api/music/")
|
||||||
public class MusicWebAPIController {
|
public class MusicWebAPIController {
|
||||||
Logger logger = LogManager.getLogger();
|
|
||||||
private final
|
private final
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
|
Logger logger = LogManager.getLogger();
|
||||||
UserUtils userUtils = UserUtils.getInstance();
|
UserUtils userUtils = UserUtils.getInstance();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@ -173,19 +173,39 @@ public class MusicWebAPIController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/getChanel", method = RequestMethod.GET)
|
@RequestMapping(value = "/getChanel", method = RequestMethod.GET)
|
||||||
public ResponseEntity<List<ChanelData>> getChanel(@RequestParam(value = "guild") String guildId) { //TODO security issue ???!!!
|
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);
|
Guild guild = MainBot.jda.getGuildById(guildId);
|
||||||
if (guild == null) {
|
if (guild == null) {
|
||||||
logger.warn("Request whit no guild!");
|
logger.warn("Request whit no guild!");
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
} else {
|
|
||||||
logger.trace("getPlaylist for " + guild.getName());
|
|
||||||
}
|
}
|
||||||
|
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<>();
|
List<ChanelData> temp = new ArrayList<>();
|
||||||
for (VoiceChannel aChanel : GetVoiceChanels.find(guild)) {
|
for (VoiceChannel aChanel : GetVoiceChanels.find(guild, member)) {
|
||||||
temp.add(new ChanelData(aChanel.getName(), aChanel.getId(), aChanel.getPosition()));
|
temp.add(new ChanelData(aChanel.getName(), aChanel.getId(), aChanel.getPosition()));
|
||||||
}
|
}
|
||||||
return new ResponseEntity<>(temp, HttpStatus.OK);
|
return new ResponseEntity<>(temp, HttpStatus.OK);
|
||||||
|
} catch (UnknownTokenException e) {
|
||||||
|
logger.warn("Get Chanel without token!");
|
||||||
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
logger.warn("Get Chanel without token!");
|
||||||
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/search", method = RequestMethod.GET)
|
@RequestMapping(value = "/search", method = RequestMethod.GET)
|
||||||
@ -211,7 +231,6 @@ public class MusicWebAPIController {
|
|||||||
} else {
|
} else {
|
||||||
logger.warn("Search without token!");
|
logger.warn("Search without token!");
|
||||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,8 +2,10 @@ package net.Broken.audio;
|
|||||||
|
|
||||||
import net.Broken.DB.Entity.GuildPreferenceEntity;
|
import net.Broken.DB.Entity.GuildPreferenceEntity;
|
||||||
import net.Broken.Tools.SettingsUtils;
|
import net.Broken.Tools.SettingsUtils;
|
||||||
|
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.GuildChannel;
|
import net.dv8tion.jda.api.entities.GuildChannel;
|
||||||
|
import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.VoiceChannel;
|
import net.dv8tion.jda.api.entities.VoiceChannel;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -16,38 +18,24 @@ import java.util.List;
|
|||||||
* Used to find general voice channels
|
* Used to find general voice channels
|
||||||
*/
|
*/
|
||||||
public class GetVoiceChanels {
|
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
|
* @param guild Current guild
|
||||||
* @return General Category
|
* @return General Category
|
||||||
*/
|
*/
|
||||||
public static List<VoiceChannel> find(Guild guild){
|
public static List<VoiceChannel> find(Guild guild, Member member) {
|
||||||
SettingsUtils settingsUtils = SettingsUtils.getInstance();
|
|
||||||
GuildPreferenceEntity pref = settingsUtils.getPreference(guild);
|
|
||||||
|
|
||||||
ArrayList<VoiceChannel> list = new ArrayList<>();
|
ArrayList<VoiceChannel> list = new ArrayList<>();
|
||||||
List<String> chanels = pref.getVisibleVoiceChanel();
|
VoiceChannel afk = guild.getAfkChannel();
|
||||||
|
GuildPreferenceEntity pref = SettingsUtils.getInstance().getPreference(guild);
|
||||||
if(chanels == null || chanels.size() == 0){
|
String autoVoice = pref.getAutoVoiceChannelID();
|
||||||
pref = settingsUtils.setDefaultVoiceChannels(guild, pref);
|
for (VoiceChannel channel : guild.getVoiceChannels()) {
|
||||||
chanels = pref.getVisibleVoiceChanel();
|
if (channel != afk && !channel.getId().equals(autoVoice) && member.getPermissions(channel).contains(Permission.VOICE_CONNECT)) {
|
||||||
|
list.add(channel);
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
list.sort(Comparator.comparingInt(GuildChannel::getPositionRaw));
|
list.sort(Comparator.comparingInt(GuildChannel::getPositionRaw));
|
||||||
|
Loading…
Reference in New Issue
Block a user