Merge branch '#2-temporaryPersistantData'
This commit is contained in:
commit
9639f06214
5 changed files with 55 additions and 6 deletions
|
@ -4,5 +4,5 @@
|
||||||
<component name="FrameworkDetectionExcludesConfiguration">
|
<component name="FrameworkDetectionExcludesConfiguration">
|
||||||
<file type="web" url="file://$PROJECT_DIR$" />
|
<file type="web" url="file://$PROJECT_DIR$" />
|
||||||
</component>
|
</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>
|
</project>
|
|
@ -4,6 +4,7 @@ import IO.JSON;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.events.GenericEvent;
|
import net.dv8tion.jda.api.events.GenericEvent;
|
||||||
import net.dv8tion.jda.api.events.ReadyEvent;
|
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.GenericGuildEvent;
|
||||||
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
|
||||||
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
|
import net.dv8tion.jda.api.events.guild.GuildLeaveEvent;
|
||||||
|
@ -133,6 +134,7 @@ public class Controller extends ListenerAdapter {
|
||||||
reload(event.getJDA());
|
reload(event.getJDA());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onGuildJoin(@NotNull GuildJoinEvent event){
|
public void onGuildJoin(@NotNull GuildJoinEvent event){
|
||||||
//Check if guild exists
|
//Check if guild exists
|
||||||
for (GuildController guildController : guilds){
|
for (GuildController guildController : guilds){
|
||||||
|
@ -156,6 +158,7 @@ public class Controller extends ListenerAdapter {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void onGuildLeave(@NotNull GuildLeaveEvent event){
|
public void onGuildLeave(@NotNull GuildLeaveEvent event){
|
||||||
|
|
||||||
JSONArray guilds = JSON.loadJson(GUILD_CONFIG_FILE);
|
JSONArray guilds = JSON.loadJson(GUILD_CONFIG_FILE);
|
||||||
|
@ -170,4 +173,12 @@ public class Controller extends ListenerAdapter {
|
||||||
}
|
}
|
||||||
JSON.saveJson(guilds, GUILD_CONFIG_FILE);
|
JSON.saveJson(guilds, GUILD_CONFIG_FILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onShutdown(@NotNull ShutdownEvent shutdownEvent){
|
||||||
|
for (GuildController guildController: guilds){
|
||||||
|
guildController.shutdown();
|
||||||
|
}
|
||||||
|
logger.debug("Shutdown complete");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,6 +141,14 @@ public class GuildController {
|
||||||
modules.add(new Help(this));
|
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){
|
private boolean checkForActivate(String moduleName){
|
||||||
if (moduleConf.containsKey(moduleName)){
|
if (moduleConf.containsKey(moduleName)){
|
||||||
logger.info("activate " + moduleName + " in " +getGUILD_ID());
|
logger.info("activate " + moduleName + " in " +getGUILD_ID());
|
||||||
|
|
|
@ -5,6 +5,9 @@ import net.dv8tion.jda.api.entities.Member;
|
||||||
import net.dv8tion.jda.api.entities.Message;
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
import org.json.simple.JSONObject;
|
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.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
@ -13,6 +16,10 @@ public abstract class SuperModule implements Module {
|
||||||
private GuildController guildController;
|
private GuildController guildController;
|
||||||
private JSONObject config;
|
private JSONObject config;
|
||||||
private final String command;
|
private final String command;
|
||||||
|
/**
|
||||||
|
* May temporary data but save worthy (active timer etc)
|
||||||
|
*/
|
||||||
|
private Map<String, Object> temporaryData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construcker of a Module
|
* Construcker of a Module
|
||||||
|
@ -23,12 +30,24 @@ public abstract class SuperModule implements Module {
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.guildController = guildController;
|
this.guildController = guildController;
|
||||||
this.config = config;
|
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){
|
public SuperModule(String command, GuildController guildController){
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.guildController = guildController;
|
this.guildController = guildController;
|
||||||
this.config = null;
|
this.config = null;
|
||||||
|
|
||||||
|
temporaryData = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -118,4 +137,16 @@ public abstract class SuperModule implements Module {
|
||||||
protected void safeConfig(){
|
protected void safeConfig(){
|
||||||
safeConfig(getConfig());
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,6 @@ public class VoiceLobby extends SuperModule {
|
||||||
|
|
||||||
private long groupCategory;
|
private long groupCategory;
|
||||||
private long launchpadChannel;
|
private long launchpadChannel;
|
||||||
private HashSet<Long> tmpChannel = new HashSet<>();
|
|
||||||
|
|
||||||
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
private Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
|
@ -98,7 +97,7 @@ public class VoiceLobby extends SuperModule {
|
||||||
|
|
||||||
// move User
|
// move User
|
||||||
event.getGuild().moveVoiceMember(((GuildMessageReceivedEvent) event).getMember(), newChannel).queue();
|
event.getGuild().moveVoiceMember(((GuildMessageReceivedEvent) event).getMember(), newChannel).queue();
|
||||||
tmpChannel.add(newChannel.getIdLong());
|
((HashSet<Long>)getTemporaryData().get("tmpChannel")).add(newChannel.getIdLong());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
thisEvent.getChannel().sendMessage("Please Join a Voice Channel first!").queue(e -> e.delete().queueAfter(1, TimeUnit.MINUTES));
|
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
|
//check if user left a temp channel
|
||||||
if ((thisEvent.getChannelLeft() != null && thisEvent.getChannelLeft().getParent().getIdLong() == groupCategory)) {
|
if ((thisEvent.getChannelLeft() != null && thisEvent.getChannelLeft().getParent().getIdLong() == groupCategory)) {
|
||||||
//check if channel is now empty
|
//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());
|
logger.debug("may remove channel: " + thisEvent.getChannelLeft().getId());
|
||||||
//wait for 30 seconds and check again
|
//wait for 30 seconds and check again
|
||||||
|
@ -197,7 +196,7 @@ public class VoiceLobby extends SuperModule {
|
||||||
|
|
||||||
logger.debug("remove channel: " + thisEvent.getChannelLeft().getId());
|
logger.debug("remove channel: " + thisEvent.getChannelLeft().getId());
|
||||||
thisEvent.getChannelLeft().delete().queue();
|
thisEvent.getChannelLeft().delete().queue();
|
||||||
tmpChannel.remove(thisEvent.getChannelLeft().getIdLong());
|
((HashSet<Long>)getTemporaryData().get("tmpChannel")).remove(thisEvent.getChannelLeft().getIdLong());
|
||||||
} else {
|
} else {
|
||||||
logger.debug("dont remove channel: " + thisEvent.getChannelLeft().getId());
|
logger.debug("dont remove channel: " + thisEvent.getChannelLeft().getId());
|
||||||
}
|
}
|
||||||
|
@ -215,7 +214,7 @@ public class VoiceLobby extends SuperModule {
|
||||||
|
|
||||||
// move User
|
// move User
|
||||||
event.getGuild().moveVoiceMember(thisEvent.getEntity(), newChannel).queue();
|
event.getGuild().moveVoiceMember(thisEvent.getEntity(), newChannel).queue();
|
||||||
tmpChannel.add(newChannel.getIdLong());
|
((HashSet<Long>)getTemporaryData().get("tmpChannel")).add(newChannel.getIdLong());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue