Skip to content

Commit

Permalink
+ 支持消息链
Browse files Browse the repository at this point in the history
+ 支持Lagrange
  • Loading branch information
cnlimiter committed Nov 22, 2023
1 parent 62c604e commit d7125cc
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 132 deletions.
4 changes: 2 additions & 2 deletions fabric/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx8G

# Mod Properties
mod_version=2.1.8
mod_version=2.1.9
maven_group=cn.evole.mods
archives_base_name=McBot-fabric

Expand All @@ -11,5 +11,5 @@ archives_base_name=McBot-fabric
minecraft_version=1.20.1
loader_version=0.14.21
fabric_version=0.83.1+1.20.1
onebot_client_version=0.3.6-light
onebot_client_version=0.3.7
toml_version=0.1.1
32 changes: 32 additions & 0 deletions fabric/src/main/java/cn/evole/mods/mcbot/Const.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.evole.mods.mcbot;

import cn.evole.mods.mcbot.init.config.ModConfig;
import cn.evole.onebot.sdk.util.BotUtils;
import net.fabricmc.loader.api.FabricLoader;
import java.nio.file.Path;

Expand Down Expand Up @@ -31,4 +33,34 @@ public static boolean isLoad(String modId){
return FabricLoader.getInstance().isModLoaded(modId);
}

public static void sendGroupMsg(String message){
for (long id : ModConfig.INSTANCE.getCommon().getGroupIdList()){
groupMsg(id, message);
}
}

public static void groupMsg(long id, String message){
if (ModConfig.INSTANCE.getBotConfig().getMsgType().equalsIgnoreCase("string")){
McBot.bot.sendGroupMsg(id, message, false);
}
else {
McBot.bot.sendGroupMsg(id, BotUtils.rawToJson(message), false);
}
}

public static void sendGuildMsg(String message){
for (String id : ModConfig.INSTANCE.getCommon().getChannelIdList()){
guildMsg(ModConfig.INSTANCE.getCommon().getGuildId(), id, message);
}
}

public static void guildMsg(String guildId, String channelId, String message){
if (ModConfig.INSTANCE.getBotConfig().getMsgType().equalsIgnoreCase("string")){
McBot.bot.sendGuildMsg(guildId, channelId, message);
}
else {
McBot.bot.sendGuildMsg(guildId, channelId, BotUtils.rawToJson(message));
}
}

}
5 changes: 2 additions & 3 deletions fabric/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public void onServerStarted(MinecraftServer server) {
}, "BotServer");
app.start();
} catch (Exception e) {
Const.LOGGER.error("§c机器人服务端未配置或未打开");
Const.LOGGER.error("§c机器人服务端未配置或未打开");
}
}
bus = new EventBus(blockingQueue);//创建事件分发器
CustomCmdHandler.INSTANCE.load();//自定义命令加载
IBotEvent.init(bus);//事件监听s
IBotEvent.init(bus);//事件监听
}

public void onServerStopping(MinecraftServer server) {
Expand All @@ -110,7 +110,6 @@ public void onServerStopped(MinecraftServer server) {

private static void killOutThreads() {
try {
//保存配置
CustomCmdHandler.INSTANCE.clear();//自定义命令持久层清空
} catch (Exception e) {
e.printStackTrace();
Expand Down
18 changes: 9 additions & 9 deletions fabric/src/main/java/cn/evole/mods/mcbot/cmds/CmdApi.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.evole.mods.mcbot.cmds;

import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.init.handler.CustomCmdHandler;
import cn.evole.mods.mcbot.util.onebot.BotUtils;
import cn.evole.onebot.client.core.Bot;
import cn.evole.onebot.sdk.event.message.GroupMessageEvent;
import cn.evole.onebot.sdk.event.message.GuildMessageEvent;

Expand All @@ -29,12 +29,12 @@ private static StringBuilder CmdMain(String cmd, boolean isOp) {
return result;
}

private static void GroupCmd(Bot bot, long groupId, String cmd, boolean isOp) {
bot.sendGroupMsg(groupId, CmdMain(cmd, isOp).toString(), true);
private static void GroupCmd(long groupId, String cmd, boolean isOp) {
Const.groupMsg(groupId, CmdMain(cmd, isOp).toString());
}

private static void GuildCmd(Bot bot, String guildId, String channelId, String cmd, boolean isOp) {
bot.sendGuildMsg(guildId, channelId, CmdMain(cmd, isOp).toString());
private static void GuildCmd(String guildId, String channelId, String cmd, boolean isOp) {
Const.guildMsg(guildId, channelId, CmdMain(cmd, isOp).toString());
}

public static void invokeCommandGroup(GroupMessageEvent event) {
Expand All @@ -43,11 +43,11 @@ public static void invokeCommandGroup(GroupMessageEvent event) {
if (BotUtils.groupAdminParse(event)) {
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(McBot.bot, event.getGroupId(), BotUtils.varParse(customCmd, command), true));//admin
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, command), true));//admin
} else
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(McBot.bot, event.getGroupId(), BotUtils.varParse(customCmd, command), false));
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, command), false));

}

Expand All @@ -57,9 +57,9 @@ public static void invokeCommandGuild(GuildMessageEvent event) {
if (BotUtils.guildAdminParse(event)) {
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(McBot.bot, event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), true));//admin
.forEach(customCmd -> GuildCmd(event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), true));//admin
} else CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(McBot.bot, event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), false));
.forEach(customCmd -> GuildCmd(event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class BotConfig extends AutoLoadTomlConfig {
private boolean miraiHttp = false;
private boolean reconnect = true;
private int maxReconnectAttempts = 20;
private String msgType = "string";

public BotConfig() {
super(null);
Expand All @@ -34,6 +35,6 @@ public BotConfig(TomlTable source) {
}

public cn.evole.onebot.client.config.BotConfig toBot(){
return new cn.evole.onebot.client.config.BotConfig(url, token, botId, isAccessToken, miraiHttp, reconnect, maxReconnectAttempts);
return new cn.evole.onebot.client.config.BotConfig(url, token, botId, isAccessToken, miraiHttp, reconnect, maxReconnectAttempts, msgType);
}
}
30 changes: 17 additions & 13 deletions fabric/src/main/java/cn/evole/mods/mcbot/init/event/IBotEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.evole.mods.mcbot.init.event;

import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.cmds.CmdApi;
import cn.evole.mods.mcbot.init.config.ModConfig;
Expand All @@ -11,6 +12,7 @@
import cn.evole.onebot.sdk.event.meta.LifecycleMetaEvent;
import cn.evole.onebot.sdk.event.notice.group.GroupDecreaseNoticeEvent;
import cn.evole.onebot.sdk.event.notice.group.GroupIncreaseNoticeEvent;
import cn.evole.onebot.sdk.util.BotUtils;
import cn.evole.onebot.sdk.util.MsgUtils;
import lombok.val;

Expand Down Expand Up @@ -50,9 +52,10 @@ public void onMessage(GroupMessageEvent event) {
send = split[1];
else return;
}
String nick = McBot.bot.getGroupMemberInfo(event.getGroupId(), event.getUserId(), true).getData().getCard();
Const.LOGGER.info(send);
var nick = McBot.bot.getGroupMemberInfo(event.getGroupId(), event.getUserId(), true);
String groupNick = ModConfig.INSTANCE.getCmd().isGroupNickOn() // 是否使用群昵称
? nick == null ? event.getSender().getCard() : nick // 防止api返回为空
? nick == null ? event.getSender().getCard() : nick.getData().getCard() // 防止api返回为空
: event.getSender().getNickname();

String toSend = ModConfig.INSTANCE.getCmd().isGamePrefixOn()
Expand Down Expand Up @@ -88,8 +91,8 @@ public void onMessage(GroupIncreaseNoticeEvent event) {
if (ModConfig.INSTANCE.getCommon().getGroupIdList().contains(event.getGroupId())
&& ModConfig.INSTANCE.getStatus().isSEnable()
&& ModConfig.INSTANCE.getStatus().isSQqWelcomeEnable()) {

McBot.bot.sendGroupMsg(event.getGroupId(), MsgUtils.builder().at(event.getUserId()).build() + "\n" + ModConfig.INSTANCE.getCmd().getWelcomeNotice(), false);
var msg = MsgUtils.builder().at(event.getUserId()).build() + "\n" + ModConfig.INSTANCE.getCmd().getWelcomeNotice();
Const.groupMsg(event.getGroupId(), msg);
}
}
});
Expand All @@ -100,7 +103,9 @@ public void onMessage(GroupDecreaseNoticeEvent event) {
if (ModConfig.INSTANCE.getCommon().getGroupIdList().contains(event.getGroupId())
&& ModConfig.INSTANCE.getStatus().isSEnable()
&& ModConfig.INSTANCE.getStatus().isSQqLeaveEnable()) {
McBot.bot.sendGroupMsg(event.getGroupId(), MsgUtils.builder().text(String.valueOf(event.getUserId())).build() + "\n" +ModConfig.INSTANCE.getCmd().getLeaveNotice(), false);

var msg = MsgUtils.builder().text(String.valueOf(event.getUserId())).build() + "\n" +ModConfig.INSTANCE.getCmd().getLeaveNotice();
Const.groupMsg(event.getGroupId(), msg);
}
}
});
Expand All @@ -126,9 +131,9 @@ public void onMessage(GuildMessageEvent event) {
send = split[1];
else return;
}
String nick = McBot.bot.getGuildMemberProfile(event.getGuildId(), String.valueOf(event.getUserId())).getData().getNickname();
var nick = McBot.bot.getGuildMemberProfile(event.getGuildId(), String.valueOf(event.getUserId()));
String guildNick = ModConfig.INSTANCE.getCmd().isGroupNickOn()
? nick == null ? event.getSender().getNickname() : nick
? nick == null ? event.getSender().getNickname() : nick.getData().getNickname()
: event.getSender().getNickname();


Expand Down Expand Up @@ -167,15 +172,14 @@ public void onMessage(LifecycleMetaEvent event) {
if (!event.getSubType().equals("connect")) return;
if (!ModConfig.INSTANCE.getCommon().getGroupIdList().isEmpty()
) {
for (val id : ModConfig.INSTANCE.getCommon().getGroupIdList()){
McBot.bot.sendGroupMsg(id, "▌ 群服互联已连接 ┈━═☆", false);
}
var msg = "▌ 群服互联已连接 ┈━═☆";
Const.sendGroupMsg(msg);

}
if (!ModConfig.INSTANCE.getCommon().getChannelIdList().isEmpty()
) {
for (val id : ModConfig.INSTANCE.getCommon().getChannelIdList()){
McBot.bot.sendGuildMsg(ModConfig.INSTANCE.getCommon().getGuildId() , id, "▌ 群服互联已连接 ┈━═☆");
}
var msg = "▌ 群服互联已连接 ┈━═☆";
Const.sendGuildMsg(msg);
}
}
});
Expand Down
32 changes: 15 additions & 17 deletions fabric/src/main/java/cn/evole/mods/mcbot/init/event/IChatEvent.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cn.evole.mods.mcbot.init.event;

import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.Const;
import cn.evole.mods.mcbot.init.config.ModConfig;
import lombok.val;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;

/**
* Description:
Expand All @@ -23,26 +22,25 @@ public static void register(Player player, String message) {
&& !player.getCommandSenderWorld().isClientSide
) {
if (ModConfig.INSTANCE.getCommon().isGuildOn() && !ModConfig.INSTANCE.getCommon().getChannelIdList().isEmpty()) {
for (String id : ModConfig.INSTANCE.getCommon().getChannelIdList())
McBot.bot.sendGuildMsg(ModConfig.INSTANCE.getCommon().getGuildId(),
id,
String.format(ModConfig.INSTANCE.getCmd().isMcPrefixOn()
? "[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "]<%s> %s"
: "<%s> %s",
var msg = String.format(ModConfig.INSTANCE.getCmd().isMcPrefixOn()
? "[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "]<%s> %s"
: "<%s> %s",
player.getDisplayName().getString(),
ModConfig.INSTANCE.getCmd().isMcChatPrefixOn()
&& ModConfig.INSTANCE.getCmd().getMcChatPrefix().equals(split[0]) ? split[1] : message));
&& ModConfig.INSTANCE.getCmd().getMcChatPrefix().equals(split[0]) ? split[1] : message);

Const.sendGuildMsg(msg);

} else {
for (long id : ModConfig.INSTANCE.getCommon().getGroupIdList())
McBot.bot.sendGroupMsg(
id,
String.format(ModConfig.INSTANCE.getCmd().isMcPrefixOn()
? "[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "]<%s> %s"
: "<%s> %s",
var msg = String.format(ModConfig.INSTANCE.getCmd().isMcPrefixOn()
? "[" + ModConfig.INSTANCE.getCmd().getMcPrefix() + "]<%s> %s"
: "<%s> %s",
player.getDisplayName().getString(),
ModConfig.INSTANCE.getCmd().isMcChatPrefixOn()
&& ModConfig.INSTANCE.getCmd().getMcChatPrefix().equals(split[0]) ? split[1] : message),
false);
&& ModConfig.INSTANCE.getCmd().getMcChatPrefix().equals(split[0]) ? split[1] : message);

Const.sendGroupMsg(msg);

}


Expand Down
Loading

0 comments on commit d7125cc

Please sign in to comment.