Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package command;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import net.dv8tion.jda.core.Permission;
- import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
- import pw.naltan.pherrous.core.objects.AssetKey;
- import pw.naltan.pherrous.engine.builders.ManualBuilder;
- import pw.naltan.pherrous.interfaces.Assets;
- import pw.naltan.pherrous.interfaces.GuildCommand;
- import pw.naltan.pherrous.interfaces.GuildMessage;
- import pw.naltan.pherrous.interfaces.Indexable;
- import pw.naltan.pherrous.interfaces.Manual;
- import pw.naltan.pherrous.io.Input;
- import pw.naltan.pherrous.io.Output;
- public class Lizzy implements GuildCommand, Indexable, Manual, Assets, GuildMessage {
- AssetKey key;
- List<String> responses;
- int chance;
- @Override
- public ManualBuilder man(GuildMessageReceivedEvent e, ManualBuilder man) {
- return man.setAuthor("Nathan")
- .setVersion("2.0")
- .addArg("freq <number>", "Set the percent frequency of Lizzy's responses.")
- .addArg("add <response>", "Add a response.")
- .addArg("remove <response>", "Remove a response.")
- .addArg("list", "Get a list of responses.")
- .addArg("invoke", "Invoke a response.")
- ;
- }
- @Override
- public String category() {
- return "Fun";
- }
- @Override
- public String name() {
- return "Lizzy";
- }
- @SuppressWarnings("unchecked")
- @Override
- public void cmd(GuildMessageReceivedEvent e) {
- String opt = "";
- try {
- opt = e.getMessage().getContentRaw().toLowerCase().split(" ")[1];
- } catch (Exception ex) {
- opt = "";
- }
- switch(opt) {
- case "add":
- add(e);
- return;
- case "remove":
- remove(e);
- return;
- case "invoke":
- invoke(e);
- return;
- case "freq":
- freq(e);
- return;
- case "list":
- String out = "Here's my repsonses\nMy chance to comment is " + chance + "%n";
- int remaining = responses.size() - 1;
- while(out.length() < 2000) {
- remaining--;
- out += "\n" + responses.get(remaining);
- }
- Output.outAsText(e.getChannel(), out);
- }
- }
- private void add(GuildMessageReceivedEvent e) {
- String add = "";
- try {
- add = e.getMessage().getContentRaw().split(" ")[2];
- } catch(Exception ex) {
- Output.outAsText(e.getChannel(), "Enter a response");
- }
- responses.add(add);
- updateJSON();
- Output.outAsText(e.getChannel(), "Successfully added " + add + ".");
- }
- private void remove(GuildMessageReceivedEvent e) {
- String del = "";
- try {
- del = e.getMessage().getContentRaw().split(" ")[2];
- } catch(Exception ex) {
- Output.outAsText(e.getChannel(), "Enter a response");
- }
- boolean success = responses.remove(del);
- String out = "That response doesn't exist";
- if(success) {
- out = "Removal successful!";
- }
- Output.outAsText(e.getChannel(), out);
- }
- private void invoke(GuildMessageReceivedEvent e) {
- Output.outAsText(e.getChannel(), responses.get(new Random().nextInt(responses.size() - 1)));
- }
- private void freq(GuildMessageReceivedEvent e) {
- int freq = 0;
- try {
- freq = Integer.parseInt(e.getMessage().getContentRaw().split(" ")[2]);
- if(freq < 0 || freq > 100) {
- throw new Exception();
- }
- } catch (Exception ex) {
- Output.outAsText(e.getChannel(), "Enter a number (0 < n < 100)");
- }
- chance = freq;
- updateJSON();
- }
- @Override
- public void reqperms(List<Permission> req) {
- }
- @Override
- public String assetDir() {
- return "lizzy";
- }
- @SuppressWarnings("unchecked")
- @Override
- public void key(AssetKey key) {
- this.key = key;
- responses = new ArrayList<String>();
- JSONObject obj = key.readJSON("lizzy");
- try {
- JSONArray arr = (JSONArray) obj.get("responses");
- for(Object current : arr) {
- responses.add((String) current);
- }
- chance = (int) obj.get("chance");
- } catch (Exception ex) {
- JSONArray arr = new JSONArray();
- arr.add("You have such a way with words, Dizzy...");
- JSONObject def = new JSONObject();
- def.put("responses", arr);
- def.put("chance", 20);
- key.writeJSON("lizzy", def);
- }
- }
- @SuppressWarnings("unchecked")
- private void updateJSON() {
- JSONArray array = new JSONArray();
- for(String current : responses) {
- array.add(current);
- }
- JSONObject out = new JSONObject();
- out.put("responses", array);
- out.put("chance", chance);
- key.writeJSON("lizzy", out);
- }
- @Override
- public void msg(GuildMessageReceivedEvent e) {
- if(e.getAuthor().getId().equals("251216694503145472") && new Random().nextInt(100) <= chance) {
- Output.outAsText(e.getChannel(), responses.get(new Random().nextInt(responses.size()-1)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement