Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.Channel;
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.RestAction;
import org.jetbrains.annotations.NotNull;

import org.togetherjava.tjbot.features.EventReceiver;
import org.togetherjava.tjbot.features.UserInteractionType;
Expand Down Expand Up @@ -56,20 +58,18 @@ public HelpThreadCreatedListener(HelpSystemHelper helper) {
}

@Override
public void onChannelCreate(ChannelCreateEvent createEvent) {
if (!createEvent.getChannelType().isThread()) {
return;
}
ThreadChannel threadChannel = createEvent.getChannel().asThreadChannel();

if (wasThreadAlreadyHandled(threadChannel.getIdLong())) {
return;
}

if (!helper.isHelpForumName(threadChannel.getParentChannel().getName())) {
return;
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
if (event.isFromThread()) {
Channel parentChannel = event.getChannel().asThreadChannel().getParentChannel();
if (helper.isHelpForumName(parentChannel.getName())) {
ThreadChannel threadChannel = event.getChannel().asThreadChannel();
int messageCount = threadChannel.getMessageCount();
if (messageCount > 1 || wasThreadAlreadyHandled(threadChannel.getIdLong())) {
return;
}
handleHelpThreadCreated(threadChannel);
}
}
handleHelpThreadCreated(threadChannel);
}

private boolean wasThreadAlreadyHandled(long threadChannelId) {
Expand All @@ -82,23 +82,10 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) {
}

private void handleHelpThreadCreated(ThreadChannel threadChannel) {
threadChannel.retrieveMessageById(threadChannel.getIdLong()).queue(message -> {

long authorId = threadChannel.getOwnerIdLong();

if (isPostedBySelfUser(message)) {
// When transfer-command is used
authorId = getMentionedAuthorByMessage(message).getIdLong();
}

helper.writeHelpThreadToDatabase(authorId, threadChannel);
});

// The creation is delayed, because otherwise it could be too fast and be executed
// after Discord created the thread, but before Discord send OPs initial message.
// Sending messages at that moment is not allowed.
createMessages(threadChannel).and(pinOriginalQuestion(threadChannel))
.queueAfter(5, TimeUnit.SECONDS);
threadChannel.retrieveStartMessage().flatMap(message -> {
registerThreadDataInDB(message, threadChannel);
return generateAutomatedResponse(threadChannel);
}).flatMap(message -> pinOriginalQuestion(threadChannel)).queue();
}

private static User getMentionedAuthorByMessage(Message message) {
Expand Down Expand Up @@ -126,7 +113,7 @@ private RestAction<Void> pinOriginalQuestion(ThreadChannel threadChannel) {
return threadChannel.retrieveMessageById(threadChannel.getIdLong()).flatMap(Message::pin);
}

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

Expand Down Expand Up @@ -228,4 +215,15 @@ private void handleDismiss(Member interactionUser, ThreadChannel channel,
}
deleteMessages.queue();
}

private void registerThreadDataInDB(Message message, ThreadChannel threadChannel) {
long authorId = threadChannel.getOwnerIdLong();

if (isPostedBySelfUser(message)) {
// When transfer-command is used
authorId = getMentionedAuthorByMessage(message).getIdLong();
}

helper.writeHelpThreadToDatabase(authorId, threadChannel);
}
}