Skip to content

Commit

Permalink
perf: avoid regex split on chat message (#638)
Browse files Browse the repository at this point in the history
* perf: avoid regex split on chat message

* chore: drop too long outbound messages
  • Loading branch information
iProdigy committed Sep 4, 2022
1 parent 3ee3fc8 commit 1e387c8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions chat/src/main/java/com/github/twitch4j/chat/TwitchChat.java
Expand Up @@ -530,8 +530,7 @@ protected void onConnected() {
}

protected void onTextMessage(String text) {
Arrays.asList(text.replace("\n\r", "\n")
.replace("\r", "\n").split("\n"))
Arrays.asList(StringUtils.split(text.replace("\r\n", "\n"), '\n'))
.forEach(message -> {
if (!message.equals("")) {
// Handle messages
Expand Down Expand Up @@ -761,6 +760,11 @@ private boolean removeCurrentChannel(String channelName) {
@Override
@SuppressWarnings("ConstantConditions")
public boolean sendMessage(String channel, String message, Map<String, Object> tags) {
if (message.length() > 500) {
log.warn("Ignoring outbound chat message for channel #{} exceeding the Twitch maximum of 500 characters: {}", channel, message);
return false;
}

StringBuilder sb = new StringBuilder();
if (tags != null && !tags.isEmpty()) {
sb.append('@');
Expand Down

0 comments on commit 1e387c8

Please sign in to comment.