|
| 1 | +package net.javadiscord.javabot.listener; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | + |
| 5 | +import lombok.RequiredArgsConstructor; |
| 6 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 7 | +import net.dv8tion.jda.api.entities.channel.ChannelType; |
| 8 | +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; |
| 9 | +import net.dv8tion.jda.api.events.channel.ChannelCreateEvent; |
| 10 | +import net.dv8tion.jda.api.hooks.ListenerAdapter; |
| 11 | +import net.dv8tion.jda.api.interactions.components.buttons.Button; |
| 12 | +import net.javadiscord.javabot.data.config.BotConfig; |
| 13 | +import net.javadiscord.javabot.util.InteractionUtils; |
| 14 | + |
| 15 | +/** |
| 16 | + * Automatically closes older job posts when the same user creates a new one. |
| 17 | + */ |
| 18 | +@RequiredArgsConstructor |
| 19 | +public class JobChannelCloseOldPostsListener extends ListenerAdapter { |
| 20 | + |
| 21 | + private final BotConfig botConfig; |
| 22 | + |
| 23 | + @Override |
| 24 | + public void onChannelCreate(ChannelCreateEvent event) { |
| 25 | + if (event.getChannel().getType() != ChannelType.GUILD_PUBLIC_THREAD) { |
| 26 | + return; |
| 27 | + } |
| 28 | + ThreadChannel post = event.getChannel().asThreadChannel(); |
| 29 | + if (post.getParentChannel().getIdLong() != |
| 30 | + botConfig.get(event.getGuild()).getModerationConfig().getJobChannelId()) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + boolean postClosed = false; |
| 36 | + |
| 37 | + for (ThreadChannel otherPost : post.getParentChannel().getThreadChannels()) { |
| 38 | + if (otherPost.getOwnerIdLong() == post.getOwnerIdLong() && |
| 39 | + otherPost.getIdLong() != post.getIdLong() && |
| 40 | + !otherPost.isPinned()) { |
| 41 | + otherPost.sendMessageEmbeds( |
| 42 | + new EmbedBuilder() |
| 43 | + .setTitle("Post closed") |
| 44 | + .setDescription("This post has been closed because the post owner created [another post](%s)." |
| 45 | + .formatted(post.getJumpUrl())) |
| 46 | + .build()) |
| 47 | + .flatMap(msg -> otherPost.getManager().setArchived(true).setLocked(true)) |
| 48 | + .queue(); |
| 49 | + postClosed = true; |
| 50 | + } |
| 51 | + } |
| 52 | + if (postClosed) { |
| 53 | + post.sendMessageEmbeds( |
| 54 | + new EmbedBuilder() |
| 55 | + .setTitle("Posts closed") |
| 56 | + .setDescription("Since only one open post is allowed per user, older posts have been closed") |
| 57 | + .setColor(Color.YELLOW) |
| 58 | + .build()) |
| 59 | + .addActionRow(Button.secondary( |
| 60 | + InteractionUtils.DELETE_ORIGINAL_TEMPLATE, |
| 61 | + "\uD83D\uDDD1️")) |
| 62 | + .queue(); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments