Skip to content

Commit

Permalink
+ 添加#80 -1 增加前缀开关(也就是控制[]是否显示)
Browse files Browse the repository at this point in the history
+ 添加#80 -2 增加开关控制群转发至服务器的消息是否显示群号
+ 添加#80 -4 群消息转发至服务器显示群昵称而不是QQ昵称
+ 修复#81 -1 idPrefixOn失效,改为mcPrefixOn
+ 初步添加跨版本开发api
  • Loading branch information
cnlimiter committed May 7, 2023
1 parent 76f2838 commit 1570cd1
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 98 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ group = project.maven_group

repositories {
mavenCentral()
mavenLocal()
maven {
url 'https://maven.nova-committee.cn/releases'
}
Expand All @@ -16,6 +17,9 @@ repositories {
name = 'ParchmentMC'
url = 'https://maven.parchmentmc.org'
}
maven {
url "https://jitpack.io"
}
}

dependencies {
Expand All @@ -32,6 +36,9 @@ dependencies {
modImplementation include("cn.evolvefield.bot:OneBot-Client:${project.client_version}")
modImplementation include("blue.endless:jankson:${project.jankson_version}")

modApi include("com.github.Klotzi111:FabricMultiVersionHelper:${project.multi_version}")
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'

//modImplementation include("eu.pb4:placeholder-api:${project.placeholder_version}")
annotationProcessor "cn.evolvefield.bot:OneBot-Client:${project.client_version}"
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ loader_version=0.14.13
# Mod Properties
version_prefix=1.19.3
mod_version=2.1.5
client_version=0.3.4-light
client_version=0.3.5-light
jankson_version=1.2.1
parchement_version=2022.12.18
multi_version=1+

maven_group=cn.evolvefield.mods
archives_base_name=Bot-Connect-fabric
# Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.evolvefield.mods.botapi.api.cmd;

import cn.evolvefield.mods.botapi.BotApi;
import de.klotzi111.fabricmultiversionhelper.api.text.TextWrapper;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
Expand All @@ -23,10 +24,10 @@
*/
public class BotCmdRun extends CommandSourceStack {
public static BotCmdRun CUSTOM = new BotCmdRun(CommandSource.NULL, Vec3.ZERO, Vec2.ZERO, BotApi.SERVER.overworld(), 4,
"Bot", Component.literal("Bot"), BotApi.SERVER, null);
"Bot", TextWrapper.literal("Bot"), BotApi.SERVER, null);

public static BotCmdRun OP = new BotCmdRun(CommandSource.NULL, Vec3.ZERO, Vec2.ZERO, BotApi.SERVER.overworld(), 4,
"OP", Component.literal("OP"), BotApi.SERVER, null);
"OP", TextWrapper.literal("OP"), BotApi.SERVER, null);
public List<String> outPut = new ArrayList<>();

public BotCmdRun(CommandSource p_81302_, Vec3 p_81303_, Vec2 p_81304_, ServerLevel p_81305_, int p_81306_, String p_81307_, Component p_81308_, MinecraftServer p_81309_, @Nullable Entity p_81310_) {
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/cn/evolvefield/mods/botapi/api/cmd/CmdApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,28 @@ private static void GuildCmd(Bot bot, String guildId, String channelId, String c
}

public static void invokeCommandGroup(GroupMessageEvent event) {
String commandHead = event.getMessage().split(" ")[0].substring(1);
String command = event.getMessage().substring(1);//去除前缀

if (BotUtils.groupAdminParse(event)) {
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getCmdAlies().equals(commandHead))
.forEach(customCmd -> GroupCmd(BotApi.bot, event.getGroupId(), BotUtils.varParse(event, customCmd, command), true));//admin
.filter(customCmd -> command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(BotApi.bot, event.getGroupId(), BotUtils.varParse(customCmd, command), true));//admin
} else
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && customCmd.getCmdAlies().equals(commandHead))
.forEach(customCmd -> GroupCmd(BotApi.bot, event.getGroupId(), BotUtils.varParse(event, customCmd, command), false));
.filter(customCmd -> customCmd.getRequirePermission() < 1 && command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(BotApi.bot, event.getGroupId(), BotUtils.varParse(customCmd, command), false));

}

public static void invokeCommandGuild(GuildMessageEvent event) {
String commandHead = event.getMessage().split(" ")[0].substring(1);
String command = event.getMessage().substring(1);//去除前缀

if (BotUtils.guildAdminParse(event)) {
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getCmdAlies().equals(commandHead))
.forEach(customCmd -> GuildCmd(BotApi.bot, event.getGuildId(), event.getChannelId(), BotUtils.varParse(event, customCmd, command), true));//admin
.filter(customCmd -> command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(BotApi.bot, event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), true));//admin
} else CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && customCmd.getCmdAlies().equals(commandHead))
.forEach(customCmd -> GuildCmd(BotApi.bot, event.getGuildId(), event.getChannelId(), BotUtils.varParse(event, customCmd, command), false));
.filter(customCmd -> customCmd.getRequirePermission() < 1 && command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(BotApi.bot, event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), false));
}
}
23 changes: 8 additions & 15 deletions src/main/java/cn/evolvefield/mods/botapi/api/cmd/CustomCmd.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cn.evolvefield.mods.botapi.api.cmd;

import cn.evolvefield.onebot.sdk.util.json.JsonsObject;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;

/**
* Description:
Expand All @@ -12,8 +12,6 @@
*/
public class CustomCmd {

private final ResourceLocation id;

private final String cmdAlies;

private final String cmdContent;
Expand All @@ -22,32 +20,27 @@ public class CustomCmd {

private boolean enabled = true;

public CustomCmd(ResourceLocation id, String cmdAlies, String cmdContent, int requirePermission) {
this.id = id;
public CustomCmd(String cmdAlies, String cmdContent, int requirePermission) {
this.cmdAlies = cmdAlies;
this.cmdContent = cmdContent;
this.requirePermission = requirePermission;
}

public static CustomCmd loadFromJson(ResourceLocation id, JsonObject json) {
public static CustomCmd loadFromJson(JsonObject json) {

var alies = GsonHelper.getAsString(json, "alies");
var content = GsonHelper.getAsString(json, "content");
int role = GsonHelper.getAsInt(json, "role", 0);
var alies = JsonsObject.parse(json).optString("alies");
var content = JsonsObject.parse(json).optString("content");
int role = JsonsObject.parse(json).optInt("role", 0);

var cmd = new CustomCmd(id, alies, content, role);
var cmd = new CustomCmd(alies, content, role);

var enabled = GsonHelper.getAsBoolean(json, "enabled", true);
var enabled = JsonsObject.parse(json).optBool("enabled", true);

cmd.setEnabled(enabled);

return cmd;
}

public ResourceLocation getId() {
return id;
}

public String getCmdAlies() {
return cmdAlies;
}
Expand Down

This file was deleted.

59 changes: 40 additions & 19 deletions src/main/java/cn/evolvefield/mods/botapi/init/config/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,33 +107,54 @@ public static class Cmd {
"%Player% 不在线或者不存在哦\n" +
"还他妈不上线搁这玩QQ呢~";//暂时没用

@SerializedName("qqPrefix")
@SerializedName("gamePrefixOn")
@Comment("####################" +
"是否开启显示到游戏中的前缀")
private boolean gamePrefixOn = true;//是否开启显示到游戏中的前缀
@SerializedName("idGamePrefixOn")
@Comment("是否开启显示到游戏中的id前缀")
private boolean idGamePrefixOn = true;//是否开启显示到游戏中的id前缀
@SerializedName("qqGamePrefix")
@Comment("来自q群显示到游戏中的前缀")
private String qqPrefix = "群聊";//来自q群显示到游戏中的前缀
@SerializedName("guildPrefix")
private String qqGamePrefix = "群聊";//来自q群显示到游戏中的前缀
@SerializedName("guildGamePrefix")
@Comment("来自频道显示到游戏中的前缀")
private String guildPrefix = "频道";//来自频道显示到游戏中的前缀
private String guildGamePrefix = "频道";//来自频道显示到游戏中的前缀
@SerializedName("groupNickOn")
@Comment("是否开启显示到游戏中的昵称为群昵称")
private boolean groupNickOn = false;//是否开启显示到游戏中的昵称为群昵称


@SerializedName("####################" +
"mcPrefixOn")
@Comment("是否开启来自游戏的消息显示到群中的前缀")
private boolean mcPrefixOn = true;//是否开启来自游戏的消息显示到群中的前缀
@SerializedName("mcPrefix")
@Comment("来自游戏的消息前缀")
private String mcPrefix = "MC";//来自游戏的消息前缀
@SerializedName("mcChatPrefixEnable")
@Comment("游戏中系统命令发送的消息头")
private boolean mcChatPrefixEnable = false;//游戏中系统命令发送的消息头
@SerializedName("mcSystemPrefixEnable")
@Comment("游戏中自定义消息头")
private boolean mcSystemPrefixEnable = true;//游戏中自定义消息头
@SerializedName("qqChatPrefixEnable")
@Comment("qq中自定义消息头")
private boolean qqChatPrefixEnable = false;//qq中自定义消息头
@Comment("来自游戏的消息显示到群中的前缀")
private String mcPrefix = "MC";//来自游戏的消息显示到群中的前缀


@SerializedName("####################" +
"mcChatPrefixOn")
@Comment("是否开启游戏中自定义关键词")
private boolean mcChatPrefixOn = false;//是否开启游戏中自定义关键词
@SerializedName("qqChatPrefixOn")
@Comment("是否开启qq中自定义关键词")
private boolean qqChatPrefixOn = false;//是否开启qq中自定义关键词
@SerializedName("mcChatPrefix")
@Comment("游戏中自定义的消息头文本")
private String mcChatPrefix = "q";//游戏中自定义的消息头文本
@SerializedName("qqChatPrefix")
@Comment("qq中自定义的消息头文本")
private String qqChatPrefix = "m";//qq中自定义的消息头文本

@SerializedName("####################" +
"mcSystemPrefixOn")
@Comment("是否开启游戏中机器人执行命令发送的消息前缀")
private boolean mcSystemPrefixOn = true;//是否开启游戏中机器人执行命令发送的消息前缀
@SerializedName("mcSystemPrefix")
@Comment("游戏中系统命令发送的消息头文本")
private String mcSystemPrefix = "SERVER";//游戏中系统命令发送的消息头文本
@Comment("游戏中机器人执行命令发送的消息前缀")
private String mcSystemPrefix = "SERVER";//游戏中机器人执行命令发送的消息前缀

}

Expand Down Expand Up @@ -173,8 +194,8 @@ public static class Common {
@Comment("选择语言系统")
private String languageSelect = "zh_cn";//选择语言系统
@SerializedName("auto_open")
@Comment("自动重连")
private boolean autoOpen = true;//自动重连
@Comment("自动连接")
private boolean autoOpen = true;//自动连接
@SerializedName("image_on")
@Comment("是否开启聊天栏图片功能")
private boolean imageOn = true;//是否开启聊天栏图片功能
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,22 @@ public void onMessage(GroupMessageEvent event) {

String send = CQUtils.replace(event.getMessage());//暂时匹配仅符合字符串聊天内容与图片

if (ConfigHandler.cached().getCmd().isQqChatPrefixEnable()) {
if (ConfigHandler.cached().getCmd().isQqChatPrefixOn()) {
var split = event.getMessage().split(" ");
if (ConfigHandler.cached().getCmd().getQqChatPrefix().equals(split[0])) //指定前缀发送
send = split[1];
else return;
}
String toSend = String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ConfigHandler.cached().getCmd().getQqPrefix(), event.getGroupId(), event.getSender().getNickname(), send);
String nick = BotApi.bot.getGroupMemberInfo(event.getGroupId(), event.getUserId(), true).getData().getNickname();
String groupNick = ConfigHandler.cached().getCmd().isGroupNickOn() // 是否使用群昵称
? nick == null ? event.getSender().getNickname() : nick // 防止api返回为空
: event.getSender().getNickname();

String toSend = ConfigHandler.cached().getCmd().isGamePrefixOn()
? ConfigHandler.cached().getCmd().isIdGamePrefixOn()
? String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ConfigHandler.cached().getCmd().getQqGamePrefix(), event.getGroupId(), groupNick, send)
: String.format("§b[§l%s§b]§a<%s>§f %s", ConfigHandler.cached().getCmd().getQqGamePrefix(), groupNick, send)
: String.format("§a<%s>§f %s", groupNick, send);
TickEventHandler.getToSendQueue().add(toSend);
}
}
Expand Down Expand Up @@ -108,13 +117,23 @@ public void onMessage(GuildMessageEvent event) {
) {

String send = CQUtils.replace(event.getMessage());//暂时匹配仅符合字符串聊天内容与图片
if (ConfigHandler.cached().getCmd().isQqChatPrefixEnable()) {
if (ConfigHandler.cached().getCmd().isQqChatPrefixOn()) {
var split = event.getMessage().split(" ");
if (ConfigHandler.cached().getCmd().getQqChatPrefix().equals(split[0])) //指定前缀发送
send = split[1];
else return;
}
String toSend = String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ConfigHandler.cached().getCmd().getGuildPrefix(), event.getChannelId(), event.getSender().getNickname(), send);
String nick = BotApi.bot.getGuildMemberProfile(event.getGuildId(), String.valueOf(event.getUserId())).getData().getNickname();
String guildNick = ConfigHandler.cached().getCmd().isGroupNickOn()
? nick == null ? event.getSender().getNickname() : nick
: event.getSender().getNickname();


String toSend = ConfigHandler.cached().getCmd().isGamePrefixOn()
? ConfigHandler.cached().getCmd().isIdGamePrefixOn()
? String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ConfigHandler.cached().getCmd().getGuildGamePrefix(), event.getChannelId(), guildNick, send)
: String.format("§b[§l%s§b]§a<%s>§f %s", ConfigHandler.cached().getCmd().getGuildGamePrefix(), guildNick, send)
: String.format("§a<%s>§f %s", guildNick, send);
TickEventHandler.getToSendQueue().add(toSend);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ public static void init() {
for (String id : ConfigHandler.cached().getCommon().getChannelIdList())
BotApi.bot.sendGuildMsg(ConfigHandler.cached().getCommon().getGuildId(),
id,
String.format("[" + ConfigHandler.cached().getCmd().getMcPrefix() + "]<%s> %s",
String.format(ConfigHandler.cached().getCmd().isMcPrefixOn()
? "[" + ConfigHandler.cached().getCmd().getMcPrefix() + "]<%s> %s"
: "<%s> %s",
player.getDisplayName().getString(),
ConfigHandler.cached().getCmd().isMcChatPrefixEnable()
ConfigHandler.cached().getCmd().isMcChatPrefixOn()
&& ConfigHandler.cached().getCmd().getMcChatPrefix().equals(split[0]) ? split[1] : message));
} else {
for (long id : ConfigHandler.cached().getCommon().getGroupIdList())
BotApi.bot.sendGroupMsg(
id,
String.format("[" + ConfigHandler.cached().getCmd().getMcPrefix() + "]<%s> %s",
String.format(ConfigHandler.cached().getCmd().isMcPrefixOn()
? "[" + ConfigHandler.cached().getCmd().getMcPrefix() + "]<%s> %s"
: "<%s> %s",
player.getDisplayName().getString(),
ConfigHandler.cached().getCmd().isMcChatPrefixEnable()
ConfigHandler.cached().getCmd().isMcChatPrefixOn()
&& ConfigHandler.cached().getCmd().getMcChatPrefix().equals(split[0]) ? split[1] : message),
true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void init(File file) {
Path changed = (Path) event.context();
if (changed.endsWith(lastFile.getName()) && isLoaded()) {
if (reload())
Const.LOGGER.info("Config at " + lastFile.getAbsolutePath() + " has changed, reloaded!");
Const.LOGGER.info("已经重新加载位于" + lastFile.getCanonicalPath() + "的配置文件!");
}
}
checker.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,9 @@ private void loadFiles() {

try {
reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
var name = file.getName().replace(".json", "");
json = JsonParser.parseReader(reader).getAsJsonObject();

customCmd = CustomCmd.loadFromJson(new ResourceLocation(Const.MODID, name), json);
customCmd = CustomCmd.loadFromJson(json);

reader.close();
} catch (Exception e) {
Expand All @@ -127,8 +126,6 @@ private void loadFiles() {

if (customCmd != null && customCmd.isEnabled()) {
String alies = customCmd.getCmdAlies();
if (customCmd.getCmdContent().contains("%"))
alies = customCmd.getCmdContent().split("%")[0].stripTrailing();
Const.LOGGER.debug(alies);
this.customCmdMap.put(alies, customCmd);

Expand Down
Loading

0 comments on commit 1570cd1

Please sign in to comment.