Vaerys_Dawn

Send Temp File Code

Oct 18th, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. //Command
  2. @CommandAnnotation(
  3.         name = "GetCCData", description = "Sends a Json File with all of the Custom command's data.", usage = "[Command Name]",
  4.         type = Constants.TYPE_CC, channel = Constants.CHANNEL_BOT_COMMANDS, requiresArgs = true)
  5. public String getCCData(){
  6.     customCommands.sendCCasJSON(channel.getID(),args,guildID);
  7.     return null;
  8. }
  9.  
  10. //SendCCasJson
  11. public void sendCCasJSON(String channelID, String commandName, String guildID) {
  12.     GuildConfig guildConfig = (GuildConfig) FileHandler.readFromJson(Utility.getFilePath(guildID, Constants.FILE_GUILD_CONFIG), GuildConfig.class);
  13.     String botChannelID = guildConfig.getChannelTypeID(Constants.CHANNEL_BOT_COMMANDS);
  14.     IChannel channel = Globals.getClient().getChannelByID(channelID);
  15.     for (CCommandObject c : commands) {
  16.         if (c.getName().equalsIgnoreCase(commandName)) {
  17.             File file = FileHandler.createTempFile(c, c.getName());
  18.             if (Utility.sendFile("> Here is the Raw Data for Custom Command: **" + c.getName() + "**", channel, file).get()) {
  19.                 Utility.sendMessage("> An error occurred when attempting to get CC data.", channel);
  20.             }
  21.             return;
  22.         }
  23.     }
  24.     Utility.sendMessage(Constants.ERROR_CC_NOT_FOUND, channel);
  25.     return;
  26. }
  27.  
  28. //FileHandler.CreateTempFile
  29. /**saves data from POGO of type "object" to temp file using String "name".*/
  30. public static File createTempFile(Object object, String name){
  31.     Gson gson = new GsonBuilder().setPrettyPrinting().create();
  32.     try {
  33.         File file = File.createTempFile(name,".json");
  34.         FileWriter writer = new FileWriter(file);
  35.         gson.toJson(object,writer);
  36.         return file;
  37.     } catch (IOException e) {
  38.         e.printStackTrace();
  39.     }
  40.     return null;
  41. }
  42.  
  43. //Utility.SendFile
  44. public static RequestBuffer.RequestFuture<Boolean> sendFile(String message, IChannel channel, File file) {
  45.     return RequestBuffer.request(() -> {
  46.         try {
  47.             if (StringUtils.containsOnly(message, "\n") || (message == null) || message.equals("")) {
  48.                 if (message != null) {
  49.                     channel.sendFile(file);
  50.                 }else {
  51.                     logger.error("Error sending File to channel with id: " + channel.getID() + " on guild with id: " + channel.getGuild().getID() +
  52.                             ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: No file to send");
  53.                     return true;
  54.                 }
  55.             }else {
  56.                 if (message != null) {
  57.                     channel.sendFile(file,message);
  58.                 }else {
  59.                     sendMessage(message,channel);
  60.                     return true;
  61.                 }
  62.             }
  63.         } catch (DiscordException e) {
  64.             if (e.getMessage().contains("CloudFlare")) {
  65.                 sendMessage(message, channel);
  66.             } else {
  67.                 e.printStackTrace();
  68.                 return true;
  69.             }
  70.         } catch (IOException e) {
  71.             e.printStackTrace();
  72.         } catch (MissingPermissionsException e) {
  73.             logger.error("Error sending File to channel with id: " + channel.getID() + " on guild with id: " + channel.getGuild().getID() +
  74.                     ".\n" + Constants.PREFIX_EDT_LOGGER_INDENT + "Reason: Missing permissions.");
  75.             return true;
  76.         }
  77.         return false;
  78.     });
  79. }
Advertisement
Add Comment
Please, Sign In to add comment