|
| 1 | +package net.discordjug.javabot.listener.filter; |
| 2 | + |
| 3 | +import lombok.RequiredArgsConstructor; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | + |
| 6 | +import java.util.Objects; |
| 7 | +import java.util.regex.Pattern; |
| 8 | + |
| 9 | +/** |
| 10 | + * This {@link MessageFilter} replaces all occurrences of 'fuck' in incoming messages with 'hug'. |
| 11 | + */ |
| 12 | +@Component |
| 13 | +@RequiredArgsConstructor |
| 14 | +public class HugFilter implements MessageFilter { |
| 15 | + |
| 16 | + private static final Pattern FUCKER = Pattern.compile( |
| 17 | + "(fuck)(ing|er|ed|k+)?", |
| 18 | + Pattern.CASE_INSENSITIVE |
| 19 | + ); |
| 20 | + |
| 21 | + @Override |
| 22 | + public MessageModificationStatus processMessage(MessageContent content) { |
| 23 | + if (!content.event().getMessage().getMentions().getUsers().isEmpty()) { |
| 24 | + return MessageModificationStatus.NOT_MODIFIED; |
| 25 | + } |
| 26 | + String before = content.messageText().toString(); |
| 27 | + String processed = replaceFucks(content.messageText().toString()); |
| 28 | + if (!before.equals(processed)) { |
| 29 | + content.messageText().setLength(0); |
| 30 | + content.messageText().append(processed); |
| 31 | + return MessageModificationStatus.MODIFIED; |
| 32 | + } else { |
| 33 | + return MessageModificationStatus.NOT_MODIFIED; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static String processHug(String originalText) { |
| 38 | + // FucK -> HuG, FuCk -> Hug |
| 39 | + return String.valueOf(copyCase(originalText, 0, 'h')) + copyCase(originalText, 1, 'u') + |
| 40 | + copyCase(originalText, 3, 'g'); |
| 41 | + } |
| 42 | + |
| 43 | + private static String replaceFucks(String str) { |
| 44 | + return FUCKER.matcher(str).replaceAll(matchResult -> { |
| 45 | + String theFuck = matchResult.group(1); |
| 46 | + String suffix = Objects.requireNonNullElse(matchResult.group(2), ""); |
| 47 | + String processedSuffix = switch (suffix.toLowerCase()) { |
| 48 | + case "er", "ed", "ing" -> copyCase(suffix, 0, 'g') + suffix; // fucking, fucker, fucked |
| 49 | + case "" -> ""; // just fuck |
| 50 | + default -> copyCase(suffix, "g".repeat(suffix.length())); // fuckkkkk... |
| 51 | + }; |
| 52 | + return processHug(theFuck) + processedSuffix; |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + private static String copyCase(String source, String toChange) { |
| 57 | + if (source.length() != toChange.length()) { |
| 58 | + throw new IllegalArgumentException("lengths differ"); |
| 59 | + } |
| 60 | + StringBuilder sb = new StringBuilder(); |
| 61 | + for (int i = 0; i < source.length(); i++) { |
| 62 | + sb.append(copyCase(source, i, toChange.charAt(i))); |
| 63 | + } |
| 64 | + return sb.toString(); |
| 65 | + } |
| 66 | + |
| 67 | + private static char copyCase(String original, int index, char newChar) { |
| 68 | + if (Character.isUpperCase(original.charAt(index))) { |
| 69 | + return Character.toUpperCase(newChar); |
| 70 | + } else { |
| 71 | + return newChar; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments