MissingIdentifier/src/main/java/Controll/SuperModule.java

122 lines
3.9 KiB
Java

package Controll;
import Modules.Module;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import org.json.simple.JSONObject;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class SuperModule implements Module {
private GuildController guildController;
private JSONObject config;
private final String command;
/**
* Construcker of a Module
* @param guildController Guild where Module is used, each guild get its own Module
* @param config name of command (this command will be called)
*/
public SuperModule(String command, GuildController guildController, JSONObject config){
this.command = command;
this.guildController = guildController;
this.config = config;
}
public SuperModule(String command, GuildController guildController){
this.command = command;
this.guildController = guildController;
this.config = null;
}
/**
* Returns Guild where Module is aktive
* @return GuildController
*/
public GuildController getGuildController() {
return guildController;
}
/**
* Checks if Given Member is allowed to use Highlevel Commands
* @param member User to check
* @return true if user as permissions
*/
public boolean checkForAdmin(Member member){
return member.getRoles().contains(guildController.getJda().getGuildById(guildController.getGUILD_ID()).getRoleById(guildController.getMOD_ROLE())) || member.isOwner();
}
/**
* Return true if Message is a valid command speakin to the module
* @param message Incomming message
* @return true if message is command
*/
public boolean isCommand(Message message){
return message.getContentRaw().matches("(?is:^" + getGuildController().getPREFIX() + command + ".*)");
}
/**
* If you want to check if called command is followed by a subcommand use this.
* As Example "!admin help" the subcommand would be help.
* @param input Message or Text Input
* @param subCommand Command to look for
* @return true if subcommand is found
*/
public boolean checkForCommand(String input, String subCommand){
return input.matches("(?is:^" + guildController.getPREFIX() + command + " " + subCommand + ".*)");
}
/**
* If your Module dont have a specific command may use this funktion to check for commands anyways
* @param input Message or Text Input
* @param command Command to look for
* @param subCommand SubCommand to look for
* @return true if Command is found
*/
public boolean checkForCommand(String input, String command, String subCommand){
return input.matches("(?is:^" + guildController.getPREFIX() + command + "[ ]?" + subCommand + ".*)");
}
/**
* If your command Contains Parameter use this command to extract them
* "!foo -baa ipsum" would return ipsum if you look for "baa"
* @param input Message or Text Input
* @param parameter Name of parameter
* @return Value of Parameter
*/
public String getParameter(String input, String parameter){
//search for parameter with data
Matcher m = Pattern.compile("(?is: -"+ parameter + " [^-]+)").matcher(input);
if (m.find()){
return m.group(0).replaceFirst("(?is: -" + parameter + " )", "").replaceAll("( )*$", "");
}
//search for flag
m = Pattern.compile("(?is: -"+ parameter + ")").matcher(input);
if (m.find())
return "";
//return null if not found
return null;
}
protected JSONObject getConfig() {
return config;
}
protected void setConfig(JSONObject config) {
this.config = config;
}
protected void safeConfig(JSONObject newConfig){
guildController.editModuleConfig(command, newConfig);
}
protected void safeConfig(){
safeConfig(getConfig());
}
}