kiafumi/src/main/java/moe/oko/Kiafumi/model/Server.java

241 lines
8.3 KiB
Java

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.
* @author Kay
* @implNote This class is where all server info is stored per-server, so be liberal with additions.
*/
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 (do protections)
private boolean serverProtected;
//Moderation role, used for /mod
private String modRole;
//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.modRole = null;
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, String modRole) {
this.id = id;
this.welcomeEnabled = welcomeEnabled;
this.welcomeChannel = welcomeChannel;
this.joinRole = joinRole;
this.serverProtected = serverProtected;
this.modRole = modRole;
this.modified = false;
}
public String getId() { return id; }
public String getJoinRole() { return joinRole; }
public String getModRole() { return modRole; }
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;
}
/**
* Options in the server class that can be modified.
* @return - Options in a string that can be printed to discord.
*/
public String getOpts() {
return """
welcomeChannel - the channel to send welcome messages to
modChannel - the channel to send moderation logs to
joinRole - the role to apply to new members who join this guild
""";
}
/**
* Fetches the option value by string for discord command.
* @param string - the string to assess
* @return - the value (if applicable) to return
*/
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";
}
case "modrole":
return modRole;
default:
return "INVALID";
}
}
/**
* Resets an option based on the string provided
* @param name - name of the option to be reset
* @return - returns whether the function succeeded.
*/
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.";
case "modrole":
modRole = null;
return "Mod role is now unset.";
default:
return "INVALID SETTING";
}
}
/**
* Sets an option by a string, if it can find one
* @param name - the name of the option
* @param value - the value to have the option set to
* @return - whether the name and value were valid and the option was set.
*/
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";
}
case "modrole":
try {
if(Kiafumi.JDA.getRoleById(value) == null) {
return "That role ID is invalid.";
} else {
modRole = value;
return "Successfully set modRole ID to " + modRole;
}
} catch (Exception ex) {
return "Bad Value";
}
default:
return "INVALID setting name.";
}
}
@Override
public String toString() {
return "Server{id=" + this.id + ",welcomeEnabled=" + welcomeEnabled + ",welcomeChannel="+welcomeChannel+
",joinrole=" + joinRole +",serverProtected="+serverProtected+ ",modRole=" + modRole +"}";
}
}