From c7245c18bb70f92b1ae40730899c523e70b893bc Mon Sep 17 00:00:00 2001 From: Alex Bevziuk Date: Tue, 11 Apr 2023 09:42:24 +0300 Subject: [PATCH] Added correct handling for long messages (>2000 symbols) Removed unused code --- .../discord/handler/EmptyMessageHandler.java | 23 ------------------- .../handler/MentionRequestHandler.java | 11 +++++++++ .../kevin/discord/handler/MessageHandler.java | 6 +---- .../handler/PrivateMessageHandler.java | 12 +++++++++- 4 files changed, 23 insertions(+), 29 deletions(-) delete mode 100644 src/main/java/info/kaspian/kevin/discord/handler/EmptyMessageHandler.java diff --git a/src/main/java/info/kaspian/kevin/discord/handler/EmptyMessageHandler.java b/src/main/java/info/kaspian/kevin/discord/handler/EmptyMessageHandler.java deleted file mode 100644 index 0f27683..0000000 --- a/src/main/java/info/kaspian/kevin/discord/handler/EmptyMessageHandler.java +++ /dev/null @@ -1,23 +0,0 @@ -package info.kaspian.kevin.discord.handler; - -import io.quarkus.runtime.Startup; -import net.dv8tion.jda.api.entities.Message; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.enterprise.context.ApplicationScoped; - -@ApplicationScoped -@Startup -public class EmptyMessageHandler { - - private static final Logger LOG = LoggerFactory.getLogger(EmptyMessageHandler.class); - - public EmptyMessageHandler() { - LOG.info("EmptyMessageHandler created"); - } - - public void handle(Message message) { - /* Ignore */ - } -} diff --git a/src/main/java/info/kaspian/kevin/discord/handler/MentionRequestHandler.java b/src/main/java/info/kaspian/kevin/discord/handler/MentionRequestHandler.java index 57aa750..4d91c72 100644 --- a/src/main/java/info/kaspian/kevin/discord/handler/MentionRequestHandler.java +++ b/src/main/java/info/kaspian/kevin/discord/handler/MentionRequestHandler.java @@ -35,8 +35,19 @@ public void handle(Message message) { .responseStream().subscribe(new DisposableSubscriber<>() { private final StringBuilder sb = new StringBuilder(); + @Override + protected void onStart() { + super.onStart(); + message.getChannel().sendTyping().queue(); + } + @Override public void onNext(String s) { + if (sb.length() + s.length() > Message.MAX_CONTENT_LENGTH) { + message.reply(sb.toString()).queue(); + message.getChannel().sendTyping().queue(); + sb.delete(0, sb.length()); + } sb.append(s); } diff --git a/src/main/java/info/kaspian/kevin/discord/handler/MessageHandler.java b/src/main/java/info/kaspian/kevin/discord/handler/MessageHandler.java index 49a6365..5d8174a 100644 --- a/src/main/java/info/kaspian/kevin/discord/handler/MessageHandler.java +++ b/src/main/java/info/kaspian/kevin/discord/handler/MessageHandler.java @@ -16,13 +16,11 @@ public class MessageHandler { private static final Logger LOG = LoggerFactory.getLogger(MessageHandler.class); - private final EmptyMessageHandler emptyMessageHandler; private final PrivateMessageHandler privateMessageHandler; private final MentionRequestHandler mentionRequestHandler; - public MessageHandler(EmptyMessageHandler emptyMessageHandler, PrivateMessageHandler privateMessageHandler, MentionRequestHandler mentionRequestHandler) { + public MessageHandler(PrivateMessageHandler privateMessageHandler, MentionRequestHandler mentionRequestHandler) { LOG.info("MessageHandler created"); - this.emptyMessageHandler = emptyMessageHandler; this.privateMessageHandler = privateMessageHandler; this.mentionRequestHandler = mentionRequestHandler; } @@ -32,8 +30,6 @@ public void handle(Message message) { privateMessageHandler.handle(message); } else if (isCurrentUserMentioned(message)) { mentionRequestHandler.handle(message); - } else { - emptyMessageHandler.handle(message); } } diff --git a/src/main/java/info/kaspian/kevin/discord/handler/PrivateMessageHandler.java b/src/main/java/info/kaspian/kevin/discord/handler/PrivateMessageHandler.java index 5c267cc..56b4ca7 100644 --- a/src/main/java/info/kaspian/kevin/discord/handler/PrivateMessageHandler.java +++ b/src/main/java/info/kaspian/kevin/discord/handler/PrivateMessageHandler.java @@ -20,7 +20,6 @@ public class PrivateMessageHandler { private static final Logger LOG = LoggerFactory.getLogger(PrivateMessageHandler.class); - private final ChatModelConnectorService chatModelConnectorService; public PrivateMessageHandler(ChatModelConnectorService chatModelConnectorService) { @@ -49,8 +48,19 @@ public void handle(Message message) { .responseStream().subscribe(new DisposableSubscriber<>() { private final StringBuilder sb = new StringBuilder(); + @Override + protected void onStart() { + super.onStart(); + channel.sendTyping().queue(); + } + @Override public void onNext(String s) { + if (sb.length() + s.length() > Message.MAX_CONTENT_LENGTH) { + channel.sendMessage(sb.toString()).queue(); + channel.sendTyping().queue(); + sb.delete(0, sb.length()); + } sb.append(s); }