Skip to content

Commit

Permalink
*全部采用websocket连接
Browse files Browse the repository at this point in the history
*fix #3
*添加入群退群提示
  • Loading branch information
cnlimiter committed Jan 8, 2022
1 parent 8b0b71b commit 309a259
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 261 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
apply plugin: 'net.minecraftforge.gradle'

group = 'cn.evolvefield.mods'
version = '1.17.1-1.4.2'
version = '1.17.1-1.5.1'

java {
archivesBaseName = 'Bot-Connect'
Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ org.gradle.daemon=false


mc_version=1.17.1
mod_version=1.2.1
forge_version=37.0.33
forge_version=37.1.1
7 changes: 4 additions & 3 deletions src/main/java/cn/evolvefield/mods/botapi/BotApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ private void processIMC(final InterModProcessEvent event) {
}

private void onServerStarting(FMLServerStartingEvent event){
// if (BotApi.config.getCommon().isENABLED()) {
// ClientThreadService.runWebSocketClient();
// }
//加载配置
ConfigManger.initBotConfig();
if (BotApi.config.getCommon().isEnable()) {
ClientThreadService.runWebSocketClient();
}

}

private void onServerStopping(FMLServerStoppingEvent event) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evolvefield.mods.botapi.message;
package cn.evolvefield.mods.botapi.api;

import cn.evolvefield.mods.botapi.util.json.JSONObject;

Expand Down
104 changes: 104 additions & 0 deletions src/main/java/cn/evolvefield/mods/botapi/api/SendMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cn.evolvefield.mods.botapi.api;


import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.util.json.JSONObject;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.List;

import static cn.evolvefield.mods.botapi.network.WebSocket.WebSocketChannelSupervise.sendToAll;

public class SendMessage {

public static void Private(long user_id, String message){
JSONObject data = new JSONObject();
JSONObject params = new JSONObject();
data.put("action", "send_private_msg");

params.put("user_id", user_id);
params.put("message", message);
data.put("params", params);
if(BotApi.config.getCommon().isDebuggable()){
BotApi.LOGGER.info("向用户" + user_id + "发送消息" + message);
}
sendToAll(new TextWebSocketFrame(data.toString()));
}


public static void Group(long group_id, String message) {
JSONObject data = new JSONObject();
JSONObject params = new JSONObject();
data.put("action", "send_group_msg");

params.put("group_id", group_id);
params.put("message", message);
data.put("params", params);
if(BotApi.config.getCommon().isDebuggable()){
BotApi.LOGGER.info("向群" + group_id + "发送消息" + message);
}
sendToAll(new TextWebSocketFrame(data.toString()));
}

// public static JSONObject getProfile(long group, long userId) {
//
// }


private static final JSONObject errorObject = new JSONObject("{\"retcode\": 1}");

//获取群成员信息
// public static JSONObject getProfile(long group, long userId) {
//
// try {
// String resp = HttpRequest.post("http://" + BotApi.config.getCommon().getSendHOST() + ":" + BotApi.config.getCommon().getSendPORT() +
// "/get_group_member_info?group_id=" + group + "&user_id=" + userId)
// .trustAllCerts()
// .trustAllHosts()
// .connectTimeout(60000)
// .readTimeout(60000)
// .body();
// return new JSONObject(resp);
// } catch (JSONException e) {
// e.printStackTrace();
// return errorObject;
// }
// }

//获取用户名信息
public static String getUsernameFromInfo(JSONObject userInfo) {
if (userInfo == null) {
return "";
}

if (userInfo.getNumber("retcode").intValue() != 0) {
return "";
}

String username = userInfo.getJSONObject("data").getString("card");
if (username.equals("")) {
username = userInfo.getJSONObject("data").getString("nickname");
}

return username;
}

public static String setListMessage(List msg) {
if (msg.size() <= 1) {
return (String)msg.get(0);
} else {
String Message = null;

for (Object o : msg) {
String a = (String) o;
if (Message == null) {
Message = a;
} else {
Message = Message + "\n" + a;
}
}

return Message;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,27 @@
public class ConnectCommand {
public static ArgumentBuilder<CommandSourceStack, ?> register() {
return literal("connect")
.then(literal("send")
.then(Commands.argument("<address:port>", StringArgumentType.greedyString())
.executes(ConnectCommand::sendExecute)))
.then(literal("receive")
.then(Commands.argument("<address:port>", StringArgumentType.greedyString())
.executes(ConnectCommand::receiveExecute)))
.executes(ConnectCommand::receiveExecute)
.then(Commands.argument("<address:port>", StringArgumentType.greedyString())
.executes(ConnectCommand::receiveExecute))
;
}
public static int sendExecute(CommandContext<CommandSourceStack> context) throws CommandRuntimeException {
String input = context.getArgument("<address:port>", String.class);
String[] args = context.getInput().split("\\s+");
switch(args.length) {
default: {
context.getSource().sendSuccess(new TextComponent("参数不合法"), true);
break;
}
case 4: {
Pattern pattern = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+):(\\d+)");
Matcher matcher = pattern.matcher(args[3]);
if(matcher.find()) {
BotApi.config.getCommon().setSEND_ENABLED(true);
BotApi.config.getCommon().setSendHOST(matcher.group(1));
BotApi.config.getCommon().setSendPORT(Integer.parseInt(matcher.group(2)));
ConfigManger.saveBotConfig(BotApi.config);
context.getSource().sendSuccess(new TextComponent("已保存,正在尝试建立http连接"), true);
} else {
context.getSource().sendSuccess(new TextComponent("格式错误"), true);
}
break;
}
case 3: {
context.getSource().sendSuccess(new TextComponent("尝试建立http连接"), true);
break;
}
}
BotApi.config.getCommon().setENABLED(true);
ConfigManger.saveBotConfig(BotApi.config);
return 0;
}

public static int receiveExecute(CommandContext<CommandSourceStack> context) throws CommandRuntimeException {
String[] args = context.getInput().split("\\s+");
switch(args.length) {
default: {
context.getSource().sendSuccess(new TextComponent("参数不合法"), true);
break;
}
case 5: {
case 4: {
Pattern pattern = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+):(\\d+)");
Matcher matcher = pattern.matcher(args[3]);
if (matcher.find()) {
BotApi.config.getCommon().setRECEIVE_ENABLED(true);
BotApi.config.getCommon().setWsHOST(matcher.group(1));
BotApi.config.getCommon().setWsPORT(Integer.parseInt(matcher.group(2)));
BotApi.config.getCommon().setKEY(args[4]);
BotApi.config.getCommon().setWsHost(matcher.group(1));
BotApi.config.getCommon().setWsPort(Integer.parseInt(matcher.group(2)));
BotApi.config.getCommon().setWsKey(args[3]);
ConfigManger.saveBotConfig(BotApi.config);
context.getSource().sendSuccess(new TextComponent("已保存,正在尝试建立WebSocket连接"), true);
ClientThreadService.runWebSocketClient();
Expand All @@ -82,13 +49,13 @@ public static int receiveExecute(CommandContext<CommandSourceStack> context) thr
}
break;
}
case 4: {
case 3: {
Pattern pattern = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+):(\\d+)");
Matcher matcher = pattern.matcher(args[3]);
Matcher matcher = pattern.matcher(args[2]);
if(matcher.find()) {
BotApi.config.getCommon().setRECEIVE_ENABLED(true);
BotApi.config.getCommon().setWsHOST(matcher.group(1));
BotApi.config.getCommon().setWsPORT(Integer.parseInt(matcher.group(2)));
BotApi.config.getCommon().setWsHost(matcher.group(1));
BotApi.config.getCommon().setWsPort(Integer.parseInt(matcher.group(2)));
ConfigManger.saveBotConfig(BotApi.config);
context.getSource().sendSuccess(new TextComponent("已保存,正在尝试建立WebSocket连接"), true);
ClientThreadService.runWebSocketClient();
Expand All @@ -97,13 +64,13 @@ public static int receiveExecute(CommandContext<CommandSourceStack> context) thr
}
break;
}
case 3: {
case 2: {
context.getSource().sendSuccess(new TextComponent("尝试建立WebSocket连接"), true);
ClientThreadService.runWebSocketClient();
break;
}
}
BotApi.config.getCommon().setENABLED(true);
BotApi.config.getCommon().setEnable(true);
ConfigManger.saveBotConfig(BotApi.config);
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
} else {
context.getSource().sendSuccess(new TextComponent("WebSocket目前未连接"), true);
}
BotApi.config.getCommon().setENABLED(false);
BotApi.config.getCommon().setEnable(false);
ConfigManger.saveBotConfig(BotApi.config);
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


import cn.evolvefield.mods.botapi.BotApi;
import cn.evolvefield.mods.botapi.message.SendMessage;
import cn.evolvefield.mods.botapi.api.SendMessage;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class StatusCommand {
return Commands.literal("status").executes(StatusCommand::execute);
}
public static int execute(CommandContext<CommandSourceStack> context) throws CommandRuntimeException {
boolean clientEnabled = BotApi.config.getCommon().isENABLED();
boolean clientEnabled = BotApi.config.getCommon().isEnable();

boolean receiveEnabled = BotApi.config.getCommon().isRECEIVE_ENABLED();
boolean rChatEnabled = BotApi.config.getCommon().isR_CHAT_ENABLE();
Expand All @@ -29,9 +29,9 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com

boolean debuggable = BotApi.config.getCommon().isDebuggable();
boolean connected = ClientThreadService.client != null;
String host = BotApi.config.getCommon().getWsHOST();
int port = BotApi.config.getCommon().getWsPORT();
String key = BotApi.config.getCommon().getKEY();
String host = BotApi.config.getCommon().getWsHost();
int port = BotApi.config.getCommon().getWsPort();
String key = BotApi.config.getCommon().getWsKey();
String toSend = "姬妻人服务状态:\n" +
"GO_CQHTTP服务器:" + host + ":" + port + "\n"
+ "全局服务状态:" + clientEnabled + "\n"
Expand Down
Loading

0 comments on commit 309a259

Please sign in to comment.