From d220b336c862ff6b70135e04d196f78121e77a30 Mon Sep 17 00:00:00 2001 From: CatX_feitu <3205711786@qq.com> Date: Fri, 23 Feb 2024 22:04:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8A=8ADiscord=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=90=8E=E7=9A=84=E7=9B=91=E5=90=AC=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E4=BB=8E=20ProtocolUtil.java=20=E5=88=B0=20DiscordLis?= =?UTF-8?q?tener.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Protocol/Listener/DiscordListener.java | 47 +++++++++++++++++-- .../CozeProxy/Protocol/ProtocolUtil.java | 38 +-------------- 2 files changed, 44 insertions(+), 41 deletions(-) diff --git a/CozeProxy/src/main/java/catx/feitu/CozeProxy/Protocol/Listener/DiscordListener.java b/CozeProxy/src/main/java/catx/feitu/CozeProxy/Protocol/Listener/DiscordListener.java index 65a91ca..996c2a1 100644 --- a/CozeProxy/src/main/java/catx/feitu/CozeProxy/Protocol/Listener/DiscordListener.java +++ b/CozeProxy/src/main/java/catx/feitu/CozeProxy/Protocol/Listener/DiscordListener.java @@ -2,13 +2,50 @@ import catx.feitu.CozeProxy.Protocol.UniversalEventListener; import catx.feitu.CozeProxy.Protocol.UniversalEventListenerConfig; +import catx.feitu.CozeProxy.Protocol.UniversalMessage; +import catx.feitu.DiscordSelfClient.client.SelfClient; +import catx.feitu.DiscordSelfClient.client.impl.Message; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; public class DiscordListener { - public UniversalEventListener handle; - public UniversalEventListenerConfig config; - public DiscordListener(UniversalEventListener handle, UniversalEventListenerConfig config) { - this.handle = handle; - this.config = config; + public void listen(SelfClient api_discord ,String channelID ,UniversalEventListener eventListener ,UniversalEventListenerConfig config) { + Thread thread = new Thread(() -> { // Websocket监听器不会写(其实是太耗时了) + try { + Thread.sleep(1000); + int attempt = 0; // 重试次数 + Message latestMessage = api_discord.getLatestMessage(channelID); + if (!latestMessage.getUser().isBot() && (config.filterReply || latestMessage.getMentions().contains(config.filterSelfUserID))) { // 如果是bot就已经出现 不需要再等待 + while (!latestMessage.getUser().isBot()) { + if (attempt > 20) { return; } + latestMessage = api_discord.getLatestMessage(channelID); + try { Thread.sleep(500); } catch (InterruptedException ignored) {} + attempt++; + } + } + eventListener.onStartGenerate(channelID); + attempt = 0; + while (attempt < 120) { + latestMessage = api_discord.getMessage(channelID ,latestMessage.getId()); + + List eventFiles = new CopyOnWriteArrayList<>(); // 存储嵌入附件URL + for (catx.feitu.DiscordSelfClient.client.impl.Attachment attachment : latestMessage.getAttachments()) { + eventFiles.add(attachment.getUrl()); + } + + eventListener.onMessageStream(channelID ,new UniversalMessage() + .setContent(latestMessage.getContent()) + .setFiles(eventFiles) + .setHasButton(latestMessage.isHasComponents()) + ); + if (latestMessage.isHasComponents()) { return; } + try { Thread.sleep(1000); } catch (InterruptedException ignored) {} + attempt++; + } + } catch (Exception ignored) { } + }); + thread.start(); } } \ No newline at end of file diff --git a/CozeProxy/src/main/java/catx/feitu/CozeProxy/Protocol/ProtocolUtil.java b/CozeProxy/src/main/java/catx/feitu/CozeProxy/Protocol/ProtocolUtil.java index 8d07f5a..6581fb3 100644 --- a/CozeProxy/src/main/java/catx/feitu/CozeProxy/Protocol/ProtocolUtil.java +++ b/CozeProxy/src/main/java/catx/feitu/CozeProxy/Protocol/ProtocolUtil.java @@ -3,6 +3,7 @@ import catx.feitu.CozeProxy.Protocol.Exception.ProtocolAPIFailedException; import catx.feitu.CozeProxy.Protocol.Exception.ProtocolNotLoginException; import catx.feitu.CozeProxy.Protocol.Exception.UnSupportedProtocolException; +import catx.feitu.CozeProxy.Protocol.Listener.DiscordListener; import catx.feitu.CozeProxy.Protocol.Listener.SlackListener; import catx.feitu.CozeProxy.Protocol.Types.UploadFile; import catx.feitu.DiscordSelfClient.client.SelfClient; @@ -87,42 +88,7 @@ public void sendMessage(String channelID , String message , List fil } } api_discord.sendMessage(message ,channelID ,uploadFiles); - - Thread thread = new Thread(() -> { // Websocket监听器不会写(其实是太耗时了) - try { - Thread.sleep(1000); - int attempt = 0; // 重试次数 - Message latestMessage = api_discord.getLatestMessage(channelID); - if (!latestMessage.getUser().isBot() && (config.filterReply || latestMessage.getMentions().contains(config.filterSelfUserID))) { // 如果是bot就已经出现 不需要再等待 - while (!latestMessage.getUser().isBot()) { - if (attempt > 20) { return; } - latestMessage = api_discord.getLatestMessage(channelID); - try { Thread.sleep(500); } catch (InterruptedException ignored) {} - attempt++; - } - } - eventListener.onStartGenerate(channelID); - attempt = 0; - while (attempt < 120) { - latestMessage = api_discord.getMessage(channelID ,latestMessage.getId()); - - List eventFiles = new CopyOnWriteArrayList<>(); // 存储嵌入附件URL - for (catx.feitu.DiscordSelfClient.client.impl.Attachment attachment : latestMessage.getAttachments()) { - eventFiles.add(attachment.getUrl()); - } - - eventListener.onMessageStream(channelID ,new UniversalMessage() - .setContent(latestMessage.getContent()) - .setFiles(eventFiles) - .setHasButton(latestMessage.isHasComponents()) - ); - if (latestMessage.isHasComponents()) { return; } - try { Thread.sleep(1000); } catch (InterruptedException ignored) {} - attempt++; - } - } catch (Exception ignored) { } - }); - thread.start(); + new DiscordListener().listen(api_discord ,channelID ,eventListener ,config); return; case catx.feitu.CozeProxy.Protocol.Protocols.SLACK: // by ChatGPT