use string chunking for method replyEphemeral(String)

This commit is contained in:
anthorob committed 2024-12-17 12:17:40 -05:00
1 parent 8479f719ab
commit 9861fac769
3 files changed
+24 -7

No files matched your search

@@ -65,8 +65,7 @@ public class HelpCommand extends AbstractCommand {
Button info = BuiltInTask.createButton(BotInfo.class).primary("Info"); Button info = BuiltInTask.createButton(BotInfo.class).primary("Info");
Button reportbug = BuiltInTask.createButton(ReportBug.class).danger("Report bug"); Button reportbug = BuiltInTask.createButton(ReportBug.class).danger("Report bug");
ActionRow row = ActionRow.of(commands, info, reportbug); ActionRow row = ActionRow.of(commands, info, reportbug);
context.replyEphemeral("Help has been sent in DM!"); context.replyEphemeral("Thanks for using the Net Neutrality Bot! What do you need help with?", List.of(row));
context.sendMessageInPrivate("Thanks for using the Net Neutrality Bot! What do you need help with?", List.of(row));
return CommandExecutionResult.none(); return CommandExecutionResult.none();
} }
@@ -75,8 +74,7 @@ public class HelpCommand extends AbstractCommand {
return CommandExecutionResult.warn("Command does not exist"); return CommandExecutionResult.warn("Command does not exist");
} }
context.replyEphemeral("Help has been sent in DM!"); context.replyEphemeral(MessageFunction.buildCommandHelpMessage(container));
context.sendMessageInPrivate(MessageFunction.buildCommandHelpMessage(container));
return CommandExecutionResult.none(); return CommandExecutionResult.none();
} }
} }
@@ -83,6 +83,11 @@ public class MessageCommandContext extends CommandContext {
@Override @Override
public void reply(MessageCreateData messageCreateData, boolean ephemeral, List<LayoutComponent> components, Consumer<AsyncContext> consumer) { public void reply(MessageCreateData messageCreateData, boolean ephemeral, List<LayoutComponent> components, Consumer<AsyncContext> consumer) {
if (ephemeral) {
sendMessageInPrivate(messageCreateData, components);
return;
}
if (getChannel() != null && getChannel().canTalk()) { if (getChannel() != null && getChannel().canTalk()) {
message.reply(messageCreateData).setComponents(components).queue(message -> consumer.accept(new AsyncContext(message))); message.reply(messageCreateData).setComponents(components).queue(message -> consumer.accept(new AsyncContext(message)));
} }
@@ -140,15 +140,23 @@ public interface Context {
defer(false, consumer); defer(false, consumer);
} }
default void replyEphemeral(MessageCreateData messageCreateData, List<LayoutComponent> components) {
reply(messageCreateData, true, components, asyncMessage -> {});
}
default void replyEphemeral(MessageCreateData messageCreateData) { default void replyEphemeral(MessageCreateData messageCreateData) {
reply(messageCreateData, true, List.of(), asyncMessage -> {}); replyEphemeral(messageCreateData, List.of());
} }
default void replyEphemeral(String message) { default void replyEphemeral(String message) {
replyEphemeral(MessageCreateData.fromContent(message)); replyEphemeralWithLimit(message);
} }
default void replyEphemeral(MessageEmbed... embeds) { default void replyEphemeral(String message, List<LayoutComponent> components) {
replyEphemeral(MessageCreateData.fromContent(message), components);
}
default void replyEphemeral(List<MessageEmbed> embeds) {
replyEphemeral(MessageCreateData.fromEmbeds(embeds)); replyEphemeral(MessageCreateData.fromEmbeds(embeds));
} }
@@ -185,6 +193,12 @@ public interface Context {
} }
} }
private void replyEphemeralWithLimit(String message) {
for (String chunk : ContextUtils.splitMessage(message, Message.MAX_CONTENT_LENGTH)) {
replyEphemeral(MessageCreateData.fromContent(chunk));
}
}
private void sendMessageInPrivateWithLimit(String message) { private void sendMessageInPrivateWithLimit(String message) {
for (String chunk : ContextUtils.splitMessage(message, Message.MAX_CONTENT_LENGTH)) { for (String chunk : ContextUtils.splitMessage(message, Message.MAX_CONTENT_LENGTH)) {
sendMessageInPrivate(MessageCreateData.fromContent(chunk)); sendMessageInPrivate(MessageCreateData.fromContent(chunk));