generated from project-mirai/mirai-console-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
122 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
src/main/java/top/nlrdev/mirai2mcsm/utils/RequestHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
src/main/java/top/nlrdev/mirai2mcsm/utils/RespondHandler.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
org.example.mirai.plugin.PluginMain | ||
top.nlrdev.mirai2mcsm.Mirai2MCSM |