Skip to content

Commit 8e2a3f9

Browse files
authored
Merge pull request #410 from 0x3C50/main
Improve HugListener to accept "fucker", "fuckkkk...", and to use regex instead
2 parents 27e2339 + dcf77a0 commit 8e2a3f9

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

src/main/java/net/javadiscord/javabot/listener/HugListener.java

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import lombok.RequiredArgsConstructor;
44
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;
67
import net.dv8tion.jda.api.entities.channel.ChannelType;
78
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
89
import net.dv8tion.jda.api.entities.channel.middleman.StandardGuildChannel;
@@ -14,13 +15,16 @@
1415
import net.javadiscord.javabot.util.WebhookUtil;
1516

1617
import javax.annotation.Nonnull;
18+
import java.util.Objects;
19+
import java.util.regex.Pattern;
1720

1821
/**
1922
* Replaces all occurrences of 'fuck' in incoming messages with 'hug'.
2023
*/
2124
@Slf4j
2225
@RequiredArgsConstructor
2326
public class HugListener extends ListenerAdapter {
27+
private static final Pattern FUCKER = Pattern.compile("(fuck)(ing|er|ed|k+)?", Pattern.CASE_INSENSITIVE);
2428
private final AutoMod autoMod;
2529
private final BotConfig botConfig;
2630

@@ -55,40 +59,45 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
5559
if (tc == null) {
5660
return;
5761
}
58-
final TextChannel textChannel = tc;
5962
String content = event.getMessage().getContentRaw();
60-
String lowerCaseContent = content.toLowerCase();
61-
if (lowerCaseContent.contains("fuck")) {
63+
if (FUCKER.matcher(content).find()) {
6264
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),
8167
e -> ExceptionLogger.capture(e, getClass().getSimpleName()));
8268
}
8369
}
8470

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();
8998
}
9099

91-
private char copyCase(String original, int index, char newChar) {
100+
private static char copyCase(String original, int index, char newChar) {
92101
if (Character.isUpperCase(original.charAt(index))) {
93102
return Character.toUpperCase(newChar);
94103
} else {

0 commit comments

Comments
 (0)