Skip to content

Commit

Permalink
Refactor help thread creation workflow (#1112)
Browse files Browse the repository at this point in the history
Thread creation flow is more clear and optimized by removing unnecessary calls to discord API
  • Loading branch information
SquidXTV authored May 15, 2024
1 parent 10718cb commit d3a19aa
Showing 1 changed file with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public HelpThreadCreatedListener(HelpSystemHelper helper) {
@Override
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
if (event.isFromThread()) {
Channel parentChannel = event.getChannel().asThreadChannel().getParentChannel();
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
Channel parentChannel = threadChannel.getParentChannel();
if (helper.isHelpForumName(parentChannel.getName())) {
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
int messageCount = threadChannel.getMessageCount();
if (messageCount > 1 || wasThreadAlreadyHandled(threadChannel.getIdLong())) {
return;
Expand All @@ -84,8 +84,11 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) {
private void handleHelpThreadCreated(ThreadChannel threadChannel) {
threadChannel.retrieveStartMessage().flatMap(message -> {
registerThreadDataInDB(message, threadChannel);
return generateAutomatedResponse(threadChannel);
}).flatMap(message -> pinOriginalQuestion(threadChannel)).queue();
return sendHelperHeadsUp(threadChannel)
.flatMap(any -> HelpThreadCreatedListener.isContextSufficient(message),
any -> createAIResponse(threadChannel, message))
.flatMap(any -> pinOriginalQuestion(message));
}).queue();
}

private static User getMentionedAuthorByMessage(Message message) {
Expand All @@ -96,24 +99,18 @@ private static boolean isPostedBySelfUser(Message message) {
return message.getJDA().getSelfUser().equals(message.getAuthor());
}

private RestAction<Message> createAIResponse(ThreadChannel threadChannel) {
RestAction<Message> originalQuestion = threadChannel.retrieveStartMessage();
return originalQuestion.flatMap(HelpThreadCreatedListener::isContextSufficient,
message -> helper.constructChatGptAttempt(threadChannel, getMessageContent(message),
componentIdInteractor));
private RestAction<Message> createAIResponse(ThreadChannel threadChannel, Message message) {
return helper.constructChatGptAttempt(threadChannel, getMessageContent(message),
componentIdInteractor);
}

private static boolean isContextSufficient(Message message) {
return !MessageUtils.containsImage(message)
&& !LinkDetection.containsLink(message.getContentRaw());
}

private RestAction<Void> pinOriginalQuestion(ThreadChannel threadChannel) {
return threadChannel.retrieveStartMessage().flatMap(Message::pin);
}

private RestAction<Message> generateAutomatedResponse(ThreadChannel threadChannel) {
return sendHelperHeadsUp(threadChannel).flatMap(any -> createAIResponse(threadChannel));
private RestAction<Void> pinOriginalQuestion(Message message) {
return message.pin();
}

private RestAction<Message> sendHelperHeadsUp(ThreadChannel threadChannel) {
Expand Down

0 comments on commit d3a19aa

Please sign in to comment.