package Controll; import IO.JSON; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.events.GenericEvent; import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.guild.GenericGuildEvent; import net.dv8tion.jda.api.events.guild.GuildJoinEvent; import net.dv8tion.jda.api.events.guild.GuildLeaveEvent; import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; import net.dv8tion.jda.api.events.guild.voice.GenericGuildVoiceEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageDeleteEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionAddEvent; import net.dv8tion.jda.api.events.message.guild.react.GuildMessageReactionRemoveEvent; import net.dv8tion.jda.api.events.user.GenericUserEvent; import net.dv8tion.jda.api.events.user.UserActivityEndEvent; import net.dv8tion.jda.api.events.user.UserActivityStartEvent; import net.dv8tion.jda.api.events.user.update.UserUpdateActivityOrderEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import org.jetbrains.annotations.NotNull; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; /** * @author https://www.Hiajen.de */ public class Controller extends ListenerAdapter { //public static final String CHECKEMOJI = "\u2714"; private final Logger logger = LoggerFactory.getLogger(Controller.class); private final String GUILD_CONFIG_FILE = "GuildConfigs.json"; public static JSONObject conf; //public static MySQL mySQL; private ArrayList guilds; /** * Constructor Initializes Attributes * @param conf bot config */ public Controller(JSONObject conf){ Controller.conf = conf; logger.info("start init MySQL connection"); //mySQL = new MySQL(conf); } /** * Reloads All Guilds */ public void reload(JDA jda){ logger.info("reload Controller"); guilds = new ArrayList(); JSONArray getGuilds = JSON.loadJson(GUILD_CONFIG_FILE); for (Object o : getGuilds){ guilds.add(new GuildController((JSONObject) o, this, jda)); } } /** * Updates Guild config in Database and reloads Guild * @param guildID * @param newValue */ public void editGuildConfig(long guildID, JSONObject newValue){ JSONArray guilds = JSON.loadJson(GUILD_CONFIG_FILE); for (Object o : guilds){ JSONObject guild = (JSONObject) o; if (Long.parseLong(guild.get("GUILD_ID").toString()) == guildID){ guilds.remove(o); guilds.add(newValue); break; } } JSON.saveJson(guilds, GUILD_CONFIG_FILE); } /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // LISTENER /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @Override public void onGenericEvent(@NotNull GenericEvent event){ if (event instanceof GuildJoinEvent) onGuildJoin((GuildJoinEvent) event); else if (event instanceof GuildLeaveEvent) onGuildLeave((GuildLeaveEvent) event); else if (event instanceof GenericGuildEvent) { guilds.forEach(x -> { if (x.getGUILD_ID() == ((GenericGuildEvent) event).getGuild().getIdLong()) x.execute(event); }); } else if (event instanceof GenericUserEvent){ if (event instanceof UserActivityStartEvent){ guilds.forEach(x -> { if (x.getGUILD_ID() == ((UserActivityStartEvent)event).getGuild().getIdLong()) x.execute(event); }); } else if (event instanceof UserActivityEndEvent){ guilds.forEach(x -> { if (x.getGUILD_ID() == ((UserActivityEndEvent)event).getGuild().getIdLong()) x.execute(event); }); } else if(event instanceof UserUpdateActivityOrderEvent){ guilds.forEach(x -> { if (x.getGUILD_ID() == ((UserUpdateActivityOrderEvent)event).getGuild().getIdLong()) x.execute(event); }); } } } @Override public void onReady(ReadyEvent event){ logger.debug("JDA is ready"); reload(event.getJDA()); } public void onGuildJoin(@NotNull GuildJoinEvent event){ //Check if guild exists for (GuildController guildController : guilds){ if (guildController.getGUILD_ID() == event.getGuild().getIdLong()) return; } JSONObject newGuild = new JSONObject(); newGuild.put("GUILD_ID", event.getGuild().getIdLong()); JSONObject newGuildConfig = new JSONObject(); newGuildConfig.put("PREFIX", "!"); newGuildConfig.put("MODROLE", 123); newGuild.put("config", newGuildConfig); newGuild.put("moduleConfig", new JSONObject()); JSONArray guilds = JSON.loadJson(GUILD_CONFIG_FILE); guilds.add(newGuild); JSON.saveJson(guilds, GUILD_CONFIG_FILE); this.guilds.add(new GuildController(newGuild, this, event.getJDA())); } public void onGuildLeave(@NotNull GuildLeaveEvent event){ JSONArray guilds = JSON.loadJson(GUILD_CONFIG_FILE); for (Object o : guilds){ JSONObject guild = (JSONObject) o; if (Long.parseLong(guild.get("GUILD_ID").toString()) == event.getGuild().getIdLong()){ guilds.remove(o); break; } } JSON.saveJson(guilds, GUILD_CONFIG_FILE); } }