Skip to content

Commit

Permalink
Fix issue: paged help does not work in private message (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoACE6716 authored Aug 21, 2023
1 parent 23008cf commit 1f7aaa5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import snw.jkook.command.CommandSender;
import snw.jkook.entity.User;
import snw.jkook.message.Message;
import snw.jkook.message.PrivateMessage;
import snw.jkook.message.component.card.CardBuilder;
import snw.jkook.message.component.card.Size;
import snw.jkook.message.component.card.Theme;
Expand Down Expand Up @@ -71,6 +72,14 @@ public CloudHelpCommand() {
@CommandMethod("[command]")
@CommandDescription("获取此帮助列表。")
public void consoleHelp(CommandSender sender, Message message, @Argument("command") @Quoted @Nullable String command, @Flag("force") boolean force) {

String messageType;
if (message instanceof PrivateMessage) {
messageType = "PM";
} else {
messageType = "CM";
}

if (sender instanceof User) {
List<String> content = Util.listCloudCommandsHelp(this.client, force);
CloudCommandInfo specificCommand = Util.findSpecificCloudCommand(this.client, command);
Expand Down Expand Up @@ -132,15 +141,15 @@ public void consoleHelp(CommandSender sender, Message message, @Argument("comman
Arrays.asList(
new ButtonElement(
Theme.PRIMARY,
HELP_VALUE_HEADER + "{\"page\": 0, \"current\": 1, \"force\": " + force + "}", // Placeholder
HELP_VALUE_HEADER + "{\"page\": 0, \"current\": 1, \"force\": " + force + ",\"messageType\": " + messageType + "}", // Placeholder
ButtonElement.EventType.NO_ACTION,
new PlainTextElement("上一页")
),
new ButtonElement(Theme.SECONDARY, "", EMPTY_PLAIN_TEXT_ELEMENT), // Placeholder
new ButtonElement(Theme.SECONDARY, "", EMPTY_PLAIN_TEXT_ELEMENT), // Placeholder
new ButtonElement(
Theme.PRIMARY,
HELP_VALUE_HEADER + "{\"page\": 2, \"current\": 1, \"force\": " + force + "}",
HELP_VALUE_HEADER + "{\"page\": 2, \"current\": 1, \"force\": " + force + ",\"messageType\": " + messageType + "}",
ButtonElement.EventType.RETURN_VAL,
new PlainTextElement("下一页")
)
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/snw/kookbc/impl/command/internal/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import snw.jkook.command.UserCommandExecutor;
import snw.jkook.entity.User;
import snw.jkook.message.Message;
import snw.jkook.message.PrivateMessage;
import snw.jkook.message.component.card.CardBuilder;
import snw.jkook.message.component.card.Size;
import snw.jkook.message.component.card.Theme;
Expand Down Expand Up @@ -67,11 +68,19 @@ public void onCommand(ConsoleCommandSender sender, Object[] arguments) {

@Override
public void onCommand(User sender, Object[] arguments, @Nullable Message message) {

if (message == null) {
// executed by CommandManager#executeCommand?
return;
}

String messageType;
if (message instanceof PrivateMessage) {
messageType = "PM";
} else {
messageType = "CM";
}

List<String> content = Util.listCommandsHelp(this.client);
JKookCommand specificCommand = arguments.length == 1 && arguments[0] != null && arguments[0] instanceof String ?
Util.findSpecificCommand(this.client, (String) arguments[0]) : null;
Expand Down Expand Up @@ -133,15 +142,15 @@ public void onCommand(User sender, Object[] arguments, @Nullable Message message
Arrays.asList(
new ButtonElement(
Theme.PRIMARY,
HELP_VALUE_HEADER + "{\"page\": 0, \"current\": 1}", // Placeholder
HELP_VALUE_HEADER + "{\"page\": 0, \"current\": 1, \"messageType\": " + messageType + "}", // Placeholder
ButtonElement.EventType.NO_ACTION,
new PlainTextElement("上一页")
),
new ButtonElement(Theme.SECONDARY, "", EMPTY_PLAIN_TEXT_ELEMENT), // Placeholder
new ButtonElement(Theme.SECONDARY, "", EMPTY_PLAIN_TEXT_ELEMENT), // Placeholder
new ButtonElement(
Theme.PRIMARY,
HELP_VALUE_HEADER + "{\"page\": 2, \"current\": 1}",
HELP_VALUE_HEADER + "{\"page\": 2, \"current\": 1, \"messageType\": " + messageType + "}",
ButtonElement.EventType.RETURN_VAL,
new PlainTextElement("下一页")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import snw.kookbc.SharedConstants;
import snw.kookbc.impl.KBCClient;
import snw.kookbc.impl.command.cloud.CloudCommandManagerImpl;
import snw.kookbc.impl.message.TextChannelMessageImpl;
import snw.kookbc.util.Util;

import java.util.Arrays;
Expand All @@ -44,6 +43,7 @@ public void event(UserClickButtonEvent event) {
JsonObject detail = JsonParser.parseString(value.substring(HELP_VALUE_HEADER.length())).getAsJsonObject();
int page = detail.get("page").getAsInt();
int currentPage = detail.get("current").getAsInt();
String messageType = detail.get("messageType").getAsString();
boolean force = detail.has("force") && detail.get("force").getAsBoolean();
if (page == currentPage) {
return;
Expand Down Expand Up @@ -85,15 +85,15 @@ public void event(UserClickButtonEvent event) {
Arrays.asList(
new ButtonElement(
Theme.PRIMARY,
String.format(HELP_VALUE_HEADER + "{\"page\": %d, \"current\": %d}", page - 1, page), // Placeholder
String.format(HELP_VALUE_HEADER + "{\"page\": %d, \"current\": %d, \"messageType\": %s}", page - 1, page, messageType), // Placeholder
page > 1 ? ButtonElement.EventType.RETURN_VAL : ButtonElement.EventType.NO_ACTION,
new PlainTextElement("上一页")
),
new ButtonElement(Theme.SECONDARY, "", EMPTY_PLAIN_TEXT_ELEMENT), // Placeholder
new ButtonElement(Theme.SECONDARY, "", EMPTY_PLAIN_TEXT_ELEMENT), // Placeholder
new ButtonElement(
Theme.PRIMARY,
String.format(HELP_VALUE_HEADER + "{\"page\": %d, \"current\": %d}", page + 1, page),
String.format(HELP_VALUE_HEADER + "{\"page\": %d, \"current\": %d, \"messageType\": %s}", page + 1, page, messageType),
(5 * page) < content.size() ? ButtonElement.EventType.RETURN_VAL : ButtonElement.EventType.NO_ACTION,
new PlainTextElement("下一页")
)
Expand All @@ -120,8 +120,16 @@ public void event(UserClickButtonEvent event) {
}
finalComponent = builder.build();
}
Message message = new TextChannelMessageImpl(this.client, event.getMessageId(), null, null, 0L, null, null);
message.setComponent(finalComponent);

if (messageType.equals("PM")) {
Message message = this.client.getCore().getUnsafe().getPrivateMessage(event.getMessageId());
;
message.setComponent(finalComponent);
} else if (messageType.equals("CM")) {
Message message = this.client.getCore().getUnsafe().getTextChannelMessage(event.getMessageId());
message.setComponent(finalComponent);
}

}

}

0 comments on commit 1f7aaa5

Please sign in to comment.