ClaptrapBot/src/main/java/net/Broken/Tools/SettingsUtils.java

316 lines
11 KiB
Java
Raw Normal View History

package net.Broken.Tools;
import net.Broken.DB.Entity.GuildPreferenceEntity;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.GuildPreferenceRepository;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot;
import net.Broken.RestApi.Data.Settings.GetSettingsData;
2018-09-24 18:43:19 +03:00
import net.Broken.RestApi.Data.Settings.PostSetSettings;
import net.Broken.RestApi.Data.Settings.Value;
import net.Broken.SpringContext;
import net.Broken.Tools.UserManager.UserUtils;
2019-09-10 09:03:16 +02:00
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.ApplicationContext;
import java.util.ArrayList;
2021-02-18 14:48:58 +01:00
import java.util.Collections;
import java.util.List;
public class SettingsUtils {
private static SettingsUtils INSTANCE;
2021-02-18 14:48:58 +01:00
private final Logger logger = LogManager.getLogger();
2021-02-18 14:48:58 +01:00
public static SettingsUtils getInstance() {
return (INSTANCE == null) ? new SettingsUtils() : INSTANCE;
}
2021-02-18 14:48:58 +01:00
private final GuildPreferenceRepository guildPreferenceRepository;
private final UserRepository userRepository;
private SettingsUtils() {
ApplicationContext context = SpringContext.getAppContext();
guildPreferenceRepository = (GuildPreferenceRepository) context.getBean("guildPreferenceRepository");
userRepository = (UserRepository) context.getBean("userRepository");
}
2021-02-18 14:48:58 +01:00
/**
* Extract all settings for displaying on webpage
*
* @param guild The guild
* @return All formatted settings
*/
public ArrayList<GetSettingsData> extractSettings(Guild guild) {
2021-02-18 14:48:58 +01:00
ArrayList<GetSettingsData> list = new ArrayList<>();
List<GuildPreferenceEntity> guildPrefList = guildPreferenceRepository.findByGuildId(guild.getId());
GuildPreferenceEntity guildPref;
2021-02-18 14:48:58 +01:00
if (guildPrefList.isEmpty()) {
guildPref = GuildPreferenceEntity.getDefault(guild);
guildPreferenceRepository.save(guildPref);
2021-02-18 14:48:58 +01:00
} else
guildPref = guildPrefList.get(0);
list.add(new GetSettingsData(
"Enable Welcome Message",
2021-02-18 14:48:58 +01:00
null,
"welcome",
GetSettingsData.TYPE.BOOL,
null,
Boolean.toString(guildPref.isWelcome())
));
list.add(new GetSettingsData(
"Welcome Message chanel",
2021-02-18 14:48:58 +01:00
null,
"welcome_chanel_id",
GetSettingsData.TYPE.LIST,
getTextChannels(guild),
guildPref.getWelcomeChanelID()
));
list.add(new GetSettingsData(
"Welcome Message",
2021-02-18 14:48:58 +01:00
null,
"welcome_message",
GetSettingsData.TYPE.STRING,
null,
guildPref.getWelcomeMessage()
));
list.add(new GetSettingsData(
"Enable Default Role",
2021-02-18 14:48:58 +01:00
null,
"default_role",
GetSettingsData.TYPE.BOOL,
null,
Boolean.toString(guildPref.isDefaultRole())
));
list.add(new GetSettingsData(
"Default Role",
2021-02-18 14:48:58 +01:00
null,
"default_role_id",
GetSettingsData.TYPE.LIST,
getRoles(guild),
guildPref.getDefaultRoleId()
));
list.add(new GetSettingsData(
"Enable Anti Spam",
2021-02-18 14:48:58 +01:00
null,
"anti_spam",
GetSettingsData.TYPE.BOOL,
null,
Boolean.toString(guildPref.isAntiSpam())
));
list.add(new GetSettingsData(
"Enable Daily Madame Message",
2021-02-18 14:48:58 +01:00
null,
"daily_madame",
GetSettingsData.TYPE.BOOL,
null,
Boolean.toString(guildPref.isDailyMadame())
));
2021-02-18 14:48:58 +01:00
list.add(new GetSettingsData(
"Enable Auto Create Voice Chanel",
null,
"auto_voice",
GetSettingsData.TYPE.BOOL,
null,
Boolean.toString(guildPref.isAutoVoice())
));
list.add(new GetSettingsData(
"Base Voice Channel For Auto Create",
"If someone joint this channel, a new voice channel will be created with the same settings.",
"auto_voice_base_channel",
GetSettingsData.TYPE.LIST,
getVoiceChannels(guild, null),
guildPref.getAutoVoiceChannelID()
));
list.add(new GetSettingsData(
"Auto Created Voice Channel title",
"Auto created voice channel will use this title, @count will be replaced by the channel count.",
"auto_voice_channel_title",
GetSettingsData.TYPE.STRING,
null,
guildPref.getAutoVoiceChannelTitle()
));
2018-12-03 15:25:57 +02:00
return list;
}
2021-02-18 14:48:58 +01:00
/**
* Check if the user have the permission to set settings
*
* @param token User token
* @param guild Guild
* @return True if user have ADMINISTRATOR permission
*/
public boolean checkPermission(String token, String guild) {
if (token == null || guild == null) {
return false;
2021-02-18 14:48:58 +01:00
} else {
try {
UserEntity user = UserUtils.getInstance().getUserWithApiToken(userRepository, token);
User jdaUser = MainBot.jda.getUserById(user.getJdaId());
Guild jdaGuild = MainBot.jda.getGuildById(guild);
2021-02-18 14:48:58 +01:00
if (jdaGuild == null || jdaUser == null)
return false;
2021-02-18 14:48:58 +01:00
Member guildUser = jdaGuild.getMember(jdaUser);
if (guildUser == null)
return false;
return guildUser.hasPermission(Permission.ADMINISTRATOR);
} catch (Exception e) {
2018-12-02 16:00:14 +02:00
logger.debug("Unknown Token or user :" + token);
return false;
}
}
}
2021-02-18 14:48:58 +01:00
public boolean setSettings(Guild guild, List<PostSetSettings> settings) {
2018-09-24 18:43:19 +03:00
GuildPreferenceEntity pref = getPreference(guild);
2021-02-18 14:48:58 +01:00
for (PostSetSettings setting : settings) {
2018-09-24 18:43:19 +03:00
String value = setting.val;
logger.debug(setting.id + " : " + value);
switch (setting.id) {
case "anti_spam":
2021-02-18 14:48:58 +01:00
boolean result_as = Boolean.parseBoolean(value);
pref.setAntiSpam(result_as);
2018-09-24 18:43:19 +03:00
break;
case "default_role":
2021-02-18 14:48:58 +01:00
boolean result_df = Boolean.parseBoolean(value);
pref.setDefaultRole(result_df);
pref = guildPreferenceRepository.save(pref);
2018-09-24 18:43:19 +03:00
break;
2021-02-18 14:48:58 +01:00
2018-09-24 18:43:19 +03:00
case "default_role_id":
try {
Role role = guild.getRoleById(value);
if (role != null) {
pref.setDefaultRoleId(role.getId());
pref = guildPreferenceRepository.save(pref);
2021-02-18 14:48:58 +01:00
} else
2018-09-24 18:43:19 +03:00
throw new NumberFormatException();
} catch (NumberFormatException e) {
logger.error("default_role_id error. Key: " + setting.id + " Val: " + setting.val);
return false;
}
break;
2021-02-18 14:48:58 +01:00
2018-09-24 18:43:19 +03:00
case "welcome":
2021-02-18 14:48:58 +01:00
boolean result_w = Boolean.parseBoolean(value);
pref.setWelcome(result_w);
2018-09-24 18:43:19 +03:00
break;
case "welcome_chanel_id":
try {
2021-02-18 14:48:58 +01:00
TextChannel channel = guild.getTextChannelById(value);
if (channel != null) {
pref.setWelcomeChanelID(channel.getId());
} else
2018-09-24 18:43:19 +03:00
throw new NumberFormatException();
} catch (NumberFormatException e) {
logger.error("welcome_chanel_id error. Key: " + setting.id + " Val: " + setting.val);
return false;
}
break;
2021-02-18 14:48:58 +01:00
2018-09-24 18:43:19 +03:00
case "welcome_message":
pref.setWelcomeMessage(value);
break;
case "daily_madame":
2021-02-18 14:48:58 +01:00
boolean result_dm = Boolean.parseBoolean(value);
pref.setDailyMadame(result_dm);
break;
2018-09-24 18:43:19 +03:00
2021-02-18 14:48:58 +01:00
case "auto_voice":
boolean result_av = Boolean.parseBoolean(value);
pref.setAutoVoice(result_av);
break;
2018-09-24 18:43:19 +03:00
2021-02-18 14:48:58 +01:00
case "auto_voice_base_channel":
VoiceChannel channel = guild.getVoiceChannelById(value);
if (channel == null) {
logger.error("voices_channels error, bad ID.");
2018-09-24 18:43:19 +03:00
return false;
2021-02-18 14:48:58 +01:00
} else
pref.setAutoVoiceChannelID(channel.getId());
break;
case "auto_voice_channel_title":
pref.setAutoVoiceChannelTitle(value);
2018-09-24 18:43:19 +03:00
break;
}
}
2018-12-03 15:25:57 +02:00
guildPreferenceRepository.save(pref);
2018-09-24 18:43:19 +03:00
return true;
}
2021-02-18 14:48:58 +01:00
private List<Value> getTextChannels(Guild guild) {
List<Value> channels = new ArrayList<>();
2021-02-18 14:48:58 +01:00
for (TextChannel channel : guild.getTextChannels()) {
channels.add(new Value(channel.getName(), channel.getId()));
}
return channels;
}
2021-02-18 14:48:58 +01:00
private List<Value> getRoles(Guild guild) {
List<Value> roles = new ArrayList<>();
2021-02-18 14:48:58 +01:00
for (Role role : guild.getRoles()) {
roles.add(new Value(role.getName(), role.getId()));
}
return roles;
}
2018-09-24 18:43:19 +03:00
2018-12-03 15:25:57 +02:00
2021-02-18 14:48:58 +01:00
private List<Value> getVoiceChannels(Guild guild, List<String> selected) {
List<Value> channels = new ArrayList<>();
for (VoiceChannel voiceChannel : guild.getVoiceChannels()) {
if (selected == null)
channels.add(new Value(voiceChannel.getName(), voiceChannel.getId()));
else
channels.add(new Value(voiceChannel.getName(), voiceChannel.getId(), selected.contains(voiceChannel.getId())));
2018-12-03 15:25:57 +02:00
}
2021-02-18 14:48:58 +01:00
return channels;
2018-12-03 15:25:57 +02:00
}
2021-02-18 14:48:58 +01:00
private List<String> checkVoiceChanel(Guild guild, List<String> values) {
2018-12-03 15:25:57 +02:00
List<String> list = new ArrayList<>();
2021-02-18 14:48:58 +01:00
for (String value : values) {
2018-12-03 15:25:57 +02:00
if (guild.getVoiceChannelById(value) != null) {
list.add(value);
} else {
2021-02-18 14:48:58 +01:00
logger.error("Unknown voice channel id: " + value);
2018-12-03 15:25:57 +02:00
list = null;
break;
}
}
return list;
}
2021-02-18 14:48:58 +01:00
public GuildPreferenceEntity getPreference(Guild guild) {
2018-09-24 18:43:19 +03:00
List<GuildPreferenceEntity> guildPrefList = guildPreferenceRepository.findByGuildId(guild.getId());
GuildPreferenceEntity guildPref;
2021-02-18 14:48:58 +01:00
if (guildPrefList.isEmpty()) {
2018-09-24 18:43:19 +03:00
logger.info("Generate default pref for " + guild.getName());
guildPref = GuildPreferenceEntity.getDefault(guild);
guildPreferenceRepository.save(guildPref);
2021-02-18 14:48:58 +01:00
} else
2018-09-24 18:43:19 +03:00
guildPref = guildPrefList.get(0);
return guildPref;
}
}