Advertisement
Vaerys_Dawn

MentionRemover

Oct 28th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. public class TagRemoveMentions extends TagObject {
  2.  
  3.     public TagRemoveMentions(int priority) {
  4.         super(priority);
  5.     }
  6.  
  7.     @Override
  8.     public String execute(String from, CommandObject command, String args) {
  9.         boolean isRoleMention = false;
  10.         String id;
  11.         if (Pattern.compile("<@&[0-9]*>").matcher(from).find()) {
  12.             id = StringUtils.substringBetween(from, "<@&", ">");
  13.             isRoleMention = true;
  14.         } else {
  15.             id = StringUtils.substringBetween(from, "<@!", ">");
  16.             if (id == null) {
  17.                 id = StringUtils.substringBetween(from, "<@", ">");
  18.             }
  19.         }
  20.         if (!isRoleMention) {
  21.             try {
  22.                 long userID = Long.parseUnsignedLong(id);
  23.                 IUser user = command.guild.getUserByID(userID);
  24.                 if (user != null) {
  25.                     from = from.replace(user.mention(true), user.getDisplayName(command.guild.get()));
  26.                     from = from.replace(user.mention(false), user.getDisplayName(command.guild.get()));
  27.                 } else {
  28.                     throw new NumberFormatException("You shouldn't see this.");
  29.                 }
  30.             } catch (NumberFormatException e) {
  31.                 from = from.replace("<@!" + id + ">", "null");
  32.                 from = from.replace("<@" + id + ">", "null");
  33.             }
  34.         } else {
  35.             //remove role mentions
  36.             try {
  37.                 long roleID = Long.parseUnsignedLong(id);
  38.                 IRole role = command.guild.getRoleByID(roleID);
  39.                 if (role != null) {
  40.                     from = from.replace("<@&" + id + ">", role.getName());
  41.                 } else {
  42.                     throw new NumberFormatException("You shouldn't see this.");
  43.                 }
  44.             } catch (NumberFormatException e) {
  45.                 from = from.replace("<@&" + id + ">", "null");
  46.             }
  47.         }
  48.         return from;
  49.     }
  50.  
  51.     @Override
  52.     public boolean cont(String from) {
  53.         return Pattern.compile("<@!?[0-9]*>").matcher(from).find() ||
  54.                 Pattern.compile("<@&[0-9]*>").matcher(from).find();
  55.     }
  56.  
  57.     @Override
  58.     public String tagName() {
  59.         return "<removeMentions>";
  60.     }
  61.  
  62.     @Override
  63.     public int argsRequired() {
  64.         return 0;
  65.     }
  66.  
  67.     @Override
  68.     public String usage() {
  69.         return null;
  70.     }
  71.  
  72.     @Override
  73.     public String desc() {
  74.         return "Removes mass mentions like Everyone and Here and turns role and user mentions into plain text.\n\n" +
  75.                 "**This is always in every Custom Command.**";
  76.     }
  77.  
  78.     @Override
  79.     public String handleTag(String from, CommandObject command, String args) {
  80.         while (cont(from)) {
  81.             from = execute(from, command, args);
  82.         }
  83.         from = from.replaceAll("(?i)@everyone", "**[REDACTED]**");
  84.         from = from.replaceAll("(?i)@here", "**[REDACTED]**");
  85.         return from;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement