-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more instances in which Dadbot can reply (#1054)
Closes #936
- Loading branch information
Showing
9 changed files
with
107 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
ninbot-app/src/main/java/dev/nincodedo/ninbot/components/dad/DadbotMessageParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dev.nincodedo.ninbot.components.dad; | ||
|
||
import dev.nincodedo.nincord.message.MessageUtils; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Pattern; | ||
|
||
@Slf4j | ||
public class DadbotMessageParser { | ||
|
||
public Optional<String> dadReply(String message, String rawMessage) { | ||
var initialPattern = Pattern.compile("i(?:['‛’‵‘′`]?m| am) ", Pattern.CASE_INSENSITIVE); | ||
var initialMatcher = initialPattern.matcher(message); | ||
if (!initialMatcher.find()) { | ||
log.debug("Failed initial matching \"{}\"", message); | ||
return Optional.empty(); | ||
} | ||
var startingMessage = message.substring(initialMatcher.end()); | ||
var endingPunctuationPattern = Pattern.compile("(?<!Mrs?|M[sx]|[SJD]r)[.?!](:? )?"); | ||
var matcher = endingPunctuationPattern.matcher(startingMessage); | ||
if (matcher.find()) { | ||
startingMessage = startingMessage.substring(0, matcher.start()); | ||
} | ||
|
||
if (!startingMessage.isEmpty()) { | ||
return Optional.of(MessageUtils.addSpoilerText(startingMessage, rawMessage)); | ||
} else { | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
ninbot-app/src/test/java/dev/nincodedo/ninbot/components/dad/DadbotMessageParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dev.nincodedo.ninbot.components.dad; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@Slf4j | ||
class DadbotMessageParserTest { | ||
|
||
DadbotMessageParser dadbotMessageParser = new DadbotMessageParser(); | ||
|
||
public static List<String> dadreplies() { | ||
return List.of("I'm happy", "im glad", "i'm tired", "hi im sad", "I'm ready for anything", "I'm a really long" | ||
+ " message with another sentence in it. This should still reply correctly.", | ||
"This is also a really long message with another sentence in it, but the dad part doesn't happen " | ||
+ "until later. I'm sure it will still work.", "I'm this, and this.", "I'm Dr. Mario and " | ||
+ "this is my favorite Discord server.", | ||
""" | ||
Long ago in a distant land, I Aku unleashed an unspeakable evil. | ||
It was a multiline string. | ||
I'm sure this is fine. | ||
""", "I'm using more punctuation than required!!!", "I'm saying I'm multiple times and that's" | ||
+ " normal I'm sure.", "the beginning of this sentence doesn't have anything good in it but " | ||
+ "I'm sure in the end it will."); | ||
} | ||
|
||
public static List<String> nodadreplies() { | ||
return List.of("I'm", "I'm ", "Just words that don't have that word"); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("dadreplies") | ||
void dadReply(String message) { | ||
var actual = dadbotMessageParser.dadReply(message, ""); | ||
assertThat(actual).isPresent(); | ||
log.debug(actual.get()); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("nodadreplies") | ||
void noDadReply(String message) { | ||
var actual = dadbotMessageParser.dadReply(message, ""); | ||
assertThat(actual).isEmpty(); | ||
} | ||
} |