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

Commit

Permalink
[+] IsBotOnline 检查CozeBot是否在线
Browse files Browse the repository at this point in the history
  • Loading branch information
NekoCurit committed Feb 4, 2024
1 parent b1a707e commit 117d5ff
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public void configure(HttpsParameters params) {
AddAPI("/api/Chat", new Chat(), true);
AddAPI("/api/ChatStream", new ChatStream(), true);
AddAPI("/api/RenameConversation", new RenameConversation(), true);
AddAPI("/api/IsBotOnline", new IsBotOnline(), true);

AddAPI("/api/discord/GetUserInfo", new GetUserInfo(), true);
AddAPI("/api/discord/ReLogin", new ReLogin(), true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package catx.feitu.coze_discord_bridge.HttpServer.api.api;

import catx.feitu.coze_discord_bridge.HttpServer.APIHandler;
import catx.feitu.coze_discord_bridge.HttpServer.HandleType;
import catx.feitu.coze_discord_bridge.HttpServer.ResponseType;
import com.alibaba.fastjson.JSONObject;

public class IsBotOnline implements APIHandler {
@Override
public ResponseType handle(HandleType Handle) {
ResponseType Response = new ResponseType();
JSONObject json = new JSONObject(true);
try {
Response.code = 200;
json.put("code", 200);
json.put("message", "获取成功");
JSONObject json_data = new JSONObject(true);
json_data.put("status", Handle.CozeGPT.IsCozeBotOnline());
json.put("data", json_data);
} catch (Exception e) {
Response.code = 502;
json.put("code", 502);
json.put("message", "获取失败");
JSONObject json_data = new JSONObject(true);
json.put("data", json_data);
}
Response.msg = json.toJSONString();
return Response;
}
}
14 changes: 11 additions & 3 deletions src/main/java/catx/feitu/coze_discord_bridge/api/CozeGPT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.javacord.api.entity.intent.Intent;
import org.javacord.api.entity.message.Message;
import org.javacord.api.entity.server.Server;
import org.javacord.api.entity.user.User;
import org.javacord.api.entity.user.UserStatus;

import java.io.ByteArrayInputStream;
import java.util.List;
Expand Down Expand Up @@ -47,10 +49,8 @@ public CozeGPT(CozeGPTConfig config,boolean autoLogin) throws Exception {
}
/**
* Discord登录
*
* @throws Exception 遇到错误会抛出异常
*/
public void Login() throws Exception {
public void Login() {
if (discord_api != null) {
discord_api.disconnect();
}
Expand Down Expand Up @@ -320,6 +320,14 @@ public ConversationInfo GetConversationInfo (String ConversationName) throws Exc
return_info.ID = Channel.get().getIdAsString();
return return_info;
}
public boolean IsCozeBotOnline() throws Exception {
GetServer();
Optional<User> cozeBot = server.getMemberById(config.CozeBot_id);
if (cozeBot.isEmpty()) {
throw new InvalidCozeBotUserIDException();
}
return cozeBot.get().getStatus() != UserStatus.OFFLINE;
}

private void GetServer() throws Exception {
if (discord_api == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package catx.feitu.coze_discord_bridge.api.Exceptions;

public class InvalidCozeBotUserIDException extends Exception {
public InvalidCozeBotUserIDException() {
super();
}
}

0 comments on commit 117d5ff

Please sign in to comment.