From d6d125e2b3cd9aae3608ed8843949a230ec317aa Mon Sep 17 00:00:00 2001 From: WhitrayHB <2039851887@qq.com> Date: Sun, 30 Jul 2023 18:25:40 +0800 Subject: [PATCH] First Commit --- build.gradle.kts | 6 +- .../top/nlrdev/mirai2mcsm/Mirai2MCSM.java | 21 +++++-- .../mirai2mcsm/commands/QueryPanelStatus.java | 23 +++++++- .../nlrdev/mirai2mcsm/configs/MCSMConfig.java | 6 +- .../mirai2mcsm/configs/PluginConfig.java | 2 +- .../top/nlrdev/mirai2mcsm/utils/Locker.java | 21 +++++++ .../mirai2mcsm/utils/RequestHandler.java | 55 +++++++++++++++++++ .../mirai2mcsm/utils/RespondHandler.java | 30 ---------- ...t.mamoe.mirai.console.plugin.jvm.JvmPlugin | 2 +- 9 files changed, 122 insertions(+), 44 deletions(-) create mode 100644 src/main/java/top/nlrdev/mirai2mcsm/utils/Locker.java create mode 100644 src/main/java/top/nlrdev/mirai2mcsm/utils/RequestHandler.java delete mode 100644 src/main/java/top/nlrdev/mirai2mcsm/utils/RespondHandler.java diff --git a/build.gradle.kts b/build.gradle.kts index 374e63c..c6287fe 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - val kotlinVersion = "1.6.10" + val kotlinVersion = "1.8.22" kotlin("jvm") version kotlinVersion kotlin("plugin.serialization") version kotlinVersion @@ -17,6 +17,6 @@ repositories { } dependencies { - implementation("org.json:json:20220924") - compileOnly("net.mamoe:mirai-core-jvm:2.13.4") + implementation("org.json:json:20230618") + compileOnly("net.mamoe:mirai-core-jvm:2.15.0") } \ No newline at end of file diff --git a/src/main/java/top/nlrdev/mirai2mcsm/Mirai2MCSM.java b/src/main/java/top/nlrdev/mirai2mcsm/Mirai2MCSM.java index 3164030..60910e1 100644 --- a/src/main/java/top/nlrdev/mirai2mcsm/Mirai2MCSM.java +++ b/src/main/java/top/nlrdev/mirai2mcsm/Mirai2MCSM.java @@ -1,21 +1,27 @@ package top.nlrdev.mirai2mcsm; +import net.mamoe.mirai.console.command.CommandManager; import net.mamoe.mirai.console.plugin.jvm.JavaPlugin; -import net.mamoe.mirai.internal.deps.okhttp3.*; import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder; import net.mamoe.mirai.internal.deps.okhttp3.OkHttpClient; +import top.nlrdev.mirai2mcsm.commands.QueryPanelStatus; import top.nlrdev.mirai2mcsm.configs.MCSMConfig; +import top.nlrdev.mirai2mcsm.configs.PluginConfig; import java.util.concurrent.TimeUnit; public final class Mirai2MCSM extends JavaPlugin { public static final Mirai2MCSM INSTANCE = new Mirai2MCSM(); - public static final OkHttpClient globalHttpClient = new OkHttpClient.Builder().connectTimeout(3, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).build(); + public static final OkHttpClient globalHttpClient = new OkHttpClient.Builder() + .connectTimeout(3, TimeUnit.SECONDS) + .readTimeout(10, TimeUnit.SECONDS).build(); private Mirai2MCSM() { super(new JvmPluginDescriptionBuilder("top.nlrdev.mirai2mcsm", "0.1.0") - .info("EG") + .name("Mirai2MCSM") + .info("一个提供Mirai对接MCSM管理面板的管理插件") + .author("NLR DevTeam") .build()); } @@ -23,20 +29,25 @@ private Mirai2MCSM() { public void onEnable() { getLogger().info("初始化插件中..."); initialize(); + getLogger().info("初始化插件完毕"); } public void initialize() { refreshRemoteInfo(); reloadConfigs(); registerCommands(); + if(MCSMConfig.INSTANCE.getApiKey.get()==""){ + getLogger().error("未填写APIKey!请在/config/top.nlrdev.mirai2mcsm/mcsmconfig"); + } } public void refreshRemoteInfo(){ } public void reloadConfigs(){ - + reloadPluginConfig(MCSMConfig.INSTANCE); + reloadPluginConfig(PluginConfig.INSTANCE); } public void registerCommands(){ - + CommandManager.INSTANCE.registerCommand(QueryPanelStatus.INSTANCE,true); } } diff --git a/src/main/java/top/nlrdev/mirai2mcsm/commands/QueryPanelStatus.java b/src/main/java/top/nlrdev/mirai2mcsm/commands/QueryPanelStatus.java index 28f1eb0..042cf0b 100644 --- a/src/main/java/top/nlrdev/mirai2mcsm/commands/QueryPanelStatus.java +++ b/src/main/java/top/nlrdev/mirai2mcsm/commands/QueryPanelStatus.java @@ -2,9 +2,16 @@ import net.mamoe.mirai.console.command.CommandSender; import net.mamoe.mirai.console.command.java.JRawCommand; +import net.mamoe.mirai.internal.deps.okhttp3.Request; +import net.mamoe.mirai.message.data.Message; import net.mamoe.mirai.message.data.MessageChain; +import net.mamoe.mirai.message.data.MessageChainBuilder; import org.jetbrains.annotations.NotNull; +import org.json.JSONObject; import top.nlrdev.mirai2mcsm.Mirai2MCSM; +import top.nlrdev.mirai2mcsm.configs.MCSMConfig; +import top.nlrdev.mirai2mcsm.configs.PluginConfig; +import top.nlrdev.mirai2mcsm.utils.RequestHandler; public class QueryPanelStatus extends JRawCommand { public static final QueryPanelStatus INSTANCE = new QueryPanelStatus(); @@ -18,6 +25,20 @@ private QueryPanelStatus() { @Override public void onCommand(@NotNull CommandSender sender, @NotNull MessageChain args) { - + String url = MCSMConfig.INSTANCE.getApiUrl.get()+"/api/overview?apikey="+MCSMConfig.INSTANCE.getApiKey.get(); + Request request = new Request.Builder().url(url).get().build(); + JSONObject json = RequestHandler.handleRequest(request); + JSONObject data = null; + if (json == null||json.getInt("status")!=200) { + sender.sendMessage("状态:运行异常\n返回值:"+json.getInt("status")); + return; + } + data = json.getJSONObject("data"); + MessageChainBuilder messageChainBuilder = new MessageChainBuilder() + .append("状态:正常运行\n") + .append("面板版本:").append(data.getString("version")).append("\n") + .append("面板系统CPU负载:").append(String.valueOf(data.getJSONObject("process").getFloat("cpu"))).append("\n") + .append(""); + sender.sendMessage(messageChainBuilder.build()); } } diff --git a/src/main/java/top/nlrdev/mirai2mcsm/configs/MCSMConfig.java b/src/main/java/top/nlrdev/mirai2mcsm/configs/MCSMConfig.java index e710257..2b850a3 100644 --- a/src/main/java/top/nlrdev/mirai2mcsm/configs/MCSMConfig.java +++ b/src/main/java/top/nlrdev/mirai2mcsm/configs/MCSMConfig.java @@ -5,10 +5,10 @@ public class MCSMConfig extends JavaAutoSavePluginConfig { public final static MCSMConfig INSTANCE = new MCSMConfig(); - public Value getApiKey = value("APIKey","此处填入APIKey"); - public Value getApiUrl = value("APIUrl", "127.0.0.1:23333"); + public Value getApiKey = value("APIKey",""); + public Value getApiUrl = value("APIUrl", "//127.0.0.1:23333"); private MCSMConfig() { - super("Config"); + super("MCSMConfig"); } } diff --git a/src/main/java/top/nlrdev/mirai2mcsm/configs/PluginConfig.java b/src/main/java/top/nlrdev/mirai2mcsm/configs/PluginConfig.java index 691cb59..7d50082 100644 --- a/src/main/java/top/nlrdev/mirai2mcsm/configs/PluginConfig.java +++ b/src/main/java/top/nlrdev/mirai2mcsm/configs/PluginConfig.java @@ -7,6 +7,6 @@ public class PluginConfig extends JavaAutoSavePluginConfig { public final static PluginConfig INSTANCE = new PluginConfig(); private PluginConfig() { - super("Config"); + super("PluginConfig"); } } \ No newline at end of file diff --git a/src/main/java/top/nlrdev/mirai2mcsm/utils/Locker.java b/src/main/java/top/nlrdev/mirai2mcsm/utils/Locker.java new file mode 100644 index 0000000..2966042 --- /dev/null +++ b/src/main/java/top/nlrdev/mirai2mcsm/utils/Locker.java @@ -0,0 +1,21 @@ +package top.nlrdev.mirai2mcsm.utils; + +import java.util.concurrent.ConcurrentHashMap; + +public class Locker { + private static final ConcurrentHashMap coolDownMap = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap lockTimeMap = new ConcurrentHashMap<>(); + + public static synchronized void lock(long uid, long lockTime) { + coolDownMap.put(uid, System.currentTimeMillis()); + lockTimeMap.put(uid, lockTime); + } + + public static synchronized boolean isLocked(long uid) { + if (!coolDownMap.containsKey(uid) || !lockTimeMap.containsKey(uid)) { + return false; + } + + return (System.currentTimeMillis() - coolDownMap.get(uid)) <= lockTimeMap.get(uid); + } +} diff --git a/src/main/java/top/nlrdev/mirai2mcsm/utils/RequestHandler.java b/src/main/java/top/nlrdev/mirai2mcsm/utils/RequestHandler.java new file mode 100644 index 0000000..a3a880e --- /dev/null +++ b/src/main/java/top/nlrdev/mirai2mcsm/utils/RequestHandler.java @@ -0,0 +1,55 @@ +package top.nlrdev.mirai2mcsm.utils; + +import net.mamoe.mirai.internal.deps.okhttp3.OkHttpClient; +import net.mamoe.mirai.internal.deps.okhttp3.Request; +import net.mamoe.mirai.internal.deps.okhttp3.Response; +import net.mamoe.mirai.utils.MiraiLogger; +import org.json.JSONObject; +import top.nlrdev.mirai2mcsm.Mirai2MCSM; + +import java.io.IOException; + +public class RequestHandler { + private static MiraiLogger logger = Mirai2MCSM.INSTANCE.getLogger(); + private static OkHttpClient httpClient = Mirai2MCSM.globalHttpClient; + public RequestHandler INSTANCE = this; + + + /** + * 请求返回处理 + * @return JsonObject + */ + public static JSONObject handleRequest(Request request){ + Response response = null; + try{ + response = httpClient.newCall(request).execute(); + }catch (RuntimeException | IOException e){ + logger.error("请求发起失败"); + throw new RuntimeException(e); + } + if(response.body() == null){ + logger.error("请求返回为空"); + return null; + } + System.out.println(response); + JSONObject json = null; + try { + System.out.println(response.body().string()); + json = new JSONObject(response.body().string()); + } catch (IOException e) { + throw new RuntimeException(e); + } + switch (json.getString("status")) { + case"400": + logger.error("请求参数不正确"); + return null; + case"403": + logger.error("无权限"); + return null; + case"500": + logger.error("内部服务器错误"); + return null; + } + return json; + } +} diff --git a/src/main/java/top/nlrdev/mirai2mcsm/utils/RespondHandler.java b/src/main/java/top/nlrdev/mirai2mcsm/utils/RespondHandler.java deleted file mode 100644 index 30e198b..0000000 --- a/src/main/java/top/nlrdev/mirai2mcsm/utils/RespondHandler.java +++ /dev/null @@ -1,30 +0,0 @@ -package top.nlrdev.mirai2mcsm.utils; - -import net.mamoe.mirai.internal.deps.okhttp3.OkHttpClient; -import net.mamoe.mirai.utils.MiraiLogger; -import org.json.JSONObject; -import top.nlrdev.mirai2mcsm.Mirai2MCSM; - -public class RespondHandler { - private MiraiLogger logger = Mirai2MCSM.INSTANCE.getLogger(); - private OkHttpClient httpClient = Mirai2MCSM.globalHttpClient; - public JSONObject handleRespond(String respond){ - if(respond==null){ - logger.error("请求返回为空"); - return null; - } - JSONObject json = new JSONObject(respond); - switch (json.getString("status")) { - case"400": - logger.error("请求参数不正确"); - return null; - case"403": - logger.error("无权限"); - return null; - case"500": - logger.error("内部服务器错误"); - return null; - } - return json; - } -} diff --git a/src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin b/src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin index 0bed70b..d63674a 100644 --- a/src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin +++ b/src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin @@ -1 +1 @@ -org.example.mirai.plugin.PluginMain \ No newline at end of file +top.nlrdev.mirai2mcsm.Mirai2MCSM \ No newline at end of file