From d89c029ac862692819526a7bfbe146292547db6a Mon Sep 17 00:00:00 2001 From: SquidXTV Date: Mon, 2 Oct 2023 20:07:24 +0200 Subject: [PATCH] Clean up --- .../tjbot/features/help/HelpSystemHelper.java | 22 ++++++++++++------- .../help/HelpThreadCreatedListener.java | 4 +++- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpSystemHelper.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpSystemHelper.java index 88bfd238b9..a7738a466c 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpSystemHelper.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpSystemHelper.java @@ -188,24 +188,30 @@ RestAction constructChatGptAttempt(ThreadChannel threadChannel, """::formatted) .flatMap(threadChannel::sendMessage) .onSuccess(m -> ids.add(m.getId())); - String[] answers = chatGPTAnswer.get(); + for (int i = 0; i < answers.length; i++) { + MessageCreateAction messageCreateAction = threadChannel.sendMessage(answers[i]); + if (i == answers.length - 1) { - message = message.map(answers[i]::formatted) - .flatMap(m -> threadChannel.sendMessage(m) - .addActionRow(Button.danger(componentIdInteractor - .generateComponentId(ids.toArray(String[]::new)), "Dismiss"))); + message = message.flatMap(ignored -> messageCreateAction + .addActionRow(generateDismissButton(componentIdInteractor, ids))); continue; } - message = message.map(answers[i]::formatted) - .flatMap(threadChannel::sendMessage) - .onSuccess(m -> ids.add(m.getId())); + + message = message + .flatMap(ignored -> messageCreateAction.onSuccess(m -> ids.add(m.getId()))); } return message; } + private Button generateDismissButton(ComponentIdInteractor componentIdInteractor, + List ids) { + String buttonId = componentIdInteractor.generateComponentId(ids.toArray(String[]::new)); + return Button.danger(buttonId, "Dismiss"); + } + private Optional prepareChatGptQuestion(ThreadChannel threadChannel, String originalQuestion) { String questionTitle = threadChannel.getName(); diff --git a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java index 7e30b12f5c..8e5db87dc0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java @@ -23,6 +23,7 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.List; +import java.util.Objects; import java.util.concurrent.TimeUnit; /** @@ -142,8 +143,9 @@ public void acceptComponentIdGenerator(ComponentIdGenerator generator) { @Override public void onButtonClick(ButtonInteractionEvent event, List args) { + // This method handles chatgpt's automatic response "dismiss" button ThreadChannel channel = event.getChannel().asThreadChannel(); - Member interactionUser = event.getMember(); + Member interactionUser = Objects.requireNonNull(event.getMember()); if (channel.getOwnerIdLong() != interactionUser.getIdLong() && !helper.hasTagManageRole(interactionUser)) { event.reply("You do not have permission for this action.").setEphemeral(true).queue();