Vaerys_Dawn

MessageUpdateEvent

Aug 10th, 2017
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.91 KB | None | 0 0
  1.     @EventSubscriber
  2.     public void onMessageUpdateEvent(MessageUpdateEvent event) {
  3.         if (event.getNewMessage().getContent() == null || event.getOldMessage().getContent() == null) {
  4.             return;
  5.         }
  6.         if (event.getNewMessage().getContent().isEmpty() || event.getOldMessage().getContent().isEmpty()) {
  7.             return;
  8.         }
  9.         if (event.getNewMessage().getContent().equals(event.getOldMessage().getContent())) {
  10.             return;
  11.         }
  12.         if (event.getChannel().isPrivate()) {
  13.             return;
  14.         }
  15.         CommandObject command = new CommandObject(event.getMessage());
  16.         IUser ourUser = command.client.getOurUser();
  17.         List<String> logID = command.guildConfig.getChannelIDsByType(Command.CHANNEL_SERVER_LOG);
  18.         List<String> dontLog = command.guildConfig.getChannelIDsByType(Command.CHANNEL_DONT_LOG);
  19.         if (dontLog != null) {
  20.             for (String id : dontLog) {
  21.                 if (command.channelSID.equals(id)) {
  22.                     return;
  23.                 }
  24.             }
  25.         }
  26.         if (logID != null) {
  27.             String serverLogID = logID.get(0);
  28.  
  29.             if (command.guildConfig.dontLogBot && command.author.isBot()) {
  30.                 return;
  31.             }
  32.             if (event.getMessage() == null) {
  33.                 return;
  34.             }
  35.             if (serverLogID != null && serverLogID.equals(command.channelSID) && ourUser.getStringID().equals(command.authorSID)) {
  36.                 return;
  37.             }
  38.             if (command.guildConfig.editLogging) {
  39.                 //formats how long ago this was.
  40.                 String formatted;
  41.                 String oldContent;
  42.                 String newContent;
  43.                 StringBuilder response = new StringBuilder();
  44.                 ZonedDateTime oldMessageTime = event.getOldMessage().getTimestamp().atZone(ZoneOffset.UTC);
  45.                 ZonedDateTime newMessageTime = event.getNewMessage().getEditedTimestamp().get().atZone(ZoneOffset.UTC);
  46.                 int charLimit;
  47.                 long difference = newMessageTime.toEpochSecond() - oldMessageTime.toEpochSecond();
  48.                 if (command.guildConfig.useTimeStamps) {
  49.                     formatted = "at `" + Utility.formatTimestamp(oldMessageTime) + " - UTC`";
  50.                 } else {
  51.                     formatted = "from " + Utility.formatTimeDifference(difference);
  52.                 }
  53.                 response.append("> **@" + command.authorUserName + "'s** Message " + formatted + " was **Edited** in channel: " + command.channel.mention() + ". ");
  54.                 //remove excess text that would cause a max char limit error.
  55.                 if (command.message.getContent().isEmpty()) {
  56.                     return;
  57.                 }
  58.                 if (command.guildConfig.extendEditLog) {
  59.                     charLimit = 900;
  60.                 } else {
  61.                     charLimit = 1800;
  62.                 }
  63.                 if (event.getOldMessage().getContent().length() > charLimit) {
  64.                     oldContent = Utility.unFormatMentions(event.getOldMessage()).substring(0, charLimit) + "...";
  65.                 } else {
  66.                     oldContent = Utility.unFormatMentions(event.getOldMessage());
  67.                 }
  68.                 if (event.getNewMessage().getContent().length() > charLimit) {
  69.                     newContent = Utility.unFormatMentions(event.getNewMessage()).substring(0, charLimit) + "...";
  70.                 } else {
  71.                     newContent = Utility.unFormatMentions(event.getNewMessage());
  72.                 }
  73.                 response.append("**\nMessage's Old Contents:**\n" + oldContent);
  74.                 if (command.guildConfig.extendEditLog) {
  75.                     response.append("\n**Message's New Contents:**\n" + newContent);
  76.                 }
  77.                 Utility.sendLog(response.toString(), command.guildConfig, false);
  78.             }
  79.         }
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment