Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. package command;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import org.json.simple.JSONArray;
  8. import org.json.simple.JSONObject;
  9.  
  10. import net.dv8tion.jda.core.Permission;
  11. import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
  12. import pw.naltan.pherrous.core.objects.AssetKey;
  13. import pw.naltan.pherrous.engine.builders.ManualBuilder;
  14. import pw.naltan.pherrous.interfaces.Assets;
  15. import pw.naltan.pherrous.interfaces.GuildCommand;
  16. import pw.naltan.pherrous.interfaces.GuildMessage;
  17. import pw.naltan.pherrous.interfaces.Indexable;
  18. import pw.naltan.pherrous.interfaces.Manual;
  19. import pw.naltan.pherrous.io.Input;
  20. import pw.naltan.pherrous.io.Output;
  21.  
  22. public class Lizzy implements GuildCommand, Indexable, Manual, Assets, GuildMessage {
  23.  
  24. AssetKey key;
  25. List<String> responses;
  26. int chance;
  27.  
  28. @Override
  29. public ManualBuilder man(GuildMessageReceivedEvent e, ManualBuilder man) {
  30. return man.setAuthor("Nathan")
  31. .setVersion("2.0")
  32. .addArg("freq <number>", "Set the percent frequency of Lizzy's responses.")
  33. .addArg("add <response>", "Add a response.")
  34. .addArg("remove <response>", "Remove a response.")
  35. .addArg("list", "Get a list of responses.")
  36. .addArg("invoke", "Invoke a response.")
  37. ;
  38. }
  39.  
  40. @Override
  41. public String category() {
  42. return "Fun";
  43. }
  44.  
  45. @Override
  46. public String name() {
  47. return "Lizzy";
  48. }
  49.  
  50. @SuppressWarnings("unchecked")
  51. @Override
  52. public void cmd(GuildMessageReceivedEvent e) {
  53.  
  54. String opt = "";
  55. try {
  56. opt = e.getMessage().getContentRaw().toLowerCase().split(" ")[1];
  57. } catch (Exception ex) {
  58. opt = "";
  59. }
  60. switch(opt) {
  61. case "add":
  62. add(e);
  63. return;
  64.  
  65. case "remove":
  66. remove(e);
  67. return;
  68.  
  69. case "invoke":
  70. invoke(e);
  71. return;
  72.  
  73. case "freq":
  74. freq(e);
  75. return;
  76.  
  77. case "list":
  78. String out = "Here's my repsonses\nMy chance to comment is " + chance + "%n";
  79. int remaining = responses.size() - 1;
  80. while(out.length() < 2000) {
  81. remaining--;
  82. out += "\n" + responses.get(remaining);
  83. }
  84. Output.outAsText(e.getChannel(), out);
  85. }
  86.  
  87. }
  88.  
  89. private void add(GuildMessageReceivedEvent e) {
  90. String add = "";
  91. try {
  92. add = e.getMessage().getContentRaw().split(" ")[2];
  93. } catch(Exception ex) {
  94. Output.outAsText(e.getChannel(), "Enter a response");
  95. }
  96. responses.add(add);
  97. updateJSON();
  98. Output.outAsText(e.getChannel(), "Successfully added " + add + ".");
  99. }
  100.  
  101. private void remove(GuildMessageReceivedEvent e) {
  102. String del = "";
  103. try {
  104. del = e.getMessage().getContentRaw().split(" ")[2];
  105. } catch(Exception ex) {
  106. Output.outAsText(e.getChannel(), "Enter a response");
  107. }
  108. boolean success = responses.remove(del);
  109. String out = "That response doesn't exist";
  110. if(success) {
  111. out = "Removal successful!";
  112. }
  113. Output.outAsText(e.getChannel(), out);
  114. }
  115.  
  116. private void invoke(GuildMessageReceivedEvent e) {
  117. Output.outAsText(e.getChannel(), responses.get(new Random().nextInt(responses.size() - 1)));
  118. }
  119.  
  120. private void freq(GuildMessageReceivedEvent e) {
  121. int freq = 0;
  122. try {
  123. freq = Integer.parseInt(e.getMessage().getContentRaw().split(" ")[2]);
  124.  
  125. if(freq < 0 || freq > 100) {
  126. throw new Exception();
  127. }
  128. } catch (Exception ex) {
  129. Output.outAsText(e.getChannel(), "Enter a number (0 < n < 100)");
  130. }
  131. chance = freq;
  132. updateJSON();
  133. }
  134.  
  135. @Override
  136. public void reqperms(List<Permission> req) {
  137. }
  138.  
  139. @Override
  140. public String assetDir() {
  141. return "lizzy";
  142. }
  143.  
  144. @SuppressWarnings("unchecked")
  145. @Override
  146. public void key(AssetKey key) {
  147. this.key = key;
  148. responses = new ArrayList<String>();
  149. JSONObject obj = key.readJSON("lizzy");
  150. try {
  151. JSONArray arr = (JSONArray) obj.get("responses");
  152. for(Object current : arr) {
  153. responses.add((String) current);
  154. }
  155. chance = (int) obj.get("chance");
  156. } catch (Exception ex) {
  157. JSONArray arr = new JSONArray();
  158. arr.add("You have such a way with words, Dizzy...");
  159.  
  160. JSONObject def = new JSONObject();
  161. def.put("responses", arr);
  162. def.put("chance", 20);
  163. key.writeJSON("lizzy", def);
  164. }
  165. }
  166.  
  167. @SuppressWarnings("unchecked")
  168. private void updateJSON() {
  169. JSONArray array = new JSONArray();
  170. for(String current : responses) {
  171. array.add(current);
  172. }
  173. JSONObject out = new JSONObject();
  174. out.put("responses", array);
  175. out.put("chance", chance);
  176. key.writeJSON("lizzy", out);
  177. }
  178.  
  179. @Override
  180. public void msg(GuildMessageReceivedEvent e) {
  181. if(e.getAuthor().getId().equals("251216694503145472") && new Random().nextInt(100) <= chance) {
  182. Output.outAsText(e.getChannel(), responses.get(new Random().nextInt(responses.size()-1)));
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement