Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
SquidXTV committed Oct 2, 2023
1 parent 8437399 commit d89c029
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,24 +188,30 @@ RestAction<Message> 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<String> ids) {
String buttonId = componentIdInteractor.generateComponentId(ids.toArray(String[]::new));
return Button.danger(buttonId, "Dismiss");
}

private Optional<String> prepareChatGptQuestion(ThreadChannel threadChannel,
String originalQuestion) {
String questionTitle = threadChannel.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -142,8 +143,9 @@ public void acceptComponentIdGenerator(ComponentIdGenerator generator) {

@Override
public void onButtonClick(ButtonInteractionEvent event, List<String> 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();
Expand Down

0 comments on commit d89c029

Please sign in to comment.