Merge branch '#2-temporaryPersistantData'

This commit is contained in:
Hiajen Hiajen 2021-04-05 13:49:25 +02:00
commit 9639f06214
5 changed files with 55 additions and 6 deletions

View file

@ -4,5 +4,5 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="adopt-openj9-1.8" project-jdk-type="JavaSDK" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="adopt-openj9-1.8" project-jdk-type="JavaSDK" />
</project>

View file

@ -4,6 +4,7 @@ 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.ShutdownEvent;
import net.dv8tion.jda.api.events.guild.GenericGuildEvent;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
@ -133,6 +134,7 @@ public class Controller extends ListenerAdapter {
reload(event.getJDA());
}
@Override
public void onGuildJoin(@NotNull GuildJoinEvent event){
//Check if guild exists
for (GuildController guildController : guilds){
@ -156,6 +158,7 @@ public class Controller extends ListenerAdapter {
}
@Override
public void onGuildLeave(@NotNull GuildLeaveEvent event){
JSONArray guilds = JSON.loadJson(GUILD_CONFIG_FILE);
@ -170,4 +173,12 @@ public class Controller extends ListenerAdapter {
}
JSON.saveJson(guilds, GUILD_CONFIG_FILE);
}
@Override
public void onShutdown(@NotNull ShutdownEvent shutdownEvent){
for (GuildController guildController: guilds){
guildController.shutdown();
}
logger.debug("Shutdown complete");
}
}

View file

@ -141,6 +141,14 @@ public class GuildController {
modules.add(new Help(this));
}
protected void shutdown(){
for (Module module: modules){
if (module instanceof SuperModule)
((SuperModule) module).shutdown();
}
logger.debug("Shutdown Modules for Guild: " + getGUILD_ID());
}
private boolean checkForActivate(String moduleName){
if (moduleConf.containsKey(moduleName)){
logger.info("activate " + moduleName + " in " +getGUILD_ID());

View file

@ -5,6 +5,9 @@ import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import org.json.simple.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -13,6 +16,10 @@ public abstract class SuperModule implements Module {
private GuildController guildController;
private JSONObject config;
private final String command;
/**
* May temporary data but save worthy (active timer etc)
*/
private Map<String, Object> temporaryData;
/**
* Construcker of a Module
@ -23,12 +30,24 @@ public abstract class SuperModule implements Module {
this.command = command;
this.guildController = guildController;
this.config = config;
temporaryData = (Map<String, Object>)config.get("TemporaryData");
if (temporaryData != null) {
this.config.remove("TemporaryData");
safeConfig();
} else {
temporaryData = new HashMap<>();
}
}
public SuperModule(String command, GuildController guildController){
this.command = command;
this.guildController = guildController;
this.config = null;
temporaryData = new HashMap<>();
}
/**
@ -118,4 +137,16 @@ public abstract class SuperModule implements Module {
protected void safeConfig(){
safeConfig(getConfig());
}
/**
* On shutdown safe temporary data what should be saved
*/
protected void shutdown(){
getConfig().put("TemporaryData", temporaryData);
safeConfig();
}
protected Map<String, Object> getTemporaryData(){
return temporaryData;
}
}

View file

@ -31,7 +31,6 @@ public class VoiceLobby extends SuperModule {
private long groupCategory;
private long launchpadChannel;
private HashSet<Long> tmpChannel = new HashSet<>();
private Logger logger = LoggerFactory.getLogger(this.getClass());
@ -98,7 +97,7 @@ public class VoiceLobby extends SuperModule {
// move User
event.getGuild().moveVoiceMember(((GuildMessageReceivedEvent) event).getMember(), newChannel).queue();
tmpChannel.add(newChannel.getIdLong());
((HashSet<Long>)getTemporaryData().get("tmpChannel")).add(newChannel.getIdLong());
} else {
thisEvent.getChannel().sendMessage("Please Join a Voice Channel first!").queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES));
@ -185,7 +184,7 @@ public class VoiceLobby extends SuperModule {
//check if user left a temp channel
if ((thisEvent.getChannelLeft() != null && thisEvent.getChannelLeft().getParent().getIdLong() == groupCategory)) {
//check if channel is now empty
if (thisEvent.getChannelLeft().getMembers().size() < 1 && tmpChannel.contains(thisEvent.getChannelLeft().getIdLong())) {
if (thisEvent.getChannelLeft().getMembers().size() < 1 && ((HashSet<Long>)getTemporaryData().get("tmpChannel")).contains(thisEvent.getChannelLeft().getIdLong())) {
logger.debug("may remove channel: " + thisEvent.getChannelLeft().getId());
//wait for 30 seconds and check again
@ -197,7 +196,7 @@ public class VoiceLobby extends SuperModule {
logger.debug("remove channel: " + thisEvent.getChannelLeft().getId());
thisEvent.getChannelLeft().delete().queue();
tmpChannel.remove(thisEvent.getChannelLeft().getIdLong());
((HashSet<Long>)getTemporaryData().get("tmpChannel")).remove(thisEvent.getChannelLeft().getIdLong());
} else {
logger.debug("dont remove channel: " + thisEvent.getChannelLeft().getId());
}
@ -215,7 +214,7 @@ public class VoiceLobby extends SuperModule {
// move User
event.getGuild().moveVoiceMember(thisEvent.getEntity(), newChannel).queue();
tmpChannel.add(newChannel.getIdLong());
((HashSet<Long>)getTemporaryData().get("tmpChannel")).add(newChannel.getIdLong());
}
}
}