feat: adding list command and send command

fix: fix NotifySubType Error
This commit is contained in:
wzp 2024-08-30 00:33:09 +08:00
parent ad89fc4d1f
commit 448167d021
13 changed files with 289 additions and 38 deletions

5
.idea/misc.xml generated
View File

@ -5,9 +5,10 @@
<groovy codeStyle="LEGACY" /> <groovy codeStyle="LEGACY" />
</component> </component>
<component name="EntryPointsManager"> <component name="EntryPointsManager">
<list size="2"> <list size="3">
<item index="0" class="java.lang.String" itemvalue="cn.wzpmc.api.plugins.event.EventHandler" /> <item index="0" class="java.lang.String" itemvalue="cn.wzpmc.api.plugins.event.EventHandler" />
<item index="1" class="java.lang.String" itemvalue="com.alibaba.fastjson2.annotation.JSONField" /> <item index="1" class="java.lang.String" itemvalue="cn.wzpmc.plugins.event.EventHandler" />
<item index="2" class="java.lang.String" itemvalue="com.alibaba.fastjson2.annotation.JSONField" />
</list> </list>
</component> </component>
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />

View File

@ -14,7 +14,7 @@ allprojects {
apply(plugin = "java") apply(plugin = "java")
val groupName by extra("cn.wzpmc") val groupName by extra("cn.wzpmc")
val projectArtifactId by extra("my-bot") val projectArtifactId by extra("my-bot")
val projectVersion by extra("1.0.1") val projectVersion by extra("1.0.2")
repositories { repositories {
mavenCentral() mavenCentral()
maven("https://libraries.minecraft.net") maven("https://libraries.minecraft.net")

View File

@ -1,5 +1,7 @@
package cn.wzpmc.api; package cn.wzpmc.api;
import lombok.SneakyThrows;
/** /**
* 主Api接口 * 主Api接口
* *
@ -20,4 +22,19 @@ public interface IMainApi {
* @since 2024/8/23 21:32 v0.0.5-dev * @since 2024/8/23 21:32 v0.0.5-dev
*/ */
<REQUEST, RESPONSE> ActionResponse<RESPONSE> doApiCall(Action<REQUEST, RESPONSE> packet) throws InterruptedException; <REQUEST, RESPONSE> ActionResponse<RESPONSE> doApiCall(Action<REQUEST, RESPONSE> packet) throws InterruptedException;
/**
* 进行请求操作
*
* @param packet 请求包
* @param <REQUEST> 请求类型
* @param <RESPONSE> 返回类型
* @return 请求返回包
* @author wzp
* @since 2024/8/27 15:11 v1.0.2
*/
@SneakyThrows
default <REQUEST, RESPONSE> ActionResponse<RESPONSE> doApiCallSafe(Action<REQUEST, RESPONSE> packet) {
return doApiCall(packet);
}
} }

View File

@ -3,7 +3,6 @@ package cn.wzpmc.api.actions.message.get;
import cn.wzpmc.api.Action; import cn.wzpmc.api.Action;
import cn.wzpmc.api.Actions; import cn.wzpmc.api.Actions;
import cn.wzpmc.entities.FriendInformation; import cn.wzpmc.entities.FriendInformation;
import cn.wzpmc.entities.MessageInformation;
import java.util.List; import java.util.List;

View File

@ -0,0 +1,24 @@
package cn.wzpmc.events.notice.notify;
import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 用户正在输入事件
*
* @author wzp
* @version 1.0.2
* @since 2024/8/29 21:52
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class InputStatusNotifyEvent extends NotifyEvent {
/**
* 状态文本
*
* @since 2024/8/29 23:16 v1.0.2
*/
@JSONField(name = "status_text")
private String statusText;
}

View File

@ -27,7 +27,14 @@ public enum NotifySubType {
* *
* @since 2024/8/1 下午10:16 v0.0.2-dev * @since 2024/8/1 下午10:16 v0.0.2-dev
*/ */
HONOR(HonorNotifyEvent.class); HONOR(HonorNotifyEvent.class),
/**
* 用户正在输入事件
*
* @since 2024/8/29 21:52 v1.0.2
*/
INPUT_STATUS(InputStatusNotifyEvent.class);
public final Class<? extends NotifyEvent> clazz; public final Class<? extends NotifyEvent> clazz;
NotifySubType(Class<? extends NotifyEvent> clazz) { NotifySubType(Class<? extends NotifyEvent> clazz) {

View File

@ -84,4 +84,15 @@ public interface CommandSender {
default boolean isAdmin() { default boolean isAdmin() {
return this.getPermissions().isAdmin(); return this.getPermissions().isAdmin();
} }
/**
* 检查指令发送者是否为完整权限管理员
*
* @return 是否为管理员
* @author wzp
* @since 2024/8/27 15:05 v1.0.2
*/
default boolean isFullAdmin() {
return this instanceof IBot || IBot.getInstance().isBotOp(this.getId());
}
} }

View File

@ -72,7 +72,14 @@ public class GroupCommandSender extends GroupUser implements CommandSender {
JsonMessage jsonMessage = new JsonMessage(); JsonMessage jsonMessage = new JsonMessage();
List<JsonMessagePart> messageParts = jsonMessage.getMessageParts(); List<JsonMessagePart> messageParts = jsonMessage.getMessageParts();
messageParts.add(new At(this.getId())); messageParts.add(new At(this.getId()));
messageParts.add(StringMessage.text(" " + messageComponent.toMessageString())); messageParts.add(StringMessage.text(" "));
if (messageComponent instanceof StringMessage) {
messageParts.add((StringMessage) messageComponent);
}
if (messageComponent instanceof JsonMessage) {
List<JsonMessagePart> jsonMessageParts = ((JsonMessage) messageComponent).getMessageParts();
messageParts.addAll(jsonMessageParts);
}
SendGroupMessageAction sendGroupMessageAction = new SendGroupMessageAction(this.groupId, jsonMessage); SendGroupMessageAction sendGroupMessageAction = new SendGroupMessageAction(this.groupId, jsonMessage);
mainApi.doApiCall(sendGroupMessageAction); mainApi.doApiCall(sendGroupMessageAction);
} }

View File

@ -2,10 +2,7 @@ package cn.wzpmc;
import cn.wzpmc.api.IMainApi; import cn.wzpmc.api.IMainApi;
import cn.wzpmc.api.actions.message.get.GetLoginInfoAction; import cn.wzpmc.api.actions.message.get.GetLoginInfoAction;
import cn.wzpmc.builtin.commands.DeOpCommand; import cn.wzpmc.builtin.commands.*;
import cn.wzpmc.builtin.commands.HelpCommand;
import cn.wzpmc.builtin.commands.OpCommand;
import cn.wzpmc.builtin.commands.StopCommand;
import cn.wzpmc.builtin.event.CommandEventHandler; import cn.wzpmc.builtin.event.CommandEventHandler;
import cn.wzpmc.configuration.Configuration; import cn.wzpmc.configuration.Configuration;
import cn.wzpmc.console.MyBotConsole; import cn.wzpmc.console.MyBotConsole;
@ -96,12 +93,6 @@ public class Main {
} }
load.onLoad(); load.onLoad();
} }
// 注册内置指令
CommandManager commandManager = myBot.getCommandManager();
commandManager.registerCommand(new StopCommand(myBot));
commandManager.registerCommand(new OpCommand());
commandManager.registerCommand(new HelpCommand());
commandManager.registerCommand(new DeOpCommand());
} }
public static WebSocketConnectionHandler createConnection(MyBot myBot, URI uri) { public static WebSocketConnectionHandler createConnection(MyBot myBot, URI uri) {
@ -137,6 +128,14 @@ public class Main {
// 获取Bot消息 // 获取Bot消息
mainApi.doApiCall(new GetLoginInfoAction()); mainApi.doApiCall(new GetLoginInfoAction());
myBot.registerEventHandler(new CommandEventHandler()); myBot.registerEventHandler(new CommandEventHandler());
// 注册内置指令
CommandManager commandManager = myBot.getCommandManager();
commandManager.registerCommand(new StopCommand(myBot));
commandManager.registerCommand(new OpCommand());
commandManager.registerCommand(new HelpCommand());
commandManager.registerCommand(new DeOpCommand());
commandManager.registerCommand(new ListCommand());
commandManager.registerCommand(new SendCommand());
startConsole(myBot, webSocketConnectionHandler); startConsole(myBot, webSocketConnectionHandler);
} }
} }

View File

@ -8,7 +8,9 @@ import cn.wzpmc.plugins.CommandManager;
import cn.wzpmc.user.CommandSender; import cn.wzpmc.user.CommandSender;
import cn.wzpmc.user.IBot; import cn.wzpmc.user.IBot;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode; import com.mojang.brigadier.tree.RootCommandNode;
@ -22,6 +24,13 @@ import java.util.concurrent.ConcurrentHashMap;
* @since 2024/8/25 15:07 * @since 2024/8/25 15:07
*/ */
public class HelpCommand implements BrigadierCommand { public class HelpCommand implements BrigadierCommand {
private final CommandManager commandManager;
public HelpCommand() {
IBot instance = MyBot.getInstance();
this.commandManager = (CommandManager) instance.getCommandManager();
}
private static void handlerNode(Collection<CommandNode<CommandSender>> node, int tabCount, StringBuilder builder) { private static void handlerNode(Collection<CommandNode<CommandSender>> node, int tabCount, StringBuilder builder) {
for (CommandNode<CommandSender> commandSenderCommandNode : node) { for (CommandNode<CommandSender> commandSenderCommandNode : node) {
builder.append("\t".repeat(Math.max(0, tabCount))); builder.append("\t".repeat(Math.max(0, tabCount)));
@ -31,29 +40,57 @@ public class HelpCommand implements BrigadierCommand {
} }
} }
private Collection<CommandNode<CommandSender>> getRootCommandNode() {
CommandDispatcher<CommandSender> dispatcher = this.commandManager.getDispatcher();
RootCommandNode<CommandSender> root = dispatcher.getRoot();
return root.getChildren();
}
@Override @Override
public LiteralArgumentBuilder<CommandSender> getCommandNode() { public LiteralArgumentBuilder<CommandSender> getCommandNode() {
return LiteralArgumentBuilder.<CommandSender>literal("help").executes(e -> { return LiteralArgumentBuilder.<CommandSender>literal("help").
IBot instance = MyBot.getInstance(); executes(e -> {
CommandManager commandManager = (CommandManager) instance.getCommandManager(); Collection<CommandNode<CommandSender>> children = getRootCommandNode();
CommandDispatcher<CommandSender> dispatcher = commandManager.getDispatcher(); CommandSender source = e.getSource();
RootCommandNode<CommandSender> root = dispatcher.getRoot(); for (CommandNode<CommandSender> child : children) {
Collection<CommandNode<CommandSender>> children = root.getChildren(); StringBuilder builder = new StringBuilder();
CommandSender source = e.getSource(); builder.append('/');
for (CommandNode<CommandSender> child : children) { builder.append(child.getUsageText());
StringBuilder builder = new StringBuilder(); builder.append('\n');
builder.append('\n'); handlerNode(child.getChildren(), 1, builder);
builder.append('/'); builder.deleteCharAt(builder.lastIndexOf("\n"));
builder.append(child.getUsageText()); source.sendMessage(StringMessage.text(builder.toString()));
builder.append('\n'); }
handlerNode(child.getChildren(), 1, builder); ConcurrentHashMap<String, RawCommand> rawCommands = this.commandManager.getRawCommands();
source.sendMessage(StringMessage.text(builder.toString())); for (Map.Entry<String, RawCommand> stringRawCommandEntry : rawCommands.entrySet()) {
} source.sendMessage(StringMessage.text("/" + stringRawCommandEntry.getKey()));
ConcurrentHashMap<String, RawCommand> rawCommands = commandManager.getRawCommands(); }
for (Map.Entry<String, RawCommand> stringRawCommandEntry : rawCommands.entrySet()) { return 0;
source.sendMessage(StringMessage.text("/" + stringRawCommandEntry.getKey())); }).
} then(RequiredArgumentBuilder.<CommandSender, String>
return 0; argument("root", StringArgumentType.word()).
}); executes(e -> {
String root = e.getArgument("root", String.class);
Collection<CommandNode<CommandSender>> children = getRootCommandNode();
CommandSender source = e.getSource();
for (CommandNode<CommandSender> child : children) {
if (!child.getName().equals(root)) {
continue;
}
StringBuilder builder = new StringBuilder();
builder.append('/');
builder.append(child.getUsageText());
builder.append('\n');
handlerNode(child.getChildren(), 1, builder);
builder.deleteCharAt(builder.lastIndexOf("\n"));
source.sendMessage(StringMessage.text(builder.toString()));
}
ConcurrentHashMap<String, RawCommand> rawCommands = this.commandManager.getRawCommands();
for (Map.Entry<String, RawCommand> stringRawCommandEntry : rawCommands.entrySet()) {
source.sendMessage(StringMessage.text("/" + stringRawCommandEntry.getKey()));
}
return 0;
})
);
} }
} }

View File

@ -0,0 +1,84 @@
package cn.wzpmc.builtin.commands;
import cn.wzpmc.api.ActionResponse;
import cn.wzpmc.api.IMainApi;
import cn.wzpmc.api.actions.message.get.GetFriendListAction;
import cn.wzpmc.api.actions.message.get.GetGroupListAction;
import cn.wzpmc.commands.BrigadierCommand;
import cn.wzpmc.entities.FriendInformation;
import cn.wzpmc.entities.GroupInformation;
import cn.wzpmc.message.StringMessage;
import cn.wzpmc.user.CommandSender;
import cn.wzpmc.user.IBot;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import java.util.List;
import java.util.Objects;
/**
* @author wzp
* @version 1.0.2
* @since 2024/8/27 14:59
*/
public class ListCommand implements BrigadierCommand {
private final IMainApi api;
public ListCommand() {
api = IBot.getInstance().getMainApi();
}
@Override
public LiteralArgumentBuilder<CommandSender> getCommandNode() {
return LiteralArgumentBuilder.<CommandSender>literal("list").
requires(CommandSender::isFullAdmin).
executes(e -> {
StringBuilder builder = new StringBuilder();
buildFriendList(builder);
buildGroupList(builder);
CommandSender source = e.getSource();
source.sendMessage(StringMessage.text(builder.toString()));
return 0;
}).
then(LiteralArgumentBuilder.<CommandSender>literal("user").
executes(e -> {
StringBuilder builder = new StringBuilder();
buildFriendList(builder);
CommandSender source = e.getSource();
source.sendMessage(StringMessage.text(builder.toString()));
return 0;
})
).then(LiteralArgumentBuilder.<CommandSender>literal("group").
executes(e -> {
StringBuilder builder = new StringBuilder();
buildGroupList(builder);
CommandSender source = e.getSource();
source.sendMessage(StringMessage.text(builder.toString()));
return 0;
})
);
}
private void buildFriendList(StringBuilder builder) {
ActionResponse<List<FriendInformation>> listActionResponse = api.doApiCallSafe(new GetFriendListAction());
List<FriendInformation> data = listActionResponse.getData();
builder.append("好友:").append('\n');
for (FriendInformation friendInformation : data) {
String name = friendInformation.getName();
builder.append(name);
String nickname = friendInformation.getNickname();
if (!Objects.equals(nickname, "") && !Objects.equals(nickname, name)) {
builder.append('[').append(nickname).append(']');
}
builder.append('(').append(friendInformation.getId()).append(')').append('\n');
}
}
private void buildGroupList(StringBuilder builder) {
ActionResponse<List<GroupInformation>> groupListResponse = api.doApiCallSafe(new GetGroupListAction());
List<GroupInformation> data = groupListResponse.getData();
builder.append("群聊:").append('\n');
for (GroupInformation groupInformation : data) {
builder.append(groupInformation.getGroupName()).append('(').append(groupInformation.getGroupId()).append(')').append('\n');
}
}
}

View File

@ -0,0 +1,62 @@
package cn.wzpmc.builtin.commands;
import cn.wzpmc.api.IMainApi;
import cn.wzpmc.api.actions.message.send.SendGroupMessageAction;
import cn.wzpmc.api.actions.message.send.SendPrivateMessageAction;
import cn.wzpmc.commands.BrigadierCommand;
import cn.wzpmc.message.StringMessage;
import cn.wzpmc.user.CommandSender;
import cn.wzpmc.user.IBot;
import com.mojang.brigadier.arguments.LongArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
/**
* @author wzp
* @version 1.0.2
* @since 2024/8/27 14:54
*/
public class SendCommand implements BrigadierCommand {
private final IMainApi api;
public SendCommand() {
IBot instance = IBot.getInstance();
this.api = instance.getMainApi();
}
@Override
public LiteralArgumentBuilder<CommandSender> getCommandNode() {
return LiteralArgumentBuilder.<CommandSender>literal("send")
.then(LiteralArgumentBuilder.<CommandSender>
literal("group").
then(RequiredArgumentBuilder.<CommandSender, Long>argument("id", LongArgumentType.longArg()).
then(RequiredArgumentBuilder.<CommandSender, String>argument("content", StringArgumentType.greedyString()).
executes(e -> {
CommandSender source = e.getSource();
Long id = e.getArgument("id", Long.class);
String content = e.getArgument("content", String.class);
api.doApiCallSafe(new SendGroupMessageAction(id, StringMessage.text(content)));
source.sendMessage(StringMessage.text("发送成功!"));
return 0;
})
)
)
)
.then(LiteralArgumentBuilder.<CommandSender>
literal("user").
then(RequiredArgumentBuilder.<CommandSender, Long>argument("id", LongArgumentType.longArg()).
then(RequiredArgumentBuilder.<CommandSender, String>argument("content", StringArgumentType.greedyString()).
executes(e -> {
CommandSender source = e.getSource();
Long id = e.getArgument("id", Long.class);
String content = e.getArgument("content", String.class);
api.doApiCallSafe(new SendPrivateMessageAction(id, StringMessage.text(content)));
source.sendMessage(StringMessage.text("发送成功!"));
return 0;
})
)
)
);
}
}

View File

@ -25,6 +25,9 @@ public class StopCommand implements BrigadierCommand {
public LiteralArgumentBuilder<CommandSender> getCommandNode() { public LiteralArgumentBuilder<CommandSender> getCommandNode() {
return LiteralArgumentBuilder.<CommandSender>literal("stop") return LiteralArgumentBuilder.<CommandSender>literal("stop")
.requires(e -> { .requires(e -> {
if (e instanceof IBot) {
return true;
}
Ops ops = bot.getOps(); Ops ops = bot.getOps();
Set<Long> admins = ops.getAdmins(); Set<Long> admins = ops.getAdmins();
return admins.contains(e.getId()); return admins.contains(e.getId());