package moe.oko.Kiafumi.model; import moe.oko.Kiafumi.Kiafumi; import javax.annotation.Nullable; /** * Server Class * Used for in-memory data storage. Loaded from Database later. */ public class Server { //Guild ID private String id; //Whether the welcome feature is enabled (Join/Leave Logs) private boolean welcomeEnabled; //If enabled, the channel for logs to be posted to. private String welcomeChannel; //The role to be assigned on join, if null ignored. private String joinRole; //TODO Whether the server has protections enabled private boolean serverProtected; //If the server has been modified in memory, for saving persistently. private boolean modified; /** * Default constructor, used for new servers * @param id - the guild id to have server constructed for. */ public Server(String id) { this.id = id; this.welcomeEnabled = false; this.welcomeChannel = null; this.joinRole = null; this.serverProtected = false; this.modified = false; } /** * Database Constructor * @param id - id of the server * @param welcomeEnabled - is welcome enabled for the server * @param welcomeChannel - channel for welcome messages, if enabled * @param serverProtected - is the server under protection rules. (/archive, /stop, etc.) */ public Server(String id, boolean welcomeEnabled, @Nullable String welcomeChannel, @Nullable String joinRole, boolean serverProtected) { this.id = id; this.welcomeEnabled = welcomeEnabled; this.welcomeChannel = welcomeChannel; this.joinRole = joinRole; this.serverProtected = serverProtected; this.modified = false; } /** * @return - the guild id the server is assigned to */ public String getId() { return id; } public String getJoinRole() { return joinRole; } public boolean isWelcomeEnabled() { return welcomeEnabled; } public String getWelcomeChannel() { return welcomeChannel; } public boolean isServerProtected() { return serverProtected; } public void setJoinRole(String joinRole) { this.modified = true; this.joinRole = joinRole; } public void setWelcomeEnabled(boolean welcomeEnabled) { this.modified = true; this.welcomeEnabled = welcomeEnabled; } public void setWelcomeChannel(String welcomeChannel) { this.modified = true; this.welcomeChannel = welcomeChannel; } /** * Checks the modification of the server file in memory * @return - whether the server settings have been modified */ public boolean isModified() { return modified; } public String getOpts() { return "welcomeEnabled - whether or not join/leave logs are enabled" + "\nwelcomeChannel - the channel to send welcome messages to" + "\njoinRole - the role to apply to new members who join this guild" + "\nserverProtected - Determines whether or not server protections are enabled."; } public String getOptionByString(String string) { switch(string.toLowerCase()) { case "welcomeenabled": if(!welcomeEnabled) { return "False"; } else { return "True"; } case "joinrole": return joinRole; case "welcomechannel": return welcomeChannel; case "serverprotected": if(!serverProtected) { return "False"; } else { return "True"; } default: return "INVALID"; } } public String resetOptionByString(String name) { switch(name.toLowerCase()) { case "welcomeenabled": welcomeEnabled = false; return "Reset Join/Leave logs to default."; case "joinrole": joinRole = null; return "Auto-role on join is now set to disabled (Default)."; case "welcomechannel": welcomeChannel = null; return "Welcome channel is now unset."; case "serverprotected": serverProtected = true; return "Server protection set to default."; default: return "INVALID SETTING"; } } public String setOptionByString(String name, String value) { modified = true; //If this is being used just tell it it's been modified. switch (name.toLowerCase()) { case "welcomeenabled": if(value.equalsIgnoreCase("false")) { welcomeEnabled = false; return "Join/Leave logs are now disabled."; } else if(value.equalsIgnoreCase("true")) { welcomeEnabled = true; return "Join/Leave logs are now enabled. Make sure you set the `welcomeChannel` setting."; } else { return "Bad Value, use either true or false"; } case "joinrole": try { if (Kiafumi.JDA.getRoleById(value) == null) { return "That role ID is invalid."; } else { joinRole = value; return "Successfully set joinRole ID to " + value; } } catch (Exception ex) { return "Bad Value"; } case "welcomechannel": try { if (Kiafumi.JDA.getTextChannelById(value) == null) { return "That channel ID is invalid."; } else { welcomeChannel = value; return "Successfully set welcomeChannel ID to " + value; } } catch (Exception ex) { return "Bad Value"; } case "serverprotected": if(value.equalsIgnoreCase("false")) { serverProtected = false; return "Server protection is now disabled."; } else if(value.equalsIgnoreCase("true")) { serverProtected = true; return "Server protection is now enabled."; } else { return "Bad Value, use either true or false"; } default: return "INVALID setting name."; } } @Override public String toString() { return "Server{id=" + this.id + ",welcomeEnabled=" + welcomeEnabled + ",welcomeChannel="+welcomeChannel+ ",joinrole=" + joinRole +",serverProtected="+serverProtected+"}"; } }