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

239 lines
8.4 KiB
Java
Raw Normal View History

2022-03-24 04:10:50 +00:00
package moe.oko.Kiafumi.model;
2022-03-28 15:56:45 +00:00
import moe.oko.Kiafumi.Kiafumi;
2022-03-26 05:04:24 +00:00
import javax.annotation.Nullable;
2022-03-24 04:28:51 +00:00
/**
* Server Class
* Used for in-memory data storage. Loaded from Database later.
*/
2022-03-24 04:10:50 +00:00
public class Server {
//Guild ID
2022-03-24 04:10:50 +00:00
private String id;
//Whether the welcome feature is enabled (Join/Leave Logs)
2022-03-24 04:10:50 +00:00
private boolean welcomeEnabled;
//If enabled, the channel for logs to be posted to.
2022-03-24 04:10:50 +00:00
private String welcomeChannel;
//The role to be assigned on join, if null ignored.
2022-03-26 05:04:24 +00:00
private String joinRole;
2022-03-28 20:43:04 +00:00
//TODO Whether the server has protections enabled (do protections)
2022-03-26 05:04:24 +00:00
private boolean serverProtected;
2022-03-30 20:58:01 +00:00
//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.
*/
2022-03-26 05:04:24 +00:00
public Server(String id) {
this.id = id;
this.welcomeEnabled = false;
this.welcomeChannel = null;
this.joinRole = null;
this.serverProtected = false;
2022-03-30 20:58:01 +00:00
this.modRole = null;
this.modified = false;
2022-03-26 05:04:24 +00:00
}
/**
* 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.)
*/
2022-03-30 20:58:01 +00:00
public Server(String id, boolean welcomeEnabled, @Nullable String welcomeChannel, @Nullable String joinRole, boolean serverProtected, String modRole) {
2022-03-26 05:04:24 +00:00
this.id = id;
this.welcomeEnabled = welcomeEnabled;
this.welcomeChannel = welcomeChannel;
this.joinRole = joinRole;
this.serverProtected = serverProtected;
2022-03-30 20:58:01 +00:00
this.modRole = modRole;
this.modified = false;
}
public String getId() { return id; }
public String getJoinRole() { return joinRole; }
2022-03-30 20:58:01 +00:00
public String getModRole() { return modRole; }
public boolean isWelcomeEnabled() {
return welcomeEnabled;
}
public String getWelcomeChannel() {
return welcomeChannel;
}
2022-03-28 19:38:25 +00:00
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;
2022-03-26 05:04:24 +00:00
}
2022-03-28 20:43:04 +00:00
/**
* Options in the server class that can be modified.
* @return - Options in a string that can be printed to discord.
*/
2022-03-28 15:56:45 +00:00
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" +
2022-03-30 20:58:01 +00:00
"\nserverProtected - Determines whether or not server protections are enabled." +
"\nmodRole - The moderator role for the /mod command (only works if protection enabled)";
2022-03-28 15:56:45 +00:00
}
2022-03-28 20:43:04 +00:00
/**
* Fetches the option value by string for discord command.
* @param string - the string to assess
* @return - the value (if applicable) to return
*/
2022-03-28 15:56:45 +00:00
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";
}
2022-03-30 20:58:01 +00:00
case "modrole":
return modRole;
2022-03-28 15:56:45 +00:00
default:
return "INVALID";
}
}
2022-03-28 20:43:04 +00:00
/**
* Resets an option based on the string provided
* @param name - name of the option to be reset
* @return - returns whether the function succeeded.
*/
2022-03-28 15:56:45 +00:00
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.";
2022-03-30 20:58:01 +00:00
case "modrole":
modRole = null;
return "Mod role is now unset.";
2022-03-28 15:56:45 +00:00
default:
return "INVALID SETTING";
}
}
2022-03-28 20:43:04 +00:00
/**
* 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.
*/
2022-03-28 15:56:45 +00:00
public String setOptionByString(String name, String value) {
2022-03-28 19:38:25 +00:00
modified = true; //If this is being used just tell it it's been modified.
2022-03-28 15:56:45 +00:00
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":
2022-03-28 19:38:25 +00:00
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";
2022-03-28 15:56:45 +00:00
}
case "welcomechannel":
2022-03-28 19:38:25 +00:00
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";
2022-03-28 15:56:45 +00:00
}
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";
}
2022-03-30 20:58:01 +00:00
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";
}
2022-03-28 15:56:45 +00:00
default:
return "INVALID setting name.";
}
}
2022-03-26 05:04:24 +00:00
@Override
public String toString() {
return "Server{id=" + this.id + ",welcomeEnabled=" + welcomeEnabled + ",welcomeChannel="+welcomeChannel+
2022-03-30 20:58:01 +00:00
",joinrole=" + joinRole +",serverProtected="+serverProtected+ ",modRole=" + modRole +"}";
2022-03-26 05:04:24 +00:00
}
2022-03-24 04:10:50 +00:00
}