MissingIdentifier/src/main/java/Modules/Quote.java

188 lines
6.6 KiB
Java

package Modules;
import Controll.GuildController;
import Controll.SuperModule;
import IO.JSON;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
public class Quote extends SuperModule{
private final Logger logger = LoggerFactory.getLogger(Quote.class);
public static final String command = "quote";
static final String QUOTE_LOCATION = "Quotes.json";
public Quote(GuildController guildController){
super(command, guildController);
if(JSON.loadJson(QUOTE_LOCATION) == null){
JSON.saveJson(new JSONArray(), QUOTE_LOCATION);
}
}
@Override
public String getCommand() {
return command;
}
@Override
public String getDescription() {
return "`quote` shows random quote\n`quote id #NUMBER` show specific quote\n`quote new QUOTE - AUTHOR` adds new quote to the system";
}
@Override
public void execute(GenericEvent event) {
if (event instanceof GuildMessageReceivedEvent) {
GuildMessageReceivedEvent thisEvent = (GuildMessageReceivedEvent) event;
if (!isCommand(thisEvent.getMessage()))
return;
logger.debug(command + "triggered");
///////////////////
// ADD NEW QUOTE
///////////////////
if (thisEvent.getMessage().getContentRaw().matches("(?is:" + getGuildController().getPREFIX() + command + " (new|add) .*)")) {
logger.debug("safe new Quote");
// Prep new Quote Object
JSONObject newQuote = new JSONObject();
newQuote.put("text", thisEvent.getMessage().getContentRaw()
.replaceAll("(?is:" + getGuildController().getPREFIX() + command + " (new|add) )", "")
.replaceAll("(?is:-.*$)",""));
newQuote.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
if (thisEvent.getMessage().getContentRaw().contains("-"))
newQuote.put("author", thisEvent.getMessage().getContentRaw().replaceAll("(?is:[^-]*- )", ""));
JSONArray arr = JSON.loadJson(QUOTE_LOCATION);
boolean exists = false;
// go through all Guilds
for (Object o: arr){
//Filter for the right guild
JSONObject guilds = (JSONObject) o;
if (guilds.get("guildID").equals(getGuildController().getGUILD_ID())){
arr.remove(o); // Remove old object
JSONArray guildQuotes = (JSONArray) guilds.get("quotes"); // get old Quotes
guildQuotes.add(newQuote); // add new Quote
JSONObject editedGuild = new JSONObject(); // safe guild
editedGuild.put("guildID", getGuildController().getGUILD_ID());
editedGuild.put("quotes", guildQuotes);
arr.add(editedGuild);
exists = true; // set flag
break;
}
}
if (!exists){ // if flag not set create new Guild
JSONObject newGuild = new JSONObject();
newGuild.put("guildID", getGuildController().getGUILD_ID());
JSONArray newQuotes = new JSONArray();
newQuotes.add(newQuote);
newGuild.put("quotes", newQuotes);
arr.add(newGuild);
}
// Safe data
JSON.saveJson(arr, QUOTE_LOCATION);
thisEvent.getMessage().delete().queue();
///////////////////
// PICK QUOTE BY ID
///////////////////
} else if (thisEvent.getMessage().getContentRaw().matches("(?is:" + getGuildController().getPREFIX() + command + " id #[0-9]+.*)")){
logger.debug("pick Quote by ID");
//STUB
///////////////////
// RandomPick
///////////////////
} else {
logger.debug("random quote pick");
JSONArray arr = JSON.loadJson(QUOTE_LOCATION);
for (Object o : arr){
JSONObject guild = (JSONObject) o;
if (guild.get("guildID").equals(getGuildController().getGUILD_ID())){
JSONArray quotes = (JSONArray) guild.get("quotes");
JSONObject quote = null;
int i = 0;
// Get Random Quote Object
do {
quote = (JSONObject) quotes.get(new Random().nextInt(quotes.size()));
i++;
if (i > quotes.size())
return;
} while (quote == null);
// Build message
EmbedBuilder eb = new EmbedBuilder();
// get text
eb.setDescription(quote.get("text").toString());
// get Date
try {
eb.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(quote.get("date").toString()).toInstant());
} catch (ParseException e){
logger.error(e.toString());
}
//get Author if exists
if (quote.containsKey("author")){
eb.setAuthor(quote.get("author").toString());
}
thisEvent.getChannel().sendMessage(eb.build()).queue();
break;
}
}
}
//Delete command
thisEvent.getMessage().delete().queue();
}
}
@Override
public String showSettings() {
JSONArray arr = JSON.loadJson(QUOTE_LOCATION);
for (Object o : arr) {
JSONObject guild = (JSONObject) o;
if (guild.get("guildID").equals(getGuildController().getGUILD_ID())) {
return "NUMBER OF QUOTES: `" + ((JSONArray) guild.get("quotes")).size() + "`";
}
}
return "NUMBER OF QUOTES: `0`";
}
}