From 4647454d4ee7a3cec1387782529b8b2facfbf356 Mon Sep 17 00:00:00 2001 From: huanmeng_qwq Date: Thu, 8 Aug 2024 17:13:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81Role=E5=92=8CCustomEm?= =?UTF-8?q?oji=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/litecommands/LiteKookFactory.java | 10 +- .../litecommands/argument/EmojiArgument.java | 102 ++++++++++++++++++ .../litecommands/argument/RoleArgument.java | 98 +++++++++++++++++ 3 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 src/main/java/snw/kookbc/impl/command/litecommands/argument/EmojiArgument.java create mode 100644 src/main/java/snw/kookbc/impl/command/litecommands/argument/RoleArgument.java diff --git a/src/main/java/snw/kookbc/impl/command/litecommands/LiteKookFactory.java b/src/main/java/snw/kookbc/impl/command/litecommands/LiteKookFactory.java index a29a43e8..adb059dc 100644 --- a/src/main/java/snw/kookbc/impl/command/litecommands/LiteKookFactory.java +++ b/src/main/java/snw/kookbc/impl/command/litecommands/LiteKookFactory.java @@ -23,7 +23,9 @@ 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; @@ -31,10 +33,9 @@ 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; @@ -63,6 +64,9 @@ public static .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()) ); } diff --git a/src/main/java/snw/kookbc/impl/command/litecommands/argument/EmojiArgument.java b/src/main/java/snw/kookbc/impl/command/litecommands/argument/EmojiArgument.java new file mode 100644 index 00000000..301beac8 --- /dev/null +++ b/src/main/java/snw/kookbc/impl/command/litecommands/argument/EmojiArgument.java @@ -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 . + */ + +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 { + private final KBCClient client; + + public EmojiArgument(KBCClient client) { + this.client = client; + } + + @Override + protected ParseResult parse(Invocation invocation, Argument 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 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> emojis = guild.getCustomEmojis(); + FIND: + while (emojis.hasNext()) { + Set 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 invocation, Argument argument, SuggestionContext context) { + return super.suggest(invocation, argument, context); + } +} diff --git a/src/main/java/snw/kookbc/impl/command/litecommands/argument/RoleArgument.java b/src/main/java/snw/kookbc/impl/command/litecommands/argument/RoleArgument.java new file mode 100644 index 00000000..b33bd757 --- /dev/null +++ b/src/main/java/snw/kookbc/impl/command/litecommands/argument/RoleArgument.java @@ -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 . + */ + +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 { + private final KBCClient client; + + public RoleArgument(KBCClient client) { + this.client = client; + } + + @Override + protected ParseResult parse(Invocation invocation, Argument 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 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> roles = guild.getRoles(); + FIND: + while (roles.hasNext()) { + Set 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 invocation, Argument argument, SuggestionContext context) { + return super.suggest(invocation, argument, context); + } +}