Skip to content

Commit 3323c48

Browse files
committed
resolve conflicts
2 parents 7708513 + 7f33782 commit 3323c48

File tree

7 files changed

+62
-18
lines changed

7 files changed

+62
-18
lines changed

application/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ dependencies {
7474

7575
implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'
7676

77-
implementation 'org.kohsuke:github-api:1.317'
77+
implementation 'org.kohsuke:github-api:1.318'
7878

7979
testImplementation 'org.mockito:mockito-core:5.3.1'
8080
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'

application/src/main/java/org/togetherjava/tjbot/features/help/HelpSystemHelper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,4 +373,13 @@ enum TicketStatus {
373373
this.val = val;
374374
}
375375
}
376+
Optional<Long> getAuthorByHelpThreadId(final long channelId) {
377+
378+
logger.debug("Looking for thread-record using channel ID: {}", channelId);
379+
380+
return database.read(context -> context.select(HelpThreads.HELP_THREADS.AUTHOR_ID)
381+
.from(HelpThreads.HELP_THREADS)
382+
.where(HelpThreads.HELP_THREADS.CHANNEL_ID.eq(channelId))
383+
.fetchOptional(HelpThreads.HELP_THREADS.AUTHOR_ID));
384+
}
376385
}

application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadAutoArchiver.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.JDA;
55
import net.dv8tion.jda.api.entities.Guild;
6+
import net.dv8tion.jda.api.entities.Member;
7+
import net.dv8tion.jda.api.entities.Message;
68
import net.dv8tion.jda.api.entities.MessageEmbed;
79
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
810
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
911
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
10-
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
12+
import net.dv8tion.jda.api.requests.RestAction;
1113
import net.dv8tion.jda.api.utils.TimeUtil;
1214
import org.slf4j.Logger;
1315
import org.slf4j.LoggerFactory;
@@ -19,7 +21,8 @@
1921
import java.util.List;
2022
import java.util.Optional;
2123
import java.util.concurrent.TimeUnit;
22-
import java.util.function.Consumer;
24+
import java.util.function.Function;
25+
import java.util.function.Supplier;
2326

2427
/**
2528
* Routine, which periodically checks all help threads and archives them if there has not been any
@@ -122,18 +125,30 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA
122125
}
123126

124127
private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) {
125-
Consumer<Throwable> handleFailure = error -> {
126-
if (error instanceof ErrorResponseException) {
127-
logger.warn("Unknown error occurred during help thread auto archive routine",
128-
error);
129-
}
130-
};
128+
129+
Function<Member, RestAction<Message>> sendEmbedWithMention =
130+
member -> threadChannel.sendMessage(member.getAsMention()).addEmbeds(embed);
131+
132+
Supplier<RestAction<Message>> sendEmbedWithoutMention =
133+
() -> threadChannel.sendMessageEmbeds(embed);
134+
135+
long authorId = helper.getAuthorByHelpThreadId(threadChannel.getIdLong()).orElseThrow();
131136

132137
threadChannel.getGuild()
133-
.retrieveMemberById(threadChannel.getOwnerIdLong())
134-
.flatMap(author -> threadChannel.sendMessage(author.getAsMention()).addEmbeds(embed))
138+
.retrieveMemberById(authorId)
139+
.mapToResult()
140+
.flatMap(authorResults -> {
141+
if (authorResults.isFailure()) {
142+
logger.info(
143+
"Trying to archive a thread ({}), but OP ({}) left the server, sending embed without mention",
144+
threadChannel.getId(), authorId, authorResults.getFailure());
145+
146+
return sendEmbedWithoutMention.get();
147+
}
148+
149+
return sendEmbedWithMention.apply(authorResults.get());
150+
})
135151
.flatMap(any -> threadChannel.getManager().setArchived(true))
136-
.queue(any -> {
137-
}, handleFailure);
152+
.queue();
138153
}
139154
}

application/src/main/java/org/togetherjava/tjbot/features/help/HelpThreadCreatedListener.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.dv8tion.jda.api.entities.Message;
77
import net.dv8tion.jda.api.entities.MessageEmbed;
88
import net.dv8tion.jda.api.entities.Role;
9+
import net.dv8tion.jda.api.entities.User;
910
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
1011
import net.dv8tion.jda.api.entities.channel.forums.ForumTag;
1112
import net.dv8tion.jda.api.events.channel.ChannelCreateEvent;
@@ -38,6 +39,7 @@ public final class HelpThreadCreatedListener extends ListenerAdapter
3839
implements EventReceiver, UserInteractor {
3940

4041
private final HelpSystemHelper helper;
42+
4143
private final Cache<Long, Instant> threadIdToCreatedAtCache = Caffeine.newBuilder()
4244
.maximumSize(1_000)
4345
.expireAfterAccess(2, TimeUnit.of(ChronoUnit.MINUTES))
@@ -81,7 +83,17 @@ private boolean wasThreadAlreadyHandled(long threadChannelId) {
8183
}
8284

8385
private void handleHelpThreadCreated(ThreadChannel threadChannel) {
84-
helper.writeHelpThreadToDatabase(threadChannel.getOwnerIdLong(), threadChannel);
86+
threadChannel.retrieveMessageById(threadChannel.getIdLong()).queue(message -> {
87+
88+
long authorId = threadChannel.getOwnerIdLong();
89+
90+
if (isPostedBySelfUser(message)) {
91+
// When transfer-command is used
92+
authorId = getMentionedAuthorByMessage(message).getIdLong();
93+
}
94+
95+
helper.writeHelpThreadToDatabase(authorId, threadChannel);
96+
});
8597

8698
// The creation is delayed, because otherwise it could be too fast and be executed
8799
// after Discord created the thread, but before Discord send OPs initial message.
@@ -90,6 +102,14 @@ private void handleHelpThreadCreated(ThreadChannel threadChannel) {
90102
.queueAfter(5, TimeUnit.SECONDS);
91103
}
92104

105+
private static User getMentionedAuthorByMessage(Message message) {
106+
return message.getMentions().getUsers().getFirst();
107+
}
108+
109+
private static boolean isPostedBySelfUser(Message message) {
110+
return message.getJDA().getSelfUser().equals(message.getAuthor());
111+
}
112+
93113
private RestAction<Message> createAIResponse(ThreadChannel threadChannel) {
94114
RestAction<Message> originalQuestion =
95115
threadChannel.retrieveMessageById(threadChannel.getIdLong());

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ group 'org.togetherjava'
99
version '1.0-SNAPSHOT'
1010

1111
ext {
12-
jooqVersion = '3.18.0'
12+
jooqVersion = '3.19.1'
1313
jacksonVersion = '2.16.0'
1414
chatGPTVersion = '0.18.0'
1515
}

buildSrc/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ repositories {
77
}
88

99
dependencies {
10-
implementation "gradle.plugin.org.flywaydb:gradle-plugin-publishing:10.3.0"
11-
implementation 'nu.studer:gradle-jooq-plugin:8.2'
10+
implementation "gradle.plugin.org.flywaydb:gradle-plugin-publishing:10.4.0"
11+
implementation 'nu.studer:gradle-jooq-plugin:9.0'
1212
}

database/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var sqliteVersion = "3.44.0.0"
77
dependencies {
88
implementation 'com.google.code.findbugs:jsr305:3.0.2'
99
implementation "org.xerial:sqlite-jdbc:${sqliteVersion}"
10-
implementation 'org.flywaydb:flyway-core:10.3.0'
10+
implementation 'org.flywaydb:flyway-core:10.4.0'
1111
implementation "org.jooq:jooq:$jooqVersion"
1212

1313
implementation project(':utils')

0 commit comments

Comments
 (0)