diff --git a/src/commands/CommandChangeIndicator.java b/src/commands/CommandChangeIndicator.java index babdc4d..287ce3e 100644 --- a/src/commands/CommandChangeIndicator.java +++ b/src/commands/CommandChangeIndicator.java @@ -21,7 +21,14 @@ public class CommandChangeIndicator extends Command return; } - guild.SetCommandIndicator(arg.substring(0, 1)); + String indicator = arg.substring(0, 1); + if(indicator.charAt(0) == '\n' || indicator.charAt(0) == '\t' || indicator.charAt(0) == '\r') + { + res.Call(LocStrings.Lookup("ChangeIndicatorError")); + return; + } + + guild.SetCommandIndicator(indicator); res.Call(String.format(LocStrings.Stub("ChangeIndicatorChanged"), guild.GetCommandIndicator())); } } diff --git a/src/commands/CommandGuildRoleAdd.java b/src/commands/CommandGuildRoleAdd.java index a459094..f99ea33 100644 --- a/src/commands/CommandGuildRoleAdd.java +++ b/src/commands/CommandGuildRoleAdd.java @@ -21,7 +21,7 @@ public class CommandGuildRoleAdd extends Command public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) { String role = input.args.split(" ")[0]; - if(guild.allowedRole.contains(role)) + if(guild.roleList.contains(role)) { if(guild.control.addRole(user.discordID, role)) { diff --git a/src/commands/CommandGuildRoleAllowed.java b/src/commands/CommandGuildRoleAllowed.java index 3fbacb5..6e30d59 100644 --- a/src/commands/CommandGuildRoleAllowed.java +++ b/src/commands/CommandGuildRoleAllowed.java @@ -25,13 +25,13 @@ public class CommandGuildRoleAllowed extends Command for(int i = 0; i < roles.length; i++) { role = roles[i].trim(); - if(guild.allowedRole.contains(role)) + if(guild.roleList.contains(role)) { res.Call(LocStrings.Stub("GuildRoleAllowedDuplicate")); } else { - guild.allowedRole.add(role); + guild.roleList.add(role); res.Call(String.format(LocStrings.Stub("GuildRoleAllowedSuccess"), role)); } } diff --git a/src/commands/CommandGuildRoleList.java b/src/commands/CommandGuildRoleList.java index c0cfcba..b150286 100644 --- a/src/commands/CommandGuildRoleList.java +++ b/src/commands/CommandGuildRoleList.java @@ -15,16 +15,16 @@ public class CommandGuildRoleList extends Command public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) { String roles = ""; - if(guild.allowedRole.isEmpty()) + if(guild.roleList.isEmpty()) { res.Call(String.format(LocStrings.Stub("GuildRoleListEmpty"))); return; } - for(int i = 0; i < guild.allowedRole.size(); i++) + for(int i = 0; i < guild.roleList.size(); i++) { if(i > 0) roles += " and "; - roles += guild.allowedRole.get(i); + roles += guild.roleList.get(i); } res.Call(String.format(LocStrings.Stub("GuildRoleListOutput"), roles)); } diff --git a/src/commands/CommandGuildRoleNotAllowed.java b/src/commands/CommandGuildRoleNotAllowed.java index 3d12593..90d244a 100644 --- a/src/commands/CommandGuildRoleNotAllowed.java +++ b/src/commands/CommandGuildRoleNotAllowed.java @@ -25,9 +25,9 @@ public class CommandGuildRoleNotAllowed extends Command for(int i = 0; i < roles.length; i++) { role = roles[i].trim(); - if(guild.allowedRole.contains(role)) + if(guild.roleList.contains(role)) { - guild.allowedRole.remove(role); + guild.roleList.remove(role); res.Call(LocStrings.Stub("GuildRoleNotAllowedSuccess")); } else diff --git a/src/commands/CommandGuildRoleRemove.java b/src/commands/CommandGuildRoleRemove.java index 5535538..0ae47da 100644 --- a/src/commands/CommandGuildRoleRemove.java +++ b/src/commands/CommandGuildRoleRemove.java @@ -21,7 +21,7 @@ public class CommandGuildRoleRemove extends Command public void OnRun(KittyGuild guild, KittyChannel channel, KittyUser user, UserInput input, Response res) { String role = input.args.split(" ")[0]; - if(guild.allowedRole.contains(role)) + if(guild.roleList.contains(role)) { if(guild.control.removeRole(user.discordID, role)) { diff --git a/src/core/benchmark/BenchmarkLog.java b/src/core/benchmark/BenchmarkLog.java index af62ac0..e9c1f98 100644 --- a/src/core/benchmark/BenchmarkLog.java +++ b/src/core/benchmark/BenchmarkLog.java @@ -2,9 +2,9 @@ package core.benchmark; import utils.GlobalLog; +//Logging shim public class BenchmarkLog -{ - // Logging +{ public static void Log(String str) { GlobalLog.Log("[Benchmark] " + str); } public static void Warn(String str) { GlobalLog.Warn("[Benchmark] " + str); } public static void Error(String str) { GlobalLog.Error(" [Benchmark] " + str); } diff --git a/src/dataStructures/KittyGuild.java b/src/dataStructures/KittyGuild.java index 92b7770..6b1c15e 100644 --- a/src/dataStructures/KittyGuild.java +++ b/src/dataStructures/KittyGuild.java @@ -7,25 +7,29 @@ import utils.AdminControl; // Context for a given guild for kittybot. Primarily designed to hold guild-specific settings. public class KittyGuild extends DatabaseTrackedObject { - public String uniqueID; + // Variables + public final String uniqueID; public KittyRating contentRating; public KittyUser guildOwner; public boolean polling; public String poll; public ArrayList hasVoted = new ArrayList(); - public ArrayList allowedRole = new ArrayList(); public ArrayList emoji = new ArrayList(); public ArrayList choices = new ArrayList(); public AdminControl control; + // Database synced info + public final KittyGuildRoleList roleList; private String commandIndicator; // Default content for a guild public KittyGuild(String uniqueID, AdminControl adminControl, ArrayList emoji) { super(uniqueID); - control = adminControl; this.uniqueID = uniqueID; + roleList = new KittyGuildRoleList(uniqueID); + + control = adminControl; this.contentRating = KittyRating.Safe; this.polling = false; this.emoji = emoji; @@ -36,6 +40,8 @@ public class KittyGuild extends DatabaseTrackedObject public KittyGuild(String commandIndicator, KittyRating contentRating, KittyUser guildOwner, String uniqueID) { super(uniqueID); + this.uniqueID = uniqueID; + roleList = new KittyGuildRoleList(uniqueID); SetCommandIndicator(commandIndicator); this.contentRating = contentRating; @@ -64,7 +70,7 @@ public class KittyGuild extends DatabaseTrackedObject poll = null; return "Poll ended!"; } - + @Override public String Serialize() { diff --git a/src/dataStructures/KittyGuildRoleList.java b/src/dataStructures/KittyGuildRoleList.java new file mode 100644 index 0000000..68f261a --- /dev/null +++ b/src/dataStructures/KittyGuildRoleList.java @@ -0,0 +1,81 @@ +package dataStructures; + +import java.util.Vector; + +import core.DatabaseTrackedObject; + +// Acts as a proxy of sorts for the allowedRole arraylist, keeping it tracked for a specific guild. +public class KittyGuildRoleList extends DatabaseTrackedObject +{ + // Variables + private Vector allowedRole = new Vector(); + private final static String delimiter = "\n"; + private final static String split = "\\n"; + private final static String differentiator = "roles"; + + // Constructor - provide it with the guild ID + public KittyGuildRoleList(String identifier) + { + super(identifier + differentiator); + } + + // Mirrored behavior + public boolean contains(String role) + { + return allowedRole.contains(role); + } + + public void add(String role) + { + allowedRole.add(role); + this.MarkDirty(); + } + + public boolean isEmpty() + { + return allowedRole.isEmpty(); + } + + public int size() + { + return allowedRole.size(); + } + + public String get(int index) + { + return allowedRole.get(index); + } + + public void remove(String role) + { + allowedRole.remove(role); + this.MarkDirty(); + } + + // Writes out the roles as a single string that can be parsed back in later + @Override + public String Serialize() + { + String toSerialize = ""; + + for(int i = 0; i < allowedRole.size(); ++i) + { + if(i != 0) + toSerialize += delimiter; + + toSerialize += allowedRole.get(i); + } + + return toSerialize; + } + + // Reads in the roles as a delimited single string that needs parsing + @Override + public void DeSerialzie(String string) + { + String[] rolesSplit = string.split(split); + + for(int i = 0; i < rolesSplit.length; ++i) + allowedRole.add(rolesSplit[i]); + } +} \ No newline at end of file