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

177 lines
5.7 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.
* @author Kay
* @implNote This class is where all server info is stored per-server, so be liberal with additions.
2022-03-24 04:28:51 +00:00
*/
2022-03-24 04:10:50 +00:00
public class Server {
// Guild ID
2022-03-24 04:10:50 +00:00
private String id;
// The channel for welcome 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-30 20:58:01 +00:00
//Moderation role, used for /mod
private String modChannel;
//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.welcomeChannel = null;
this.modChannel = null;
2022-03-26 05:04:24 +00:00
this.joinRole = null;
this.modified = false;
2022-03-26 05:04:24 +00:00
}
/**
* Database Constructor
* @param id - id of the server
* @param welcomeChannel - channel for welcome messages, if enabled
*/
public Server(String id, @Nullable String welcomeChannel, @Nullable String joinRole, String modChannel) {
2022-03-26 05:04:24 +00:00
this.id = id;
this.welcomeChannel = welcomeChannel;
this.modChannel = modChannel;
2022-03-26 05:04:24 +00:00
this.joinRole = joinRole;
this.modified = false;
}
public String getId() { return id; }
public String getJoinRole() { return joinRole; }
public String getModChannel() { return modChannel; }
public String getWelcomeChannel() {
return welcomeChannel;
}
public void setJoinRole(String joinRole) {
this.modified = true;
this.joinRole = joinRole;
}
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 """
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""";
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) {
return switch (string.toLowerCase()) {
case "joinrole" -> joinRole;
case "welcomechannel" -> welcomeChannel;
case "modchannel" -> modChannel;
default -> "INVALID";
};
2022-03-28 15:56:45 +00:00
}
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 "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 "modchannel":
modChannel = null;
return "Mod channel 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) {
modified = true; // If this is being used set it to modified.
2022-03-28 15:56:45 +00:00
switch (name.toLowerCase()) {
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 "modchannel":
2022-03-30 20:58:01 +00:00
try {
if(Kiafumi.JDA.getRoleById(value) == null) {
return "That role ID is invalid.";
} else {
modChannel = value;
return "Successfully set modChannel ID to " + modChannel;
2022-03-30 20:58:01 +00:00
}
} 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 + ",welcomeChannel="+welcomeChannel+
",joinrole=" + joinRole + ",modChannel=" + modChannel +"]";
2022-03-26 05:04:24 +00:00
}
2022-03-24 04:10:50 +00:00
}