-
-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[初稿] 将MohistAI项目中的QQ SDK内置到 Mohist-1.20.1+中 #3313
- Loading branch information
Showing
16 changed files
with
496 additions
and
5 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
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,70 @@ | ||
package com.mohistmc.ai.koukou; | ||
|
||
import com.mohistmc.plugins.config.MohistPluginConfig; | ||
import java.io.File; | ||
import java.util.List; | ||
|
||
public class AIConfig extends MohistPluginConfig { | ||
|
||
public static AIConfig INSTANCE; | ||
|
||
public AIConfig(File file) { | ||
super(file); | ||
} | ||
|
||
public static void init() { | ||
INSTANCE = new AIConfig(new File("mohist-config", "qq.yml")); | ||
INSTANCE.yaml.addDefault("enable", false); | ||
INSTANCE.yaml.addDefault("debug", false); | ||
INSTANCE.yaml.addDefault("server_name", "群消息"); | ||
INSTANCE.yaml.addDefault("post_server", "http://0.0.0.0:3000"); | ||
INSTANCE.yaml.addDefault("http_server.hostname", "0.0.0.0"); | ||
INSTANCE.yaml.addDefault("http_server.port", 2025); | ||
INSTANCE.yaml.addDefault("chat_post_group", List.of("123456789")); | ||
INSTANCE.yaml.addDefault("command.enable", false); | ||
INSTANCE.yaml.addDefault("command.owners", List.of("123456789")); | ||
INSTANCE.yaml.addDefault("command.name", "执行"); | ||
INSTANCE.save(); | ||
} | ||
|
||
public boolean enable() { | ||
return yaml.getBoolean("enable", false); | ||
} | ||
|
||
public boolean debug() { | ||
return yaml.getBoolean("debug", false); | ||
} | ||
|
||
public String post_server() { | ||
return yaml.getString("post_server", "http://0.0.0.0:3000"); | ||
} | ||
|
||
public String http_server_hostname() { | ||
return yaml.getString("http_server.hostname", "0.0.0.0"); | ||
} | ||
|
||
public int http_server_port() { | ||
return yaml.getInt("http_server.port", 2025); | ||
} | ||
|
||
public List<String> chat_post_group() { | ||
return yaml.getStringList("chat_post_group"); | ||
} | ||
|
||
public boolean command_enable() { | ||
return yaml.getBoolean("command.enable"); | ||
} | ||
|
||
public List<String> command_owners() { | ||
return yaml.getStringList("command.owners"); | ||
} | ||
|
||
public String command_name() { | ||
return yaml.getString("command.name", "执行"); | ||
} | ||
|
||
public String server_name() { | ||
return yaml.getString("server_name", "群消息"); | ||
} | ||
|
||
} |
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,34 @@ | ||
package com.mohistmc.ai.koukou; | ||
|
||
import com.mohistmc.ai.koukou.network.MyHttpHandler; | ||
import com.mohistmc.ai.koukou.network.event.ListenRegister; | ||
import com.sun.net.httpserver.HttpServer; | ||
import java.net.InetSocketAddress; | ||
import java.util.concurrent.Executors; | ||
import lombok.SneakyThrows; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class ApiController { | ||
|
||
public static ListenRegister eventBus = ListenRegister.getInstance(); | ||
public static Logger LOGGER = LogManager.getLogger("Mohist Http Server"); | ||
|
||
public static void init() { | ||
if (!AIConfig.INSTANCE.enable()) return; | ||
ApiController api = new ApiController(); | ||
api.start(); | ||
eventBus.registerListener(new KouKouPostListener()); | ||
} | ||
|
||
@SneakyThrows | ||
public void start() { | ||
var host = AIConfig.INSTANCE.http_server_hostname(); | ||
var port = AIConfig.INSTANCE.http_server_port(); | ||
HttpServer server = HttpServer.create(new InetSocketAddress(host, port), 0); | ||
server.createContext("/", new MyHttpHandler()); | ||
server.setExecutor(Executors.newFixedThreadPool(5)); | ||
server.start(); | ||
LOGGER.info("已部署AI服务 {}:{}", host, port); | ||
} | ||
} |
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,45 @@ | ||
package com.mohistmc.ai.koukou; | ||
|
||
import com.mohistmc.ai.koukou.network.HttpRequestUtils; | ||
import com.mohistmc.mjson.Json; | ||
import java.util.HashMap; | ||
import java.util.Objects; | ||
import lombok.SneakyThrows; | ||
|
||
public class KouKou { | ||
|
||
public static void sendToGroup(String message) { | ||
if (!AIConfig.INSTANCE.enable()) return; | ||
for (String groupId : AIConfig.INSTANCE.chat_post_group()) { | ||
send_group_msg(String.valueOf(groupId), message); | ||
} | ||
} | ||
|
||
public static void sendToGroup(String groupId, String message) { | ||
if (!AIConfig.INSTANCE.enable()) return; | ||
send_group_msg(String.valueOf(groupId), message); | ||
} | ||
|
||
@SneakyThrows | ||
public static void send_group_msg(String group_id, String message) { | ||
HashMap<String, String> param = new HashMap<>(); | ||
param.put("group_id", group_id); | ||
param.put("message", message); | ||
var string = HttpRequestUtils.post("/send_group_msg", param); | ||
if (string == null) { | ||
debug("string == null"); | ||
return; | ||
} | ||
debug(string); | ||
var json = Json.read(string); | ||
if (Objects.equals(json.asString("status"), "failed")) { | ||
debug("发送失败"); | ||
return; | ||
} | ||
debug("返回数据: " + json); | ||
} | ||
|
||
public static void debug(String debug_message) { | ||
if (AIConfig.INSTANCE.debug()) ApiController.LOGGER.info(debug_message); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/mohistmc/ai/koukou/KouKouPostListener.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,47 @@ | ||
package com.mohistmc.ai.koukou; | ||
|
||
import com.mohistmc.ai.koukou.entity.MessageRequest; | ||
import com.mohistmc.ai.koukou.network.event.BaseListener; | ||
import com.mohistmc.ai.koukou.network.event.HttpPostEvent; | ||
import com.mohistmc.mjson.Json; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import lombok.SneakyThrows; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.craftbukkit.v1_20_R1.command.ColouredConsoleSender; | ||
|
||
public class KouKouPostListener implements BaseListener { | ||
|
||
@SneakyThrows | ||
public void onEvent(HttpPostEvent event) { | ||
Json json = event.getJson(); | ||
if (event.isQQ()) { | ||
MessageRequest request = json.asBean(MessageRequest.class); | ||
String t = request.getMessage_type(); | ||
var group = request.getGroup_id(); | ||
String msg = request.getRaw_message(); | ||
String sender_nickname = request.getSender().getNickname(); | ||
if (t != null && t.equals("group")) { | ||
if (AIConfig.INSTANCE.chat_post_group().contains(String.valueOf(group))) { | ||
if (AIConfig.INSTANCE.command_enable() && AIConfig.INSTANCE.command_owners().contains(String.valueOf(request.getUser_id()))) { | ||
String label = AIConfig.INSTANCE.command_name(); | ||
if (msg.startsWith(label)) { | ||
String cmd = msg.replace(label + " ", ""); | ||
Bukkit.dispatchCommand(new ColouredConsoleSender().group(group), cmd); | ||
return; | ||
} | ||
} | ||
msg = msg.replaceAll("§\\S", ""); | ||
msg = msg.replace("__color__", "§"); | ||
String pattern = "\\[CQ:.*?]"; | ||
Pattern r = Pattern.compile(pattern); | ||
Matcher m = r.matcher(msg); | ||
if (!m.find()) { | ||
Bukkit.broadcastMessage("[%s] <%s>: %s".formatted(AIConfig.INSTANCE.server_name(), sender_nickname, msg)); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/com/mohistmc/ai/koukou/entity/MessageRequest.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,76 @@ | ||
package com.mohistmc.ai.koukou.entity; | ||
|
||
import com.mohistmc.mjson.ToJson; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class MessageRequest { | ||
@ToJson | ||
private long self_id; | ||
@ToJson | ||
private long user_id; | ||
@ToJson | ||
private long time; | ||
@ToJson | ||
private long message_id; | ||
@ToJson | ||
private long real_id; | ||
@ToJson | ||
private String message_type; | ||
@ToJson | ||
private Sender sender; | ||
@ToJson | ||
private String raw_message; | ||
@ToJson | ||
private long font; | ||
@ToJson | ||
private String sub_type; | ||
@ToJson | ||
private Message[] message; | ||
@ToJson | ||
private String message_format; | ||
@ToJson | ||
private String post_type; | ||
@ToJson | ||
private long group_id; | ||
@ToJson | ||
private String card_new; | ||
|
||
@lombok.Data | ||
public static class Message { | ||
@ToJson | ||
private Data data; | ||
@ToJson | ||
private String type; | ||
} | ||
|
||
@lombok.Data | ||
public static class Data { | ||
@ToJson | ||
private String text; | ||
@ToJson | ||
private String id; | ||
@ToJson | ||
private String file; | ||
@ToJson | ||
private String file_id; | ||
@ToJson | ||
private String url; | ||
@ToJson | ||
private String file_size; | ||
} | ||
|
||
@lombok.Data | ||
public static class Sender { | ||
@ToJson | ||
private long user_id; | ||
@ToJson | ||
private String nickname; | ||
@ToJson | ||
private String card; | ||
@ToJson | ||
private String role; | ||
} | ||
} | ||
|
||
|
23 changes: 23 additions & 0 deletions
23
src/main/java/com/mohistmc/ai/koukou/network/HttpRequestUtils.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,23 @@ | ||
package com.mohistmc.ai.koukou.network; | ||
|
||
import com.mohistmc.ai.koukou.AIConfig; | ||
import com.mohistmc.mjson.Json; | ||
import java.util.Map; | ||
import kong.unirest.core.HttpResponse; | ||
import kong.unirest.core.Unirest; | ||
import lombok.SneakyThrows; | ||
|
||
public class HttpRequestUtils { | ||
|
||
@SneakyThrows | ||
public static String post(String path, Map<String, String> body) { | ||
var json = Json.read(body); | ||
String url = AIConfig.INSTANCE.post_server() + path; | ||
HttpResponse<String> response = Unirest.post(url) | ||
.header("User-Agent", "Mohist") | ||
.header("Content-Type", "application/json") | ||
.body(json.toString()) | ||
.asString(); | ||
return response.getBody(); | ||
} | ||
} |
Oops, something went wrong.