Add visible voice channels setting

This commit is contained in:
Sebastien 2018-12-03 15:25:57 +02:00
parent 75aa1eb241
commit 3813efff01
10 changed files with 219 additions and 82 deletions

View File

@ -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<String> 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<String> 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<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);
}
public Integer getId() {
@ -120,4 +129,12 @@ public class GuildPreferenceEntity {
public void setDailyMadame(boolean dailyMadame) {
this.dailyMadame = dailyMadame;
}
public List<String> getVisibleVoiceChanel() {
return visibleVoiceChanel;
}
public void setVisibleVoiceChanel(List<String> visibleVoiceChanel) {
this.visibleVoiceChanel = visibleVoiceChanel;
}
}

View File

@ -24,7 +24,7 @@ public class GetSettingsData {
}
public enum TYPE{
BOOL,LIST,STRING
BOOL,LIST,STRING,SELECT_LIST
}
}

View File

@ -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<String> vals;
}

View File

@ -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;
}
}

View File

@ -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<ChanelData> 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);

View File

@ -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<String> 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<Value> getVoiceChanels(Guild guild, GuildPreferenceEntity guildPref){
List<String> prefVoice = new ArrayList<>(guildPref.getVisibleVoiceChanel());
if(prefVoice.size() == 0){
guildPref = setDefaultVoiceChanels(guild, guildPref);
}
List<Value> 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<String> edit = guildPref.getVisibleVoiceChanel();
for(String prefVoiceItem : prefVoice){
edit.remove(prefVoiceItem);
}
guildPref.setVisibleVoiceChanel(edit);
guildPreferenceRepository.save(guildPref);
}
return chanels;
}
private List<String> checkVoiceChanel(Guild guild, List<String> values){
List<String> 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<String> 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<String> 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<GuildPreferenceEntity> guildPrefList = guildPreferenceRepository.findByGuildId(guild.getId());
GuildPreferenceEntity guildPref;

View File

@ -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<VoiceChannel> find(Guild guild){
List<Category> 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;
}
}

View File

@ -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<VoiceChannel> find(Guild guild){
SettingsUtils settingsUtils = SettingsUtils.getInstance();
GuildPreferenceEntity pref = settingsUtils.getPreference(guild);
ArrayList<VoiceChannel> list = new ArrayList<>();
List<String> 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;
}
}

View File

@ -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();

View File

@ -59,6 +59,22 @@
</select>
</div>
<div th:id="${setting.id}" class="input-field col l12 m12 s12 collect-select-multiple"
th:if="${setting.type.toString() == 'SELECT_LIST'}">
<select multiple="multiple">
<option disabled="disabled" value="">Choose your
option
</option>
<option th:each="val : ${setting.values}" th:value="${val.id}"
th:text="${#strings.capitalize(val.name)}"
th:selected="${val.selected} == true"></option>
</select>
</div>
<div class="input-field col l12 m12 s12 collect" th:if="${setting.type.toString() == 'STRING'}">
<input th:id="${setting.id}" placeholder="Use @name variable" th:value="${setting.current}"
id="first_name" type="text" class="validate collect-text"/>