Skip to content

Commit

Permalink
First Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
WhitrayHB committed Jul 30, 2023
1 parent 9725608 commit d6d125e
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 44 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
val kotlinVersion = "1.6.10"
val kotlinVersion = "1.8.22"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion

Expand All @@ -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")
}
21 changes: 16 additions & 5 deletions src/main/java/top/nlrdev/mirai2mcsm/Mirai2MCSM.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
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());
}

@Override
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
}
}
6 changes: 3 additions & 3 deletions src/main/java/top/nlrdev/mirai2mcsm/configs/MCSMConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

public class MCSMConfig extends JavaAutoSavePluginConfig {
public final static MCSMConfig INSTANCE = new MCSMConfig();
public Value<String> getApiKey = value("APIKey","此处填入APIKey");
public Value<String> getApiUrl = value("APIUrl", "127.0.0.1:23333");
public Value<String> getApiKey = value("APIKey","");
public Value<String> getApiUrl = value("APIUrl", "//127.0.0.1:23333");

private MCSMConfig() {
super("Config");
super("MCSMConfig");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public class PluginConfig extends JavaAutoSavePluginConfig {
public final static PluginConfig INSTANCE = new PluginConfig();

private PluginConfig() {
super("Config");
super("PluginConfig");
}
}
21 changes: 21 additions & 0 deletions src/main/java/top/nlrdev/mirai2mcsm/utils/Locker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package top.nlrdev.mirai2mcsm.utils;

import java.util.concurrent.ConcurrentHashMap;

public class Locker {
private static final ConcurrentHashMap<Long, Long> coolDownMap = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<Long, Long> 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);
}
}
55 changes: 55 additions & 0 deletions src/main/java/top/nlrdev/mirai2mcsm/utils/RequestHandler.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
30 changes: 0 additions & 30 deletions src/main/java/top/nlrdev/mirai2mcsm/utils/RespondHandler.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.example.mirai.plugin.PluginMain
top.nlrdev.mirai2mcsm.Mirai2MCSM

0 comments on commit d6d125e

Please sign in to comment.