Skip to content

Commit

Permalink
feat(mcbot): add local bot execution support
Browse files Browse the repository at this point in the history
- Add AppHandler class to handle OneBot app initialization and execution- Implement localExecute method in ConnectCommand to support local bot execution
- Update BotConfig to include URL and port for bot connection
- Add CompressUtils class to handle decompression of bot app files
- Implement DownloadUtils class to handle file downloads
- Update FileUtils to support resource directory management
- Add LgrConfig class for Lagrange configuration- Update various commands to remove unnecessary config saves
  • Loading branch information
cnlimiter committed Oct 27, 2024
1 parent f5fb026 commit e1f51d4
Show file tree
Hide file tree
Showing 27 changed files with 488 additions and 28 deletions.
4 changes: 3 additions & 1 deletion common/src/main/java/cn/evole/mods/mcbot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class Constants {
public static final ExecutorService cqExecutor = Executors.newSingleThreadExecutor();
public static final ExecutorService commonExecutor = Executors.newFixedThreadPool(4);
public static final Gson GSON = GsonUtils.getNullGson();
public static final Path CONFIG_FOLDER = FileUtils.checkFolder(Services.PLATFORM.getGamePath().resolve("mcbot"));
public static Path CONFIG_FOLDER = FileUtils.checkFolder(Services.PLATFORM.getGamePath().resolve("mcbot"));
public static Path APP_FOLDER = FileUtils.checkFolder(CONFIG_FOLDER.resolve("app"));
public static Path DATA_FOLDER = FileUtils.checkFolder(CONFIG_FOLDER.resolve("data"));

public static boolean isShutdown = false;
Expand All @@ -33,6 +34,7 @@ public class Constants {
public static MinecraftServer SERVER = null;
public static McBotCommandSource mcBotCommand = null;

public static final String LAGRANGE_URL = "https://github.com/LagrangeDev/Lagrange.Core/releases/download/nightly/";

public static void shutdown(){
cqExecutor.shutdownNow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public class ConnectApi {
public static void wsConnect() {
Constants.onebot.close();//关闭线程
if (Constants.onebot != null) Constants.onebot.close();//关闭线程
Constants.onebot = null;//强制为null
Constants.onebot = OneBotClient
.create(ModConfig.get().getBotConfig().build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
ModConfig.get().getCommon().addGroupId(id);
context.getSource().sendSuccess(() -> Component.literal("已成功添加QQ群号:" + id + "!"), true);
}
ModConfig.get().save();

return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
val id = context.getArgument("AuthKey", String.class);
ModConfig.get().getBotConfig().getToken().setValueFromString(id);
context.getSource().sendSuccess(() -> Component.literal("已设置框架的AuthKey为:" + id), true);
ModConfig.get().save();

return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
int id = context.getArgument("BotId", Integer.class);
ModConfig.get().getBotConfig().getBotId().setValueFromString(String.valueOf(id));
context.getSource().sendSuccess(() -> Component.literal("已设置机器人QQ号为:" + id + "!"), true);
ModConfig.get().save();

return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.evole.mods.mcbot.Constants;
import cn.evole.mods.mcbot.api.connect.ConnectApi;
import cn.evole.mods.mcbot.common.config.ModConfig;
import cn.evole.mods.mcbot.util.onebot.AppHandler;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import lombok.val;
Expand Down Expand Up @@ -37,11 +38,18 @@ public static int commonExecute(CommandContext<CommandSourceStack> context) {
return 1;
}

public static int localExecute(CommandContext<CommandSourceStack> context) {
AppHandler.init();
ModConfig.get().getBotConfig().getUrl().getDefaultStringValue();
doConnect(context);
return 1;
}

public static void doConnect(CommandContext<CommandSourceStack> context) {
if (!Constants.onebot.getWs().isOpen()) {
context.getSource().sendSuccess(() -> Component.literal("▌ " + ChatFormatting.LIGHT_PURPLE + "尝试链接框架"), true);
ConnectApi.wsConnect();
ModConfig.get().save();

} else {
context.getSource().sendSuccess(() -> Component.literal("▌ " + ChatFormatting.LIGHT_PURPLE + "已存在WS连接"), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
} else {
context.getSource().sendSuccess(() -> Component.literal("已关闭开发者模式"), true);
}
ModConfig.get().save();

return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
} else {
context.getSource().sendSuccess(() -> Component.literal("QQ群号:" + id + "并未出现!"), true);
}
ModConfig.get().save();

return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
context.getSource().sendSuccess(() -> Component.literal("WebSocket目前未连接"), true);
}
ModConfig.get().getCommon().getEnable().setBooleanValue(false);
ModConfig.get().save();

}
return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
out.append(s).append("\n");
}
context.getSource().sendSuccess(() -> Component.literal(out.toString()), true);
ModConfig.get().save();

return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
} else {
context.getSource().sendSuccess(() -> Component.literal("已关闭自动重连"), true);
}
ModConfig.get().save();

return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static int allExecute(CommandContext<CommandSourceStack> context) throws
} else {
context.getSource().sendSuccess(() -> Component.literal("全局接收群消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand All @@ -29,7 +29,7 @@ public static int chatExecute(CommandContext<CommandSourceStack> context) throws
} else {
context.getSource().sendSuccess(() -> Component.literal("接收群内聊天消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;

}
Expand All @@ -43,7 +43,7 @@ public static int cmdExecute(CommandContext<CommandSourceStack> context) throws
} else {
context.getSource().sendSuccess(() -> Component.literal("接收群内命令消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}
}
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
} catch (Exception e) {
context.getSource().sendSuccess(() -> Component.literal("重载配置失败"), true);
}
ModConfig.get().save();

return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static int qqLeaveExecute(CommandContext<CommandSourceStack> context) thr
} else {
context.getSource().sendSuccess(() -> Component.literal("发送离开QQ群的消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand All @@ -31,7 +31,7 @@ public static int qqWelcomeExecute(CommandContext<CommandSourceStack> context) t
} else {
context.getSource().sendSuccess(() -> Component.literal("发送新人加入QQ群的消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand All @@ -43,7 +43,7 @@ public static int allExecute(CommandContext<CommandSourceStack> context) throws
} else {
context.getSource().sendSuccess(() -> Component.literal("全局发送消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand All @@ -56,7 +56,7 @@ public static int joinExecute(CommandContext<CommandSourceStack> context) throws
} else {
context.getSource().sendSuccess(() -> Component.literal("发送玩家加入游戏消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand All @@ -69,7 +69,7 @@ public static int leaveExecute(CommandContext<CommandSourceStack> context) throw
} else {
context.getSource().sendSuccess(() -> Component.literal("发送玩家离开游戏消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand All @@ -82,7 +82,7 @@ public static int deathExecute(CommandContext<CommandSourceStack> context) throw
} else {
context.getSource().sendSuccess(() -> Component.literal("发送玩家死亡游戏消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand All @@ -95,7 +95,7 @@ public static int chatExecute(CommandContext<CommandSourceStack> context) throws
} else {
context.getSource().sendSuccess(() -> Component.literal("发送玩家聊天游戏消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand All @@ -108,7 +108,7 @@ public static int achievementsExecute(CommandContext<CommandSourceStack> context
} else {
context.getSource().sendSuccess(() -> Component.literal("发送玩家成就游戏消息开关已被设置为关闭"), true);
}
ModConfig.get().save();

return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static int execute(CommandContext<CommandSourceStack> context) throws Com
+ "发送群成员进群消息状态:" + sQqWelcomeEnabled + "\n"
+ "发送群成员退群消息状态:" + sQqLeaveEnabled + "\n";
context.getSource().sendSuccess(() -> Component.literal(toSend), true);
ModConfig.get().save();

return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Setter
public class BotConfig extends AutoInitConfigContainer.AutoInitConfigCategoryBase {
public ConfigString tag = new ConfigString("config.mcbot.bot.tag", "main", "跨服支持,权限支持");
public ConfigString url = new ConfigString("config.mcbot.bot.url", "127.0.0.1", "地址(支持域名和ipv6)");
public ConfigString url = new ConfigString("config.mcbot.bot.url", "127.0.0.1:18082", "地址(支持域名和ipv6)");
public ConfigString token = new ConfigString("config.mcbot.bot.token", "", "鉴权");
public ConfigString botId = new ConfigString("config.mcbot.bot.botId", "0", "机器人qq");
public ConfigBoolean reconnect = new ConfigBoolean("config.mcbot.bot.reconnect", true, "自动重连");
Expand All @@ -30,7 +30,10 @@ public BotConfig() {
}

public cn.evole.onebot.client.core.BotConfig build() {
return new cn.evole.onebot.client.core.BotConfig(url.getStringValue(), token.getStringValue(), Long.parseLong(botId.getStringValue()), token.getStringValue().startsWith("mirai_"), reconnect.getBooleanValue(), maxReconnectAttempts.getIntegerValue());
return new cn.evole.onebot.client.core.BotConfig(
url.getStringValue().startsWith("ws://") ? url.getStringValue() : "ws://" + url.getStringValue()
, token.getStringValue(), Long.parseLong(botId.getStringValue()), token.getStringValue().startsWith("mirai_"),
reconnect.getBooleanValue(), maxReconnectAttempts.getIntegerValue());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cn.evole.mods.mcbot.common.config;

import com.iafenvoy.jupiter.config.AutoInitConfigContainer;

/**
* @Project: McBot
* @Author: cnlimiter
* @CreateTime: 2024/10/28 00:12
* @Description:
*/
public class LgrConfig extends AutoInitConfigContainer.AutoInitConfigCategoryBase{
public LgrConfig() {
super("lgr", "config.mcbot.category.lgr");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
.requires(source -> source.hasPermission(2))
.then(Commands.literal("connect")
.executes(ConnectCommand::commonExecute)
.then(Commands.literal("local").executes(ConnectCommand::localExecute))
.then(Commands.argument("parameter", StringArgumentType.greedyString())
.executes(ConnectCommand::execute)
)
Expand Down
Loading

0 comments on commit e1f51d4

Please sign in to comment.