|
2 | 2 |
|
3 | 3 | import lombok.RequiredArgsConstructor; |
4 | 4 | import lombok.extern.slf4j.Slf4j; |
5 | | -import net.dv8tion.jda.api.entities.*; |
| 5 | +import net.dv8tion.jda.api.entities.Message; |
| 6 | +import net.dv8tion.jda.api.entities.Webhook; |
6 | 7 | import net.dv8tion.jda.api.entities.channel.ChannelType; |
7 | 8 | import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; |
8 | 9 | import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildChannel; |
|
14 | 15 | import net.javadiscord.javabot.util.WebhookUtil; |
15 | 16 |
|
16 | 17 | import javax.annotation.Nonnull; |
| 18 | +import java.util.Objects; |
| 19 | +import java.util.regex.Pattern; |
17 | 20 |
|
18 | 21 | /** |
19 | 22 | * Replaces all occurrences of 'fuck' in incoming messages with 'hug'. |
20 | 23 | */ |
21 | 24 | @Slf4j |
22 | 25 | @RequiredArgsConstructor |
23 | 26 | public class HugListener extends ListenerAdapter { |
| 27 | + private static final Pattern FUCKER = Pattern.compile("(fuck)(ing|er|ed|k+)?", Pattern.CASE_INSENSITIVE); |
24 | 28 | private final AutoMod autoMod; |
25 | 29 | private final BotConfig botConfig; |
26 | 30 |
|
@@ -55,40 +59,45 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) { |
55 | 59 | if (tc == null) { |
56 | 60 | return; |
57 | 61 | } |
58 | | - final TextChannel textChannel = tc; |
59 | 62 | String content = event.getMessage().getContentRaw(); |
60 | | - String lowerCaseContent = content.toLowerCase(); |
61 | | - if (lowerCaseContent.contains("fuck")) { |
| 63 | + if (FUCKER.matcher(content).find()) { |
62 | 64 | long threadId = event.isFromThread() ? event.getChannel().getIdLong() : 0; |
63 | | - StringBuilder sb = new StringBuilder(content.length()); |
64 | | - int index = 0; |
65 | | - int indexBkp = index; |
66 | | - while ((index = lowerCaseContent.indexOf("fuck", index)) != -1) { |
67 | | - sb.append(content.substring(indexBkp, index)); |
68 | | - sb.append(loadHug(content, index)); |
69 | | - indexBkp = index++ + 4; |
70 | | - if (content.length() >= indexBkp + 3 && "ing".equals(lowerCaseContent.substring(indexBkp, indexBkp + 3))) { |
71 | | - sb.append(copyCase(content, indexBkp-1, 'g')); |
72 | | - sb.append(content.substring(indexBkp, indexBkp + 3)); |
73 | | - index+=3; |
74 | | - indexBkp+=3; |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - sb.append(content.substring(indexBkp)); |
79 | | - WebhookUtil.ensureWebhookExists(textChannel, |
80 | | - wh -> sendWebhookMessage(wh, event.getMessage(), sb.toString(), threadId), |
| 65 | + WebhookUtil.ensureWebhookExists(tc, |
| 66 | + wh -> sendWebhookMessage(wh, event.getMessage(), replaceFucks(content), threadId), |
81 | 67 | e -> ExceptionLogger.capture(e, getClass().getSimpleName())); |
82 | 68 | } |
83 | 69 | } |
84 | 70 |
|
85 | | - private String loadHug(String originalText, int startIndex) { |
86 | | - return copyCase(originalText, startIndex, 'h') + "" |
87 | | - + copyCase(originalText, startIndex + 1, 'u') + "" |
88 | | - + copyCase(originalText, startIndex + 3, 'g'); |
| 71 | + private static String processHug(String originalText) { |
| 72 | + // FucK -> HuG, FuCk -> Hug |
| 73 | + return String.valueOf(copyCase(originalText, 0, 'h')) |
| 74 | + + copyCase(originalText, 1, 'u') |
| 75 | + + copyCase(originalText, 3, 'g'); |
| 76 | + } |
| 77 | + |
| 78 | + private static String replaceFucks(String str) { |
| 79 | + return FUCKER.matcher(str).replaceAll(matchResult -> { |
| 80 | + String theFuck = matchResult.group(1); |
| 81 | + String suffix = Objects.requireNonNullElse(matchResult.group(2), ""); |
| 82 | + String processedSuffix = switch(suffix.toLowerCase()) { |
| 83 | + case "er", "ed", "ing" -> copyCase(suffix, 0, 'g') + suffix; // fucking, fucker, fucked |
| 84 | + case "" -> ""; // just fuck |
| 85 | + default -> copyCase(suffix, "g".repeat(suffix.length())); // fuckkkkk... |
| 86 | + }; |
| 87 | + return processHug(theFuck) + processedSuffix; |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + private static String copyCase(String source, String toChange) { |
| 92 | + if (source.length() != toChange.length()) throw new IllegalArgumentException("lengths differ"); |
| 93 | + StringBuilder sb = new StringBuilder(); |
| 94 | + for (int i = 0; i < source.length(); i++) { |
| 95 | + sb.append(copyCase(source, i, toChange.charAt(i))); |
| 96 | + } |
| 97 | + return sb.toString(); |
89 | 98 | } |
90 | 99 |
|
91 | | - private char copyCase(String original, int index, char newChar) { |
| 100 | + private static char copyCase(String original, int index, char newChar) { |
92 | 101 | if (Character.isUpperCase(original.charAt(index))) { |
93 | 102 | return Character.toUpperCase(newChar); |
94 | 103 | } else { |
|
0 commit comments