MissingIdentifier/src/main/java/Controll/GuildController.java

209 lines
6.2 KiB
Java

package Controll;
import Modules.*;
import Modules.Module;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.events.message.guild.react.GenericGuildMessageReactionEvent;
import net.dv8tion.jda.api.events.user.GenericUserEvent;
import net.dv8tion.jda.api.events.user.update.GenericUserPresenceEvent;
import org.json.simple.JSONObject;
import net.dv8tion.jda.api.events.guild.GenericGuildEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
public class GuildController {
private final Logger logger = LoggerFactory.getLogger(GuildController.class);
private ArrayList<Module> modules;
private String PREFIX;
private long GUILD_ID;
private long MOD_ROLE;
private Controller controller;
private JSONObject moduleConf;
private JDA jda;
/**
* init Guild and load settings
*/
public GuildController(JSONObject config, Controller controller, JDA jda) {
//safe config
this.controller = controller;
this.jda = jda;
GUILD_ID = Long.parseLong(config.get("GUILD_ID").toString());
JSONObject guildConfig = (JSONObject) config.get("config");
PREFIX = "\\Q" + guildConfig.get("PREFIX").toString() + "\\E";
MOD_ROLE = Long.parseLong(guildConfig.get("MODROLE").toString());
moduleConf = (JSONObject) config.get("moduleConfig");
loadModules();
}
/**
* on Update try to execute init modules
* @param event update event
*/
public void execute(GenericEvent event) {
if (event instanceof GuildMessageReceivedEvent && ((GuildMessageReceivedEvent)event).getAuthor().isBot())
return;
if (event instanceof GenericGuildMessageReactionEvent && ((GenericGuildMessageReactionEvent)event).getUser().isBot())
return;
modules.forEach(mod -> mod.execute(event));
}
public long getGUILD_ID(){
return GUILD_ID;
}
public String getPREFIX(){
return PREFIX;
}
public long getMOD_ROLE() {
return MOD_ROLE;
}
protected void reload (JDA jda){
logger.info("reload guild: " + GUILD_ID);
this.jda = jda;
loadModules();
}
private void loadModules() {
logger.info("load guild modules of:" + GUILD_ID);
//Init Modules
modules = new ArrayList<Module>();
if (checkForActivate(AutoSelect.COMMAND))
modules.add(new AutoSelect(this, (JSONObject) moduleConf.get(AutoSelect.COMMAND)));
if (checkForActivate(LoveCalc.command))
modules.add(new LoveCalc(this));
if (checkForActivate(Purge.COMMAND))
modules.add(new Purge(this));
if (checkForActivate(Quote.command))
modules.add(new Quote(this));
if (checkForActivate(RandomResponde.command))
modules.add(new RandomResponde(this, (JSONObject)moduleConf.get(RandomResponde.command)));
if (checkForActivate(Dice.COMMAND))
modules.add(new Dice(this));
if (checkForActivate(Welcome.command))
modules.add(new Welcome(this, (JSONObject)moduleConf.get(Welcome.command)));
if (checkForActivate(Shuffle.command))
modules.add(new Shuffle(this, (JSONObject) moduleConf.get(Shuffle.command)));
if (checkForActivate(Annoy.COMMAND))
modules.add(new Annoy(this, (JSONObject) moduleConf.get(Annoy.COMMAND)));
if (checkForActivate(VoiceLobby.command))
modules.add(new VoiceLobby(this, (JSONObject)moduleConf.get(VoiceLobby.command)));
if (checkForActivate(Fold.COMMAND))
modules.add(new Fold(this));
if (checkForActivate(Stats.COMMAND))
modules.add(new Stats(this, (JSONObject)moduleConf.get(Stats.COMMAND)));
if (checkForActivate(OnlineHighlight.command))
modules.add(new OnlineHighlight(this, (JSONObject)moduleConf.get(OnlineHighlight.command)));
if (checkForActivate(SecretChannel.COMMAND))
modules.add(new SecretChannel(this, (JSONObject)moduleConf.get(SecretChannel.COMMAND)));
modules.add(new Admin(this));
modules.add(new Help(this));
}
private boolean checkForActivate(String moduleName){
if (moduleConf.containsKey(moduleName)){
logger.info("activate " + moduleName + " in " +getGUILD_ID());
return ((JSONObject) moduleConf.get(moduleName)).get(moduleName).equals("true");
} else {
logger.info("init entry for " + moduleName);
moduleConf.put(moduleName, new JSONObject());
((JSONObject)moduleConf.get(moduleName)).put(moduleName,"false");
editGuildConfig();
return false;
}
}
public JSONObject getModuleConfig(){
return moduleConf;
}
protected void editModuleConfig(String module, JSONObject newModuleConfig){
moduleConf.replace(module, newModuleConfig);
editGuildConfig();
}
protected void editGuildConfig(){
JSONObject newGuild = new JSONObject();
newGuild.put("GUILD_ID", GUILD_ID);
JSONObject newGuildConfig = new JSONObject();
newGuildConfig.put("PREFIX", PREFIX.substring(2, PREFIX.length()-2));
newGuildConfig.put("MODROLE", MOD_ROLE);
newGuild.put("config", newGuildConfig);
newGuild.put("moduleConfig", moduleConf);
editGuildConfig(newGuild);
}
protected void editGuildConfig(JSONObject config){
controller.editGuildConfig(GUILD_ID, config);
}
protected void removeModule(String command){
for (Module m : modules){
if (m.getCommand().equals(command)){
modules.remove(m);
break;
}
}
}
protected void editPrefix(String prefix){
PREFIX = prefix;
this.editGuildConfig();
}
protected void editModRole(Long modrole){
MOD_ROLE = modrole;
editGuildConfig();
}
public JDA getJda() {
return jda;
}
protected List<Module> getModuleList(){
return modules;
}
}