From 3813efff01eb9215d6ce21adebdee54460803409 Mon Sep 17 00:00:00 2001 From: Sebastien Date: Mon, 3 Dec 2018 15:25:57 +0200 Subject: [PATCH] Add visible voice channels setting --- .../DB/Entity/GuildPreferenceEntity.java | 29 ++++- .../Data/Settings/GetSettingsData.java | 2 +- .../Data/Settings/PostSetSettings.java | 3 + .../Broken/RestApi/Data/Settings/Value.java | 6 + .../Broken/RestApi/MusicWebAPIController.java | 7 +- .../java/net/Broken/Tools/SettingsUtils.java | 109 +++++++++++++++--- .../java/net/Broken/audio/FindGeneral.java | 57 --------- .../net/Broken/audio/GetVoiceChanels.java | 61 ++++++++++ src/main/resources/static/js/settings.js | 11 ++ src/main/resources/templates/settings.html | 16 +++ 10 files changed, 219 insertions(+), 82 deletions(-) delete mode 100644 src/main/java/net/Broken/audio/FindGeneral.java create mode 100644 src/main/java/net/Broken/audio/GetVoiceChanels.java diff --git a/src/main/java/net/Broken/DB/Entity/GuildPreferenceEntity.java b/src/main/java/net/Broken/DB/Entity/GuildPreferenceEntity.java index e879f47..c57f4a0 100644 --- a/src/main/java/net/Broken/DB/Entity/GuildPreferenceEntity.java +++ b/src/main/java/net/Broken/DB/Entity/GuildPreferenceEntity.java @@ -1,11 +1,11 @@ package net.Broken.DB.Entity; import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.VoiceChannel; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; @Entity public class GuildPreferenceEntity { @@ -29,9 +29,12 @@ public class GuildPreferenceEntity { private boolean dailyMadame; + @ElementCollection + private List visibleVoiceChanel; - public GuildPreferenceEntity(String guildId, boolean antiSpam, boolean welcome, String welcomeMessage, String welcomeChanelID, boolean defaultRole, String defaultRoleId, boolean dailyMadame) { + + public GuildPreferenceEntity(String guildId, boolean antiSpam, boolean welcome, String welcomeMessage, String welcomeChanelID, boolean defaultRole, String defaultRoleId, boolean dailyMadame, ArrayList visibleVoiceChanel) { this.guildId = guildId; this.antiSpam = antiSpam; this.welcome = welcome; @@ -40,13 +43,19 @@ public class GuildPreferenceEntity { this.defaultRole = defaultRole; this.defaultRoleId = defaultRoleId; this.dailyMadame = dailyMadame; + this.visibleVoiceChanel = visibleVoiceChanel; } public GuildPreferenceEntity(){} public static GuildPreferenceEntity getDefault(Guild guild){ - return new GuildPreferenceEntity(guild.getId(), false, false, "Welcome to this awesome server @name! ", " ", false, " ", true); + ArrayList 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); } public Integer getId() { @@ -120,4 +129,12 @@ public class GuildPreferenceEntity { public void setDailyMadame(boolean dailyMadame) { this.dailyMadame = dailyMadame; } + + public List getVisibleVoiceChanel() { + return visibleVoiceChanel; + } + + public void setVisibleVoiceChanel(List visibleVoiceChanel) { + this.visibleVoiceChanel = visibleVoiceChanel; + } } diff --git a/src/main/java/net/Broken/RestApi/Data/Settings/GetSettingsData.java b/src/main/java/net/Broken/RestApi/Data/Settings/GetSettingsData.java index a51a453..b542ff0 100644 --- a/src/main/java/net/Broken/RestApi/Data/Settings/GetSettingsData.java +++ b/src/main/java/net/Broken/RestApi/Data/Settings/GetSettingsData.java @@ -24,7 +24,7 @@ public class GetSettingsData { } public enum TYPE{ - BOOL,LIST,STRING + BOOL,LIST,STRING,SELECT_LIST } } diff --git a/src/main/java/net/Broken/RestApi/Data/Settings/PostSetSettings.java b/src/main/java/net/Broken/RestApi/Data/Settings/PostSetSettings.java index 1bb362e..d8e04ae 100644 --- a/src/main/java/net/Broken/RestApi/Data/Settings/PostSetSettings.java +++ b/src/main/java/net/Broken/RestApi/Data/Settings/PostSetSettings.java @@ -1,6 +1,9 @@ package net.Broken.RestApi.Data.Settings; +import java.util.List; + public class PostSetSettings { public String id; public String val; + public List vals; } diff --git a/src/main/java/net/Broken/RestApi/Data/Settings/Value.java b/src/main/java/net/Broken/RestApi/Data/Settings/Value.java index 7ef7b5d..248f296 100644 --- a/src/main/java/net/Broken/RestApi/Data/Settings/Value.java +++ b/src/main/java/net/Broken/RestApi/Data/Settings/Value.java @@ -3,6 +3,7 @@ package net.Broken.RestApi.Data.Settings; public class Value { public String name; public String id; + public boolean selected; public Value() { } @@ -11,4 +12,9 @@ public class Value { this.name = name; this.id = id; } + public Value(String name, String id, boolean selected) { + this.name = name; + this.id = id; + this.selected = selected; + } } diff --git a/src/main/java/net/Broken/RestApi/MusicWebAPIController.java b/src/main/java/net/Broken/RestApi/MusicWebAPIController.java index 7fac26e..8a77a00 100644 --- a/src/main/java/net/Broken/RestApi/MusicWebAPIController.java +++ b/src/main/java/net/Broken/RestApi/MusicWebAPIController.java @@ -3,21 +3,18 @@ package net.Broken.RestApi; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; import com.sedmelluq.discord.lavaplayer.track.AudioTrack; -import net.Broken.Commands.Music; 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.EmbedMessageUtils; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; import net.Broken.Tools.UserManager.UserUtils; import net.Broken.audio.AudioM; -import net.Broken.audio.FindGeneral; +import net.Broken.audio.GetVoiceChanels; import net.Broken.audio.Youtube.SearchResult; import net.Broken.audio.Youtube.YoutubeTools; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.VoiceChannel; -import org.apache.http.HttpResponse; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -193,7 +190,7 @@ public class MusicWebAPIController { logger.trace("getPlaylist for " + guild.getName()); } List temp = new ArrayList<>(); - for(VoiceChannel aChanel : FindGeneral.find(guild)){ + for(VoiceChannel aChanel : GetVoiceChanels.find(guild)){ temp.add(new ChanelData(aChanel.getName(),aChanel.getId(),aChanel.getPosition())); } return new ResponseEntity<>(temp, HttpStatus.OK); diff --git a/src/main/java/net/Broken/Tools/SettingsUtils.java b/src/main/java/net/Broken/Tools/SettingsUtils.java index bac9642..7963cd7 100644 --- a/src/main/java/net/Broken/Tools/SettingsUtils.java +++ b/src/main/java/net/Broken/Tools/SettingsUtils.java @@ -13,10 +13,7 @@ import net.Broken.SpringContext; import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException; import net.Broken.Tools.UserManager.UserUtils; import net.dv8tion.jda.core.Permission; -import net.dv8tion.jda.core.entities.Guild; -import net.dv8tion.jda.core.entities.Role; -import net.dv8tion.jda.core.entities.TextChannel; -import net.dv8tion.jda.core.entities.User; +import net.dv8tion.jda.core.entities.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.context.ApplicationContext; @@ -36,8 +33,8 @@ public class SettingsUtils { return (INSTANCE == null) ? new SettingsUtils() : INSTANCE; } - GuildPreferenceRepository guildPreferenceRepository; - UserRepository userRepository; + private GuildPreferenceRepository guildPreferenceRepository; + private UserRepository userRepository; private SettingsUtils() { @@ -60,7 +57,13 @@ public class SettingsUtils { else guildPref = guildPrefList.get(0); - + list.add(new GetSettingsData( + "Visible Voices Channels", + "voices_channels", + GetSettingsData.TYPE.SELECT_LIST, + getVoiceChanels(guild, guildPref), + null + )); list.add(new GetSettingsData( "Enable Welcome Message", @@ -117,6 +120,9 @@ public class SettingsUtils { Boolean.toString(guildPref.isDailyMadame()) )); + + + return list; } @@ -154,11 +160,26 @@ public class SettingsUtils { String value = setting.val; logger.debug(setting.id + " : " + value); switch (setting.id) { + + case "voices_channels": + + List list = checkVoiceChanel(guild, setting.vals); + if(list == null){ + logger.error("voices_channels error, bad ID."); + return false; + } + else{ + pref.setVisibleVoiceChanel(list); + } + + break; + + + case "anti_spam": if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) { boolean result = Boolean.parseBoolean(value); pref.setAntiSpam(result); - pref = guildPreferenceRepository.save(pref); } else { logger.error("anti_spam error. Key: " + setting.id + " Val: " + setting.val); return false; @@ -201,7 +222,6 @@ public class SettingsUtils { if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) { boolean result = Boolean.parseBoolean(value); pref.setWelcome(result); - pref = guildPreferenceRepository.save(pref); } else { logger.error("welcome error. Key: " + setting.id + " Val: " + setting.val); return false; @@ -213,8 +233,6 @@ public class SettingsUtils { if (chanel != null) { pref.setWelcomeChanelID(chanel.getId()); - pref = guildPreferenceRepository.save(pref); - } else { throw new NumberFormatException(); } @@ -226,7 +244,6 @@ public class SettingsUtils { break; case "welcome_message": pref.setWelcomeMessage(value); - pref = guildPreferenceRepository.save(pref); break; @@ -234,7 +251,6 @@ public class SettingsUtils { if (value.toLowerCase().equals("true") || value.toLowerCase().equals("false")) { boolean result = Boolean.parseBoolean(value); pref.setDailyMadame(result); - pref = guildPreferenceRepository.save(pref); } else { logger.error("daily_madame error. Key: " + setting.id + " Val: " + setting.val); @@ -244,6 +260,7 @@ public class SettingsUtils { break; } } + guildPreferenceRepository.save(pref); return true; } @@ -265,6 +282,72 @@ public class SettingsUtils { return roles; } + + private List getVoiceChanels(Guild guild, GuildPreferenceEntity guildPref){ + + + List prefVoice = new ArrayList<>(guildPref.getVisibleVoiceChanel()); + if(prefVoice.size() == 0){ + guildPref = setDefaultVoiceChanels(guild, guildPref); + } + + List chanels = new ArrayList<>(); + for(VoiceChannel voiceChannel : guild.getVoiceChannels()){ + chanels.add(new Value(voiceChannel.getName(), voiceChannel.getId(), prefVoice.contains(voiceChannel.getId()))); + prefVoice.remove(voiceChannel.getId()); + } + + if(prefVoice.size() != 0){ + List edit = guildPref.getVisibleVoiceChanel(); + for(String prefVoiceItem : prefVoice){ + edit.remove(prefVoiceItem); + } + guildPref.setVisibleVoiceChanel(edit); + guildPreferenceRepository.save(guildPref); + } + + return chanels; + + } + + private List checkVoiceChanel(Guild guild, List values){ + List list = new ArrayList<>(); + for(String value : values){ + + if (guild.getVoiceChannelById(value) != null) { + list.add(value); + } else { + logger.error("Unknown voice chanel id: " + value); + list = null; + break; + } + } + return list; + } + + public GuildPreferenceEntity cleanVoicePref(Guild guild, GuildPreferenceEntity guildPref){ + List voice = guildPref.getVisibleVoiceChanel(); + for(String prefVoice : guildPref.getVisibleVoiceChanel()){ + if(guild.getVoiceChannelById(prefVoice) == null) + voice.remove(prefVoice); + } + guildPref.setVisibleVoiceChanel(voice); + return guildPreferenceRepository.save(guildPref); + } + + + public GuildPreferenceEntity setDefaultVoiceChanels(Guild guild, GuildPreferenceEntity guildPref){ + List prefVoice = guildPref.getVisibleVoiceChanel(); + if(prefVoice == null) + prefVoice = new ArrayList<>(); + for(VoiceChannel voiceChannel : guild.getVoiceChannels()){ + prefVoice.add(voiceChannel.getId()); + } + guildPref.setVisibleVoiceChanel(prefVoice); + return guildPreferenceRepository.save(guildPref); + + } + public GuildPreferenceEntity getPreference(Guild guild){ List guildPrefList = guildPreferenceRepository.findByGuildId(guild.getId()); GuildPreferenceEntity guildPref; diff --git a/src/main/java/net/Broken/audio/FindGeneral.java b/src/main/java/net/Broken/audio/FindGeneral.java deleted file mode 100644 index 26813e0..0000000 --- a/src/main/java/net/Broken/audio/FindGeneral.java +++ /dev/null @@ -1,57 +0,0 @@ -package net.Broken.audio; - -import net.dv8tion.jda.core.entities.Category; -import net.dv8tion.jda.core.entities.Channel; -import net.dv8tion.jda.core.entities.Guild; -import net.dv8tion.jda.core.entities.VoiceChannel; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.List; - -/** - * Used to find general voice channels - */ -public class FindGeneral { - static Logger logger = LogManager.getLogger(); - - /** - * Search for 🤖 char on category name, if this category can't be find, auto create it - * @param guild Current guild - * @return General Category - */ - public static List find(Guild guild){ - List categories = guild.getCategories(); - Category finded = null; - for(Category cat : categories){ - if(cat.getName().contains("\uD83E\uDD16")){ - finded = cat; - break; - } - } - if(finded == null) - return guild.getVoiceChannels(); - - return finded.getVoiceChannels(); - - } - - /** - * Create default category "🤖 Salons Vocaux 🤖", and create basic voice channel on it. - * @param guild Current guild - * @return Brand new General Category - */ - private static Category create(Guild guild){ - logger.info("Can't find general voice chanel, creating it!"); - Channel temp = guild.getController().createCategory("\uD83E\uDD16 Salons Vocaux \uD83E\uDD16").complete(); - Category cat = guild.getCategoryById(temp.getId()); - cat.createVoiceChannel("Général").complete(); - cat.createVoiceChannel("Cour").complete(); - cat.createVoiceChannel("\uD83C\uDFAE Game 1 \uD83C\uDFAE").complete(); - cat.createVoiceChannel("\uD83C\uDFAE Game 2 \uD83C\uDFAE").complete(); - cat.createVoiceChannel("\uD83C\uDFAE Game 3 \uD83C\uDFAE").complete(); - cat.createVoiceChannel("AFK").complete(); - cat = guild.getCategoryById(temp.getId()); - return cat; - } -} diff --git a/src/main/java/net/Broken/audio/GetVoiceChanels.java b/src/main/java/net/Broken/audio/GetVoiceChanels.java new file mode 100644 index 0000000..8eae0d7 --- /dev/null +++ b/src/main/java/net/Broken/audio/GetVoiceChanels.java @@ -0,0 +1,61 @@ +package net.Broken.audio; + +import net.Broken.DB.Entity.GuildPreferenceEntity; +import net.Broken.DB.Repository.GuildPreferenceRepository; +import net.Broken.DB.Repository.UserRepository; +import net.Broken.SpringContext; +import net.Broken.Tools.SettingsUtils; +import net.dv8tion.jda.core.entities.Category; +import net.dv8tion.jda.core.entities.Channel; +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.VoiceChannel; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.context.ApplicationContext; + +import java.util.ArrayList; +import java.util.List; + +/** + * Used to find general voice channels + */ +public class GetVoiceChanels { + private static Logger logger = LogManager.getLogger(); + + /** + * Search for 🤖 char on category name, if this category can't be find, auto create it + * @param guild Current guild + * @return General Category + */ + public static List find(Guild guild){ + SettingsUtils settingsUtils = SettingsUtils.getInstance(); + GuildPreferenceEntity pref = settingsUtils.getPreference(guild); + + ArrayList list = new ArrayList<>(); + List chanels = pref.getVisibleVoiceChanel(); + + if(chanels == null || chanels.size() == 0){ + pref = settingsUtils.setDefaultVoiceChanels(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.cleanVoicePref(guild, pref); + + } + + return list; + + } + +} diff --git a/src/main/resources/static/js/settings.js b/src/main/resources/static/js/settings.js index ba74276..d1a326b 100644 --- a/src/main/resources/static/js/settings.js +++ b/src/main/resources/static/js/settings.js @@ -22,6 +22,17 @@ $(document).ready(function(){ }); + + var select_multi = $('.collect-select-multiple'); + select_multi.each(function(){ + var instance = M.FormSelect.getInstance($(this).find("select")[0]); + + var id = $(this).attr("id"); + post_json["settings"].push({"id" : id, "vals" : instance.getSelectedValues()}); + + + }); + var switch_collected = $('.collect-switch'); switch_collected.each(function(){ var val = $(this).is(':checked').toString(); diff --git a/src/main/resources/templates/settings.html b/src/main/resources/templates/settings.html index 5c01a46..3afd0f0 100644 --- a/src/main/resources/templates/settings.html +++ b/src/main/resources/templates/settings.html @@ -59,6 +59,22 @@ + +
+ +
+ + +