|
| 1 | +package org.togetherjava.tjbot.features.help; |
| 2 | + |
| 3 | +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; |
| 4 | +import net.dv8tion.jda.api.events.channel.update.ChannelUpdateArchivedEvent; |
| 5 | +import net.dv8tion.jda.api.hooks.ListenerAdapter; |
| 6 | +import org.jetbrains.annotations.NotNull; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import org.togetherjava.tjbot.db.Database; |
| 11 | +import org.togetherjava.tjbot.features.EventReceiver; |
| 12 | + |
| 13 | +import java.time.Instant; |
| 14 | + |
| 15 | +import static org.togetherjava.tjbot.db.generated.tables.HelpThreads.HELP_THREADS; |
| 16 | + |
| 17 | +public final class HelpThreadArchivedListener extends ListenerAdapter implements EventReceiver { |
| 18 | + |
| 19 | + private final HelpSystemHelper helper; |
| 20 | + private final Logger logger = LoggerFactory.getLogger(HelpThreadArchivedListener.class); |
| 21 | + private final Database database; |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates a new instance. |
| 25 | + * |
| 26 | + * @param helper to work with the help threads |
| 27 | + */ |
| 28 | + public HelpThreadArchivedListener(HelpSystemHelper helper, Database database) { |
| 29 | + this.helper = helper; |
| 30 | + this.database = database; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void onChannelUpdateArchived(@NotNull ChannelUpdateArchivedEvent event) { |
| 35 | + if (!event.getChannelType().isThread()) { |
| 36 | + return; |
| 37 | + } |
| 38 | + ThreadChannel threadChannel = event.getChannel().asThreadChannel(); |
| 39 | + |
| 40 | + |
| 41 | + if (!helper.isHelpForumName(threadChannel.getParentChannel().getName())) { |
| 42 | + return; |
| 43 | + } |
| 44 | + handleThreadStatus(threadChannel); |
| 45 | + } |
| 46 | + |
| 47 | + private void handleThreadStatus(ThreadChannel threadChannel) { |
| 48 | + long threadId = threadChannel.getIdLong(); |
| 49 | + Instant closedAt = Instant.now(); |
| 50 | + |
| 51 | + int status = database.read(context -> context.selectFrom(HELP_THREADS) |
| 52 | + .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) |
| 53 | + .fetchOne(HELP_THREADS.TICKET_STATUS)); |
| 54 | + |
| 55 | + if (status == HelpSystemHelper.TicketStatus.ACTIVE.val) { |
| 56 | + changeStatusToArchive(closedAt, threadId); |
| 57 | + } |
| 58 | + |
| 59 | + changeStatusToActive(threadId); |
| 60 | + } |
| 61 | + |
| 62 | + private void changeStatusToArchive(Instant closedAt, long threadId) { |
| 63 | + database.write(context -> context.update(HELP_THREADS) |
| 64 | + .set(HELP_THREADS.CLOSED_AT, closedAt) |
| 65 | + .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ARCHIVED.val) |
| 66 | + .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) |
| 67 | + .execute()); |
| 68 | + |
| 69 | + logger.info("Thread with id: {}, updated to active in database", threadId); |
| 70 | + } |
| 71 | + |
| 72 | + private void changeStatusToActive(long threadId) { |
| 73 | + database.write(context -> context.update(HELP_THREADS) |
| 74 | + .set(HELP_THREADS.TICKET_STATUS, HelpSystemHelper.TicketStatus.ACTIVE.val) |
| 75 | + .where(HELP_THREADS.CHANNEL_ID.eq(threadId)) |
| 76 | + .execute()); |
| 77 | + |
| 78 | + logger.info("Thread with id: {}, updated to archive status in database", threadId); |
| 79 | + } |
| 80 | +} |
0 commit comments