Skip to content

Commit

Permalink
Merge pull request #137 from A0000Xz/fabric
Browse files Browse the repository at this point in the history
添加了严格指令校验
  • Loading branch information
cnlimiter authored Feb 20, 2024
2 parents ddde41b + 630a14f commit fa3acd8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
30 changes: 20 additions & 10 deletions fabric/src/main/java/cn/evole/mods/mcbot/cmds/CmdApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class CmdApi {
private static StringBuilder CmdMain(String cmd, boolean isOp) {
StringBuilder result = new StringBuilder();
//#if MC >= 11900
//$$ McBot.SERVER.getCommands().performPrefixedCommand(isOp ? BotCmdRun.OP : BotCmdRun.CUSTOM, cmd);//优雅
McBot.SERVER.getCommands().performPrefixedCommand(isOp ? BotCmdRun.OP : BotCmdRun.CUSTOM, cmd);//优雅
//#else
McBot.SERVER.getCommands().performCommand(isOp ? BotCmdRun.OP : BotCmdRun.CUSTOM, cmd);
//$$ McBot.SERVER.getCommands().performCommand(isOp ? BotCmdRun.OP : BotCmdRun.CUSTOM, cmd);
//#endif
for (String s : (isOp ? BotCmdRun.OP.outPut : BotCmdRun.CUSTOM.outPut)) {
result.append(s.replaceAll(\\S", "")).append("\n");
Expand All @@ -39,15 +39,25 @@ private static void GuildCmd(String guildId, String channelId, String cmd, boole

public static void invokeCommandGroup(GroupMessageEvent event) {
String command = event.getMessage().substring(1);//去除前缀
String origincommand = command;
command = BotUtils.cmdParse(command);
String performedcommand = command;

if (BotUtils.groupAdminParse(event)) {
if (performedcommand.equals("list")) {
// 如果指令包含list,则强行以非管理员身份执行
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> command.contains(customCmd.getCmdAlies()))
.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(event.getGroupId(), BotUtils.varParse(customCmd, command), false));
.filter(customCmd -> customCmd.getRequirePermission() < 1 && performedcommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, origincommand), false));
}else{
if (BotUtils.groupAdminParse(event)) {
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> performedcommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, origincommand), true));//admin
} else
CustomCmdHandler.INSTANCE.getCustomCmds().stream()
.filter(customCmd -> customCmd.getRequirePermission() < 1 && performedcommand.equals(customCmd.getCmdAlies()))
.forEach(customCmd -> GroupCmd(event.getGroupId(), BotUtils.varParse(customCmd, origincommand), false));
}

}

Expand All @@ -62,4 +72,4 @@ public static void invokeCommandGuild(GuildMessageEvent event) {
.filter(customCmd -> customCmd.getRequirePermission() < 1 && command.contains(customCmd.getCmdAlies()))
.forEach(customCmd -> GuildCmd(event.getGuildId(), event.getChannelId(), BotUtils.varParse(customCmd, command), false));
}
}
}
12 changes: 12 additions & 0 deletions fabric/src/main/java/cn/evole/mods/mcbot/util/onebot/BotUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ private static int getSubStr(String str) {
// 查找字符出现的个数 = (原字符串长度 - 替换后的字符串长度)/要查找的字符串长度
return (str.length() - destStr.length()) / "%".length();
}
public static String cmdParse(String command) {
// 找到最后一个空格的位置
int lastSpaceIndex = command.lastIndexOf(" ");

// 如果没有空格,则整个命令就是关键词
if (lastSpaceIndex == -1) {
return command;
}

// 返回最后一个空格之前的内容
return command.substring(0, lastSpaceIndex);
}
}

0 comments on commit fa3acd8

Please sign in to comment.