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

Commit

Permalink
refactor(CozeProxy): 重写登录逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
NahidaLing committed Apr 21, 2024
1 parent f3d0bfc commit 67ca8fa
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
104 changes: 87 additions & 17 deletions CozeProxy/src/main/java/catx/feitu/CozeProxy/CozeGPT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import catx.feitu.CozeProxy.utils.extensions.Protocol;

import java.io.ByteArrayInputStream;
import java.net.Proxy;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -36,32 +37,101 @@ public CozeGPT(CozeGPTConfig config) {
eventListen = new EventHandle(utils);
this.config = config;
}
public CozeGPT(CozeGPTConfig config,boolean autoLogin) throws Exception {
this(config);
if (autoLogin) {
this.login();
}
}
/**
* Discord登录
* 登录一个新的Discord账号
* @param token DiscordUser账号Token
* @param proxy 自定义代理配置 null使用配置中默认代理
* @throws Exception 如果登录过程遇到任何问题,则抛出异常.
*/
public void login() throws Exception {
disconnect();
public void login(String token, Proxy proxy) throws Exception {
Protocol protocolJoin = new Protocol();

for (String token : config.token) {
Protocol protocolJoin = new Protocol();
protocolJoin.setConfig(new EventListenConfig(config.serverID ,config.botID,!config.Disable_CozeBot_ReplyMsgCheck));
protocolJoin.login(config.loginApp ,token ,proxy == null ? config.Proxy : proxy);

protocolJoin.setConfig(new EventListenConfig(config.serverID ,config.botID,!config.Disable_CozeBot_ReplyMsgCheck));
protocolJoin.login(config.loginApp ,token ,config.Proxy);
protocol.add(protocolJoin);
}
/**
* 登录一个新的Discord账号
* @param token DiscordUser账号Token
* @throws Exception 如果登录过程遇到任何问题,则抛出异常.
*/
public void login(String token) throws Exception {
login(token,null);
}
/**
* 批量登录新的Discord账号
* @param tokens DiscordUser账号Token列表
* @param ignoredException 是否忽略异常 为否一个账号登录失败则直接throw
* @param proxy 自定义代理配置 null使用配置中默认代理
* @return 登录成功的账号数
* @throws Exception 如果登录过程遇到任何问题,则抛出异常.
*/
public int login(List<String> tokens, boolean ignoredException, Proxy proxy) throws Exception {
int i = 0;
for (String token : tokens) {
try {
Protocol protocolJoin = new Protocol();

protocolJoin.setConfig(new EventListenConfig(config.serverID ,config.botID,!config.Disable_CozeBot_ReplyMsgCheck));
protocolJoin.login(config.loginApp ,token ,proxy == null ? config.Proxy : proxy);

protocol.add(protocolJoin);
protocol.add(protocolJoin);
i++;
} catch (Exception e) {
if (!ignoredException) {
throw e;
}
}
}
if (protocol.isEmpty()) throw new NoAccountLoginException();
return i;
}
/**
* 批量登录新的Discord账号
* @param tokens DiscordUser账号Token列表
* @param ignoredException 是否忽略异常 为否一个账号登录失败则直接throw
* @return 登录成功的账号数
* @throws Exception 如果登录过程遇到任何问题,则抛出异常.
*/
public int login(List<String> tokens, boolean ignoredException) throws Exception {
return login(tokens, ignoredException, null);
}
/**
* 批量登录新的Discord账号
* @param tokens DiscordUser账号Token列表
* @param proxy 自定义代理配置 null使用配置中默认代理
* @return 登录成功的账号数
*/
public int login(List<String> tokens, Proxy proxy) {
try {
return login(tokens, true,proxy);
} catch (Exception ignored) {
return 0;
}
}
/**
* 批量登录新的Discord账号
* @param tokens DiscordUser账号Token列表
* @return 登录成功的账号数
*/
public int login(List<String> tokens) {
try {
return login(tokens, true);
} catch (Exception ignored) {
return 0;
}
}
/**
* 获取已登录账号数
* @return 已登录账号数
*/
public int getLoginCount() {
return protocol.size();
}
/**
* Discord登出
* 退出登录并删除全部账号
*/
public void disconnect() {
public void disconnectAll() {
for (ProtocolHandle protocol : protocol) {
try { protocol.disconnect(); } catch (Exception ignored) { }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

public class CozeGPTConfig {
public String loginApp = Protocols.DISCORD;
/**
* 用户Token 发消息使用
* Coze现在屏蔽bot账号发送消息 必须换成用户账号发送
*/
public List<String> token = new ArrayList<String>();
/**
* 连接到Discord服务器时所使用的代理
* 对于某些无法直连Discord的地区很有帮助
Expand Down

0 comments on commit 67ca8fa

Please sign in to comment.