Skip to content

Commit

Permalink
feat: 支持Role和CustomEmoji参数解析
Browse files Browse the repository at this point in the history
  • Loading branch information
huanmeng-qwq authored Aug 8, 2024
1 parent 7c0718f commit 4647454
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@
import snw.jkook.Core;
import snw.jkook.command.CommandSender;
import snw.jkook.command.ConsoleCommandSender;
import snw.jkook.entity.CustomEmoji;
import snw.jkook.entity.Guild;
import snw.jkook.entity.Role;
import snw.jkook.entity.User;
import snw.jkook.entity.channel.Channel;
import snw.jkook.entity.channel.NonCategoryChannel;
import snw.jkook.entity.channel.TextChannel;
import snw.jkook.entity.channel.VoiceChannel;
import snw.jkook.message.Message;
import snw.jkook.plugin.Plugin;
import snw.kookbc.impl.CoreImpl;
import snw.kookbc.impl.command.CommandManagerImpl;
import snw.kookbc.impl.command.litecommands.argument.ChannelArgument;
import snw.kookbc.impl.command.litecommands.argument.GuildArgument;
import snw.kookbc.impl.command.litecommands.argument.UserArgument;
import snw.kookbc.impl.command.litecommands.argument.*;
import snw.kookbc.impl.command.litecommands.tools.KookMessageContextual;
import snw.kookbc.impl.command.litecommands.tools.KookOnlyConsoleContextual;
import snw.kookbc.impl.command.litecommands.tools.KookOnlyUserContextual;
Expand Down Expand Up @@ -63,6 +64,9 @@ public static <B extends LiteCommandsBuilder<CommandSender, LiteKookSettings, B>
.argument(TextChannel.class, new ChannelArgument<>(plugin.getCore().getHttpAPI()))
.argument(VoiceChannel.class, new ChannelArgument<>(plugin.getCore().getHttpAPI()))

.argument(Role.class, new RoleArgument(((CoreImpl) plugin.getCore()).getClient()))
.argument(CustomEmoji.class, new EmojiArgument(((CoreImpl) plugin.getCore()).getClient()))

.result(String.class, new StringHandler())
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* KookBC -- The Kook Bot Client & JKook API standard implementation for Java.
* Copyright (C) 2022 - 2023 KookBC contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package snw.kookbc.impl.command.litecommands.argument;

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
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.entity.CustomEmoji;
import snw.jkook.entity.Guild;
import snw.jkook.message.ChannelMessage;
import snw.jkook.message.Message;
import snw.jkook.util.PageIterator;
import snw.kookbc.impl.KBCClient;

import java.util.Objects;
import java.util.Optional;
import java.util.Set;

public class EmojiArgument extends ArgumentResolver<CommandSender, CustomEmoji> {
private final KBCClient client;

public EmojiArgument(KBCClient client) {
this.client = client;
}

@Override
protected ParseResult<CustomEmoji> parse(Invocation<CommandSender> invocation, Argument<CustomEmoji> context, String argument) {
String emojiName = argument;
String emojiId = argument;
if (emojiName.startsWith("(emj)")) {
emojiName = emojiName.substring(5);
}
int indexOf = emojiName.indexOf("(emj)");
if (indexOf >= 0) {
emojiId = emojiName.substring(indexOf + 5);
emojiName = emojiName.substring(0, indexOf);
}
indexOf = emojiId.indexOf(']');
if (indexOf >= 0) {
emojiId = emojiId.substring(1, indexOf);
}
try {
Optional<Message> optional = invocation.context().get(Message.class);
if (!optional.isPresent()) {
return ParseResult.failure(new CommandException("Unsupported argument: " + argument));
}
Message message = optional.get();
if (message instanceof ChannelMessage) {
ChannelMessage channelMessage = (ChannelMessage) message;
Guild guild = channelMessage.getChannel().getGuild();

CustomEmoji emoji = client.getStorage().getEmoji(emojiId);
if (emoji == null) {
PageIterator<Set<CustomEmoji>> emojis = guild.getCustomEmojis();
FIND:
while (emojis.hasNext()) {
Set<CustomEmoji> set = emojis.next();
for (CustomEmoji r : set) {
if (Objects.equals(r.getId(), emojiId) && Objects.equals(r.getName(), emojiName)) {
emoji = r;
break FIND;
}
}
}
}
if (emoji == null) {
return ParseResult.failure(new CommandException("CustomEmoji not found"));
}
return ParseResult.success(emoji);
}
return ParseResult.failure(new CommandException("Unsupported argument: " + argument));
} catch (final Exception e) {
return ParseResult.failure(new CommandException("CustomEmoji not found"));
}
}

@Override
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<CustomEmoji> argument, SuggestionContext context) {
return super.suggest(invocation, argument, context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* KookBC -- The Kook Bot Client & JKook API standard implementation for Java.
* Copyright (C) 2022 - 2023 KookBC contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package snw.kookbc.impl.command.litecommands.argument;

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
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.entity.Guild;
import snw.jkook.entity.Role;
import snw.jkook.message.ChannelMessage;
import snw.jkook.message.Message;
import snw.jkook.util.PageIterator;
import snw.kookbc.impl.KBCClient;

import java.util.Optional;
import java.util.Set;

public class RoleArgument extends ArgumentResolver<CommandSender, Role> {
private final KBCClient client;

public RoleArgument(KBCClient client) {
this.client = client;
}

@Override
protected ParseResult<Role> parse(Invocation<CommandSender> invocation, Argument<Role> context, String argument) {
String input = argument;
int index = input.indexOf("(rol)");
if (index >= 0) {
input = input.substring(index + 5);
}
index = input.indexOf("(rol)");
if (index >= 0) {
input = input.substring(0, index);
}
try {
Optional<Message> optional = invocation.context().get(Message.class);
if (!optional.isPresent()) {
return ParseResult.failure(new CommandException("Unsupported argument: " + argument));
}
Message message = optional.get();
if (message instanceof ChannelMessage) {
ChannelMessage channelMessage = (ChannelMessage) message;
Guild guild = channelMessage.getChannel().getGuild();

int roleId = Integer.parseInt(input);

Role role = client.getStorage().getRole(guild, roleId);
if (role == null) {
PageIterator<Set<Role>> roles = guild.getRoles();
FIND:
while (roles.hasNext()) {
Set<Role> set = roles.next();
for (Role r : set) {
if (r.getId() == roleId) {
role = r;
break FIND;
}
}
}
}
if (role == null) {
return ParseResult.failure(new CommandException("Role not found"));
}
return ParseResult.success(role);
}
return ParseResult.failure(new CommandException("Unsupported argument: " + argument));
} catch (final Exception e) {
return ParseResult.failure(new CommandException("Role not found", e));
}
}

@Override
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<Role> argument, SuggestionContext context) {
return super.suggest(invocation, argument, context);
}
}

0 comments on commit 4647454

Please sign in to comment.