Skip to content

Commit

Permalink
feat: 所有参数解析支持定制message提示
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmeng-qwq committed Aug 27, 2024
1 parent 738a2a1 commit 7a6b061
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@

import dev.rollczi.litecommands.LiteCommandsBuilder;
import dev.rollczi.litecommands.LiteCommandsFactory;
import dev.rollczi.litecommands.LiteCommandsInternal;
import dev.rollczi.litecommands.extension.annotations.LiteAnnotationsProcessorExtension;
import dev.rollczi.litecommands.handler.result.ResultHandler;
import dev.rollczi.litecommands.handler.result.ResultHandlerChain;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.InvokedMessage;
import dev.rollczi.litecommands.processor.LiteBuilderProcessor;
import snw.jkook.Core;
import snw.jkook.HttpAPI;
import snw.jkook.command.CommandException;
import snw.jkook.command.CommandSender;
import snw.jkook.command.ConsoleCommandSender;
import snw.jkook.entity.CustomEmoji;
Expand Down Expand Up @@ -79,15 +86,16 @@ public static <B extends LiteCommandsBuilder<CommandSender, LiteKookSettings, B>
.context(User.class, new KookOnlyUserContextual<>("只有用户才能执行该命令"))
.context(ConsoleCommandSender.class, new KookOnlyConsoleContextual<>("只有后台才能执行该命令"))

.argument(User.class, new UserArgument(httpAPI))
.argument(Guild.class, new GuildArgument(httpAPI))
.argument(Channel.class, new ChannelArgument<>(httpAPI))
.argument(NonCategoryChannel.class, new ChannelArgument<>(httpAPI))
.argument(TextChannel.class, new ChannelArgument<>(httpAPI))
.argument(VoiceChannel.class, new ChannelArgument<>(httpAPI))
.selfProcessor((builder, internal) ->
builder.argument(User.class, new UserArgument(httpAPI, internal.getMessageRegistry()))
.argument(Guild.class, new GuildArgument(httpAPI, internal.getMessageRegistry()))
.argument(Channel.class, new ChannelArgument<>(httpAPI, internal.getMessageRegistry()))
.argument(NonCategoryChannel.class, new ChannelArgument<>(httpAPI, internal.getMessageRegistry()))
.argument(TextChannel.class, new ChannelArgument<>(httpAPI, internal.getMessageRegistry()))
.argument(VoiceChannel.class, new ChannelArgument<>(httpAPI, internal.getMessageRegistry()))

.argument(Role.class, new RoleArgument(client))
.argument(CustomEmoji.class, new EmojiArgument(client))
.argument(Role.class, new RoleArgument(client, internal.getMessageRegistry()))
.argument(CustomEmoji.class, new EmojiArgument(client, internal.getMessageRegistry())))

.result(String.class, new ReplyResultHandler<>())
.result(CardComponent.class, new ReplyResultHandler<>())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageKey;
import dev.rollczi.litecommands.message.MessageRegistry;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import snw.jkook.HttpAPI;
Expand All @@ -31,10 +33,14 @@
import snw.jkook.entity.channel.Channel;

public class ChannelArgument<T extends Channel> extends ArgumentResolver<CommandSender, T> {
public static final MessageKey<String> CHANNEL_NOT_FOUND = MessageKey.of("channel_not_found", "Channel not found");

private final HttpAPI httpAPI;
private final MessageRegistry<CommandSender> messageRegistry;

public ChannelArgument(HttpAPI httpAPI) {
public ChannelArgument(HttpAPI httpAPI, MessageRegistry<CommandSender> messageRegistry) {
this.httpAPI = httpAPI;
this.messageRegistry = messageRegistry;
}

@SuppressWarnings("unchecked")
Expand All @@ -50,7 +56,7 @@ protected ParseResult<T> parse(Invocation<CommandSender> invocation, Argument<T>
try {
Channel channel = httpAPI.getChannel(input);
if (channel == null) {
return ParseResult.failure(new CommandException("Channel not found"));
return ParseResult.failure(messageRegistry.getInvoked(CHANNEL_NOT_FOUND, invocation, argument));
}
return ParseResult.success((T) channel);
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageKey;
import dev.rollczi.litecommands.message.MessageRegistry;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import snw.jkook.command.CommandException;
import snw.jkook.command.CommandSender;
import snw.jkook.command.ConsoleCommandSender;
import snw.jkook.entity.CustomEmoji;
import snw.jkook.entity.Guild;
import snw.jkook.message.ChannelMessage;
Expand All @@ -38,10 +41,21 @@
import java.util.Set;

public class EmojiArgument extends ArgumentResolver<CommandSender, CustomEmoji> {
public static final MessageKey<String> EMOJI_NOT_FOUND = MessageKey.of("emoji_not_found", "Emoji not found");
public static final MessageKey<Message> NOT_CHANNEL = MessageKey.of("emoji_not_channel", "Not supporting finding emoji");
public static final MessageKey<CommandSender> SENDER_UNSUPPORTED = MessageKey.of("emoji_sender_unsupported", sender -> {
if (sender instanceof ConsoleCommandSender) {
return "Unsupported console command";
}
return "Unsupported command";
});

private final KBCClient client;
private final MessageRegistry<CommandSender> messageRegistry;

public EmojiArgument(KBCClient client) {
public EmojiArgument(KBCClient client, MessageRegistry<CommandSender> messageRegistry) {
this.client = client;
this.messageRegistry = messageRegistry;
}

@Override
Expand All @@ -63,7 +77,7 @@ protected ParseResult<CustomEmoji> parse(Invocation<CommandSender> invocation, A
try {
Optional<Message> optional = invocation.context().get(Message.class);
if (!optional.isPresent()) {
return ParseResult.failure(new CommandException("Unsupported argument: " + argument));
return ParseResult.failure(messageRegistry.getInvoked(SENDER_UNSUPPORTED, invocation, invocation.sender()));
}
Message message = optional.get();
if (message instanceof ChannelMessage) {
Expand All @@ -85,13 +99,13 @@ protected ParseResult<CustomEmoji> parse(Invocation<CommandSender> invocation, A
}
}
if (emoji == null) {
return ParseResult.failure(new CommandException("CustomEmoji not found"));
return ParseResult.failure(messageRegistry.getInvoked(EMOJI_NOT_FOUND, invocation, argument));
}
return ParseResult.success(emoji);
}
return ParseResult.failure(new CommandException("Unsupported argument: " + argument));
return ParseResult.failure(messageRegistry.getInvoked(NOT_CHANNEL, invocation, message));
} catch (final Exception e) {
return ParseResult.failure(new CommandException("CustomEmoji not found"));
return ParseResult.failure(new CommandException("CustomEmoji not found", e));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageKey;
import dev.rollczi.litecommands.message.MessageRegistry;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import snw.jkook.HttpAPI;
Expand All @@ -30,10 +32,14 @@
import snw.jkook.entity.Guild;

public class GuildArgument extends ArgumentResolver<CommandSender, Guild> {
public static final MessageKey<String> GUILD_NOT_FOUND = MessageKey.of("guild_not_found", "Guild not found");

private final HttpAPI httpAPI;
private final MessageRegistry<CommandSender> messageRegistry;

public GuildArgument(HttpAPI httpAPI) {
public GuildArgument(HttpAPI httpAPI, MessageRegistry<CommandSender> messageRegistry) {
this.httpAPI = httpAPI;
this.messageRegistry = messageRegistry;
}

@Override
Expand All @@ -42,11 +48,11 @@ protected ParseResult<Guild> parse(Invocation<CommandSender> invocation, Argumen
try {
Guild guild = httpAPI.getGuild(input);
if (guild == null) {
return ParseResult.failure(new CommandException("Guild not found"));
return ParseResult.failure(messageRegistry.getInvoked(GUILD_NOT_FOUND, invocation, argument));
}
return ParseResult.success(guild);
} catch (final Exception e) {
return ParseResult.failure(new CommandException("Guild not found"));
return ParseResult.failure(new CommandException("Guild not found", e));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageKey;
import dev.rollczi.litecommands.message.MessageRegistry;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import snw.jkook.command.CommandException;
import snw.jkook.command.CommandSender;
import snw.jkook.command.ConsoleCommandSender;
import snw.jkook.entity.Guild;
import snw.jkook.entity.Role;
import snw.jkook.entity.channel.Channel;
import snw.jkook.message.ChannelMessage;
import snw.jkook.message.Message;
import snw.jkook.util.PageIterator;
Expand All @@ -37,10 +41,21 @@
import java.util.Set;

public class RoleArgument extends ArgumentResolver<CommandSender, Role> {
public static final MessageKey<String> ROLE_NOT_FOUND = MessageKey.of("role_not_found", "User not found");
public static final MessageKey<Message> NOT_CHANNEL = MessageKey.of("role_not_channel", "Not supporting finding role");
public static final MessageKey<CommandSender> SENDER_UNSUPPORTED = MessageKey.of("role_sender_unsupported", sender -> {
if (sender instanceof ConsoleCommandSender) {
return "Unsupported console command";
}
return "Unsupported command";
});

private final KBCClient client;
private final MessageRegistry<CommandSender> messageRegistry;

public RoleArgument(KBCClient client) {
public RoleArgument(KBCClient client, MessageRegistry<CommandSender> messageRegistry) {
this.client = client;
this.messageRegistry = messageRegistry;
}

@Override
Expand All @@ -57,7 +72,7 @@ protected ParseResult<Role> parse(Invocation<CommandSender> invocation, Argument
try {
Optional<Message> optional = invocation.context().get(Message.class);
if (!optional.isPresent()) {
return ParseResult.failure(new CommandException("Unsupported argument: " + argument));
return ParseResult.failure(messageRegistry.getInvoked(SENDER_UNSUPPORTED, invocation, invocation.sender()));
}
Message message = optional.get();
if (message instanceof ChannelMessage) {
Expand All @@ -81,11 +96,11 @@ protected ParseResult<Role> parse(Invocation<CommandSender> invocation, Argument
}
}
if (role == null) {
return ParseResult.failure(new CommandException("Role not found"));
return ParseResult.failure(messageRegistry.getInvoked(ROLE_NOT_FOUND, invocation, argument));
}
return ParseResult.success(role);
}
return ParseResult.failure(new CommandException("Unsupported argument: " + argument));
return ParseResult.failure(messageRegistry.getInvoked(NOT_CHANNEL, invocation, message));
} catch (final Exception e) {
return ParseResult.failure(new CommandException("Role not found", e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.message.MessageKey;
import dev.rollczi.litecommands.message.MessageRegistry;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import snw.jkook.HttpAPI;
Expand All @@ -30,10 +32,14 @@
import snw.jkook.entity.User;

public class UserArgument extends ArgumentResolver<CommandSender, User> {
public static final MessageKey<String> USER_NOT_FOUND = MessageKey.of("user_not_found", "User not found");

private final HttpAPI httpAPI;
private final MessageRegistry<CommandSender> messageRegistry;

public UserArgument(HttpAPI httpAPI) {
public UserArgument(HttpAPI httpAPI, MessageRegistry<CommandSender> messageRegistry) {
this.httpAPI = httpAPI;
this.messageRegistry = messageRegistry;
}

@Override
Expand All @@ -48,11 +54,11 @@ protected ParseResult<User> parse(Invocation<CommandSender> invocation, Argument
try {
User user = httpAPI.getUser(input);
if (user == null) {
return ParseResult.failure(new CommandException("User not found"));
return ParseResult.failure(messageRegistry.getInvoked(USER_NOT_FOUND, invocation, argument));
}
return ParseResult.success(user);
} catch (final Exception e) {
return ParseResult.failure(new CommandException("User not found"));
return ParseResult.failure(new CommandException("User not found", e));
}
}

Expand Down

0 comments on commit 7a6b061

Please sign in to comment.