1414import net .javadiscord .javabot .util .WebhookUtil ;
1515
1616import javax .annotation .Nonnull ;
17+ import java .util .Objects ;
18+ import java .util .regex .Pattern ;
1719
1820/**
1921 * Replaces all occurrences of 'fuck' in incoming messages with 'hug'.
@@ -24,6 +26,20 @@ public class HugListener extends ListenerAdapter {
2426 private final AutoMod autoMod ;
2527 private final BotConfig botConfig ;
2628
29+ private static final Pattern FUCKER = Pattern .compile ("(fuck)(ing|er|k+)?" , Pattern .CASE_INSENSITIVE );
30+
31+ private static String processFuck (String str ) {
32+ return FUCKER .matcher (str ).replaceAll (matchResult -> {
33+ String theFuck = matchResult .group (1 );
34+ String suffix = Objects .requireNonNullElse (matchResult .group (2 ), "" );
35+ String processedSuffix = switch (suffix .toLowerCase ()) {
36+ case "er" , "ing" -> copyCase (suffix , 0 , 'g' ) + suffix ;
37+ default -> suffix .startsWith ("k" ) ? "g" .repeat (suffix .length ()) : "" ;
38+ };
39+ return processHug (theFuck ) + processedSuffix ;
40+ });
41+ }
42+
2743 @ Override
2844 public void onMessageReceived (@ Nonnull MessageReceivedEvent event ) {
2945 if (!event .isFromGuild ()) {
@@ -57,38 +73,22 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
5773 }
5874 final TextChannel textChannel = tc ;
5975 String content = event .getMessage ().getContentRaw ();
60- String lowerCaseContent = content .toLowerCase ();
61- if (lowerCaseContent .contains ("fuck" )) {
76+ if (FUCKER .matcher (content ).find ()) {
6277 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 ));
7978 WebhookUtil .ensureWebhookExists (textChannel ,
80- wh -> sendWebhookMessage (wh , event .getMessage (), sb . toString ( ), threadId ),
79+ wh -> sendWebhookMessage (wh , event .getMessage (), processFuck ( content ), threadId ),
8180 e -> ExceptionLogger .capture (e , getClass ().getSimpleName ()));
8281 }
8382 }
8483
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' );
84+ private static String processHug (String originalText ) {
85+ // FucK -> HuG, FuCk -> Hug
86+ return String .valueOf (copyCase (originalText , 0 , 'h' ))
87+ + copyCase (originalText , 1 , 'u' )
88+ + copyCase (originalText , 3 , 'g' );
8989 }
9090
91- private char copyCase (String original , int index , char newChar ) {
91+ private static char copyCase (String original , int index , char newChar ) {
9292 if (Character .isUpperCase (original .charAt (index ))) {
9393 return Character .toUpperCase (newChar );
9494 } else {
0 commit comments