Skip to content

Commit c9e3a09

Browse files
committed
feat(help): add manual reactivation button for inactive threads
1 parent 1070f23 commit c9e3a09

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import net.dv8tion.jda.api.entities.MessageEmbed;
99
import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
1010
import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
11+
import net.dv8tion.jda.api.interactions.components.buttons.Button;
1112
import net.dv8tion.jda.api.requests.RestAction;
1213
import net.dv8tion.jda.api.utils.TimeUtil;
1314
import org.slf4j.Logger;
@@ -128,41 +129,48 @@ private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed)
128129
() -> triggerAuthorIdNotFoundArchiveFlow(threadChannel, embed));
129130
}
130131

131-
private void triggerArchiveFlow(ThreadChannel threadChannel, long authorId,
132-
MessageEmbed embed) {
132+
private void triggerArchiveFlow(ThreadChannel threadChannel, long authorId, MessageEmbed embed) {
133133

134+
// --- UPDATED: Added ActionRow with custom namespace ID ---
134135
Function<Member, RestAction<Message>> sendEmbedWithMention =
135-
member -> threadChannel.sendMessage(member.getAsMention()).addEmbeds(embed);
136+
member -> threadChannel.sendMessage(member.getAsMention())
137+
.addEmbeds(embed)
138+
.addActionRow(Button.primary("OTHER:thread-inactivity:mark-active", "Mark Active"));
136139

137140
Supplier<RestAction<Message>> sendEmbedWithoutMention =
138-
() -> threadChannel.sendMessageEmbeds(embed);
141+
() -> threadChannel.sendMessageEmbeds(embed)
142+
.addActionRow(Button.primary("OTHER:thread-inactivity:mark-active", "Mark Active"));
143+
// ---------------------------------------------------------
139144

140145
threadChannel.getGuild()
141-
.retrieveMemberById(authorId)
142-
.mapToResult()
143-
.flatMap(authorResults -> {
144-
if (authorResults.isFailure()) {
145-
logger.info(
146-
"Trying to archive a thread ({}), but OP ({}) left the server, sending embed without mention",
147-
threadChannel.getId(), authorId, authorResults.getFailure());
148-
149-
return sendEmbedWithoutMention.get();
150-
}
151-
152-
return sendEmbedWithMention.apply(authorResults.get());
153-
})
154-
.flatMap(_ -> threadChannel.getManager().setArchived(true))
155-
.queue();
146+
.retrieveMemberById(authorId)
147+
.mapToResult()
148+
.flatMap(authorResults -> {
149+
if (authorResults.isFailure()) {
150+
logger.info(
151+
"Trying to archive a thread ({}), but OP ({}) left the server, sending embed without mention",
152+
threadChannel.getId(), authorId, authorResults.getFailure());
153+
154+
return sendEmbedWithoutMention.get();
155+
}
156+
157+
return sendEmbedWithMention.apply(authorResults.get());
158+
})
159+
.flatMap(_ -> threadChannel.getManager().setArchived(true))
160+
.queue();
156161
}
157162

158-
private void triggerAuthorIdNotFoundArchiveFlow(ThreadChannel threadChannel,
159-
MessageEmbed embed) {
163+
private void triggerAuthorIdNotFoundArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) {
160164

161165
logger.info(
162166
"Was unable to find a matching thread for id: {} in DB, archiving thread without mentioning OP",
163167
threadChannel.getId());
168+
169+
// --- UPDATED: Added ActionRow with custom namespace ID ---
164170
threadChannel.sendMessageEmbeds(embed)
165-
.flatMap(sentEmbed -> threadChannel.getManager().setArchived(true))
166-
.queue();
171+
.addActionRow(Button.primary("OTHER:thread-inactivity:mark-active", "Mark Active"))
172+
.flatMap(sentEmbed -> threadChannel.getManager().setArchived(true))
173+
.queue();
174+
// ---------------------------------------------
167175
}
168176
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public final class HelpThreadCreatedListener extends ListenerAdapter
4747
private static final Logger log = LoggerFactory.getLogger(HelpThreadCreatedListener.class);
4848
private final HelpSystemHelper helper;
4949

50+
private static final String MARK_ACTIVE_ID = "mark-active";
51+
private final ComponentIdInteractor inactivityInteractor =
52+
new ComponentIdInteractor(UserInteractionType.OTHER, "thread-inactivity");
53+
5054
private final Cache<Long, Instant> threadIdToCreatedAtCache = Caffeine.newBuilder()
5155
.maximumSize(1_000)
5256
.expireAfterAccess(2, TimeUnit.of(ChronoUnit.MINUTES))
@@ -187,7 +191,16 @@ private Consumer<Throwable> handleParentMessageDeleted(Member user, ThreadChanne
187191

188192
@Override
189193
public void onButtonClick(ButtonInteractionEvent event, List<String> args) {
190-
// This method handles chatgpt's automatic response "dismiss" button
194+
// Check if the button belongs to the "thread-inactivity" namespace
195+
if (inactivityInteractor.isMatch(event.getComponentId())) {
196+
String subId = inactivityInteractor.getComponentId(event.getComponentId());
197+
198+
if (subId.equals("mark-active")) {
199+
handleMarkActiveInteraction(event);
200+
return; // EXIT: This logic is owned by handleMarkActiveInteraction
201+
}
202+
}
203+
// Handle chatgpt's automatic response "dismiss" button
191204
event.deferEdit().queue();
192205

193206
ThreadChannel channel = event.getChannel().asThreadChannel();
@@ -248,4 +261,17 @@ private void registerThreadDataInDB(Message message, ThreadChannel threadChannel
248261

249262
helper.writeHelpThreadToDatabase(authorId, threadChannel);
250263
}
264+
265+
private void handleMarkActiveInteraction(ButtonInteractionEvent event) {
266+
event.deferEdit().queue();
267+
268+
if (event.getChannel() instanceof ThreadChannel thread) {
269+
// Reopen the thread so people can chat, then delete the bot's warning
270+
thread.getManager().setArchived(false).queue();
271+
event.getMessage().delete().queue();
272+
273+
log.info("Thread {} was manually reactivated via button by {}",
274+
thread.getId(), event.getUser().getName());
275+
}
276+
}
251277
}

0 commit comments

Comments
 (0)