Vaerys_Dawn

POGOs Example

Jun 26th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1.  
  2.  
  3. //This Class handles reading and writing of POGOs
  4. public class FileHandler {
  5.  
  6.     public void createDirectory(String dirName) {
  7.         File file = new File(dirName);
  8.         if (!file.exists()) {
  9.             file.mkdirs();
  10.         }
  11.     }
  12.  
  13.     public Object readfromJson(String file, Class<?> objClass){
  14.         Gson gson = new Gson();
  15.         try (Reader reader = new FileReader(file)) {
  16.             Object newObject = gson.fromJson(reader, objClass);
  17.             return newObject;
  18.         } catch (FileNotFoundException e) {
  19.             e.printStackTrace();
  20.         } catch (IOException e) {
  21.             e.printStackTrace();
  22.         }
  23.         return null;
  24.     }
  25.  
  26.     public void writetoJson(String file, Object object){
  27.         Gson gson = new GsonBuilder().setPrettyPrinting().create();
  28.         try (FileWriter writer = new FileWriter(file)) {
  29.             gson.toJson(object, writer);
  30.         } catch (IOException e) {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34. }
  35.  
  36. //Call this in onGuildcreate event
  37.  
  38.     //init Custom Commands
  39.         file = "CommandLists/" + readableGuildID + "_CustomCommands.json";
  40.         CustomCommands customCommands = new CustomCommands();
  41.         if (!Files.exists(Paths.get(file))) {
  42.             handler.writetoJson(file, customCommands);
  43.             logger.info("Creating Custom Commands List");
  44.         }
  45.  
  46. //to get the data
  47.  
  48.     //loads the custom commands for the Guild.
  49.         file = "CommandLists/" + readableGuildID + "_CustomCommands.json";
  50.         CustomCommands customCommands = (CustomCommands) handler.readfromJson(file, CustomCommands.class);
  51.  
  52.    
  53. //Custom commands Class as an example
  54. public class CustomCommands {
  55.  
  56.     ArrayList<String[]> commands = new ArrayList<String[]>();
  57.     final String commandNotFound = "No Command with that name found.";
  58.  
  59.     public String createCommand(String userID, String commandName, String response){
  60.         boolean noDuplicate = true;
  61.         for (String[] sA : commands){
  62.             if (sA[1].equalsIgnoreCase(commandName)){
  63.                 noDuplicate = false;
  64.             }
  65.         }
  66.         if (noDuplicate) {
  67.             String[] newEntry = new String[3];
  68.             newEntry[0] = userID;
  69.             newEntry[1] = commandName;
  70.             newEntry[2] = response;
  71.             commands.add(newEntry);
  72.             return "Command Added";
  73.         }
  74.         return "A Command with that name already exists, Cannot create command.";
  75.     }
  76.  
  77.     public String getCommand(String commandName){
  78.         for (String[] cA: commands){
  79.             if (cA[1].equalsIgnoreCase(commandName)){
  80.                 return cA[2];
  81.             }
  82.         }
  83.         return commandNotFound;
  84.     }
  85.  
  86.     public String removeCommand(boolean isMod, String userID, String commandName){
  87.         for (String[] sA : commands){
  88.             if (sA[1].equalsIgnoreCase(commandName)){
  89.                 if (sA[0].equalsIgnoreCase("LockedCommand")){
  90.                     return "This command cannot be removed";
  91.                 }
  92.                 if (isMod || sA[0].equals(userID)){
  93.                     commands.remove(sA);
  94.                     return "Command Removed";
  95.                 }
  96.                 return "You Do not have Permission to Remove this command";
  97.             }
  98.         }
  99.         return "A command with that name does not exist";
  100.     }
  101.  
  102.     public String listCommands(){
  103.         StringBuilder stringBuilder = new StringBuilder();
  104.         stringBuilder.append("Here is the List of Custom Commands");
  105.         for (String[] sA : commands){
  106.             stringBuilder.append("\n  "+ sA[1]);
  107.         }
  108.         return stringBuilder.toString();
  109.     }
  110. }
  111.  
  112. //how i call the command
  113.  
  114.     if (message.toString().toLowerCase().startsWith(Globals.CCPrefix.toLowerCase())) {
  115.         StringBuilder CCName = new StringBuilder();
  116.         CCName.append(message.toString());
  117.         CCName.delete(0, Globals.CCPrefix.length());
  118.         String CCResponse = customCommands.getCommand(CCName.toString());
  119.         CCResponse = CCResponse.replaceAll("#author#", message.getAuthor().getName());
  120.         channel.sendMessage(CCResponse);
  121.     }
Advertisement
Add Comment
Please, Sign In to add comment