Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
refactor: 把Discord发送消息后的监听移动从 ProtocolUtil.java 到 DiscordListener.java
Browse files Browse the repository at this point in the history
  • Loading branch information
NahidaLing committed Feb 23, 2024
1 parent 7e48480 commit d220b33
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,42 +88,7 @@ public void sendMessage(String channelID , String message , List<UploadFile> 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<String> 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
Expand Down

0 comments on commit d220b33

Please sign in to comment.