|
| 1 | +package com.eternalcode.discordapp.ticket; |
| 2 | + |
| 3 | +import com.eternalcode.discordapp.util.UrlValidator; |
| 4 | +import java.awt.Color; |
| 5 | +import java.time.Instant; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.concurrent.CompletableFuture; |
| 9 | +import java.util.concurrent.ThreadLocalRandom; |
| 10 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 11 | +import net.dv8tion.jda.api.JDA; |
| 12 | +import net.dv8tion.jda.api.Permission; |
| 13 | +import net.dv8tion.jda.api.entities.Guild; |
| 14 | +import net.dv8tion.jda.api.entities.Member; |
| 15 | +import net.dv8tion.jda.api.entities.MessageEmbed; |
| 16 | +import net.dv8tion.jda.api.entities.Role; |
| 17 | +import net.dv8tion.jda.api.entities.User; |
| 18 | +import net.dv8tion.jda.api.entities.channel.concrete.Category; |
| 19 | +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; |
| 20 | +import net.dv8tion.jda.api.interactions.components.buttons.Button; |
| 21 | +import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; |
| 22 | +import net.dv8tion.jda.api.utils.messages.MessageCreateData; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | + |
| 26 | +public class TicketChannelService { |
| 27 | + |
| 28 | + private static final Logger LOGGER = LoggerFactory.getLogger(TicketChannelService.class); |
| 29 | + |
| 30 | + private final JDA jda; |
| 31 | + private final TicketConfig config; |
| 32 | + private final TicketService ticketService; |
| 33 | + private final TranscriptGeneratorService transcriptGeneratorService; |
| 34 | + |
| 35 | + public TicketChannelService( |
| 36 | + JDA jda, |
| 37 | + TicketConfig config, |
| 38 | + TicketService ticketService, |
| 39 | + TranscriptGeneratorService transcriptGeneratorService |
| 40 | + ) { |
| 41 | + this.jda = jda; |
| 42 | + this.config = config; |
| 43 | + this.ticketService = ticketService; |
| 44 | + this.transcriptGeneratorService = transcriptGeneratorService; |
| 45 | + } |
| 46 | + |
| 47 | + public CompletableFuture<Optional<TextChannel>> createTicket(long userId, String categoryId) { |
| 48 | + TicketConfig.TicketCategoryConfig category = this.config.getCategoryById(categoryId); |
| 49 | + if (category == null || !category.enabled) { |
| 50 | + return CompletableFuture.completedFuture(Optional.empty()); |
| 51 | + } |
| 52 | + |
| 53 | + return this.ticketService.canUserCreateTicket(userId, categoryId) |
| 54 | + .thenCompose(canCreate -> { |
| 55 | + if (!canCreate) { |
| 56 | + return CompletableFuture.completedFuture(Optional.empty()); |
| 57 | + } |
| 58 | + |
| 59 | + return CompletableFuture.supplyAsync(() -> { |
| 60 | + try { |
| 61 | + long ticketId = Instant.now().toEpochMilli() + ThreadLocalRandom.current().nextInt(1000, 9999); |
| 62 | + TextChannel channel = this.createChannel(userId, category, ticketId); |
| 63 | + |
| 64 | + TicketWrapper ticket = new TicketWrapper(userId, channel.getIdLong(), categoryId); |
| 65 | + this.ticketService.saveTicket(ticket).join(); |
| 66 | + |
| 67 | + channel.sendMessage(this.createWelcomeMessage(ticketId, userId, category)).queue(); |
| 68 | + |
| 69 | + LOGGER.info( |
| 70 | + "Created ticket #{} for user {} in channel {}", |
| 71 | + ticketId, |
| 72 | + userId, |
| 73 | + channel.getIdLong()); |
| 74 | + return Optional.of(channel); |
| 75 | + } |
| 76 | + catch (Exception exception) { |
| 77 | + LOGGER.error("Failed to create ticket for user {}", userId, exception); |
| 78 | + return Optional.empty(); |
| 79 | + } |
| 80 | + }); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + public CompletableFuture<Boolean> closeTicket(long channelId, long staffId, String reason) { |
| 85 | + return this.ticketService.getTicketByChannel(channelId).thenCompose(ticketOpt -> { |
| 86 | + if (ticketOpt.isEmpty()) { |
| 87 | + LOGGER.warn("Attempted to close non-existent ticket for channel {}", channelId); |
| 88 | + return CompletableFuture.completedFuture(false); |
| 89 | + } |
| 90 | + |
| 91 | + TicketWrapper ticket = ticketOpt.get(); |
| 92 | + |
| 93 | + return this.ticketService.deleteTicket(ticket.getId()).thenApply(result -> { |
| 94 | + if (result > 0) { |
| 95 | + this.transcriptGeneratorService.generateAndSendTranscript(ticket, staffId, reason); |
| 96 | + |
| 97 | + TextChannel channel = this.jda.getTextChannelById(channelId); |
| 98 | + if (channel != null) { |
| 99 | + channel.delete().queue( |
| 100 | + success -> LOGGER.info("Successfully closed and deleted ticket #{}", ticket.getId()), |
| 101 | + failure -> LOGGER.error( |
| 102 | + "Failed to delete channel {} for ticket #{}", |
| 103 | + channelId, |
| 104 | + ticket.getId(), |
| 105 | + failure) |
| 106 | + ); |
| 107 | + } |
| 108 | + return true; |
| 109 | + } |
| 110 | + LOGGER.warn("Failed to delete ticket #{} from database", ticket.getId()); |
| 111 | + return false; |
| 112 | + }); |
| 113 | + }).exceptionally(exception -> { |
| 114 | + LOGGER.error("Error closing ticket for channel {}", channelId, exception); |
| 115 | + return false; |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + public CompletableFuture<Void> cleanupInactiveTickets() { |
| 120 | + Instant cutoffTime = Instant.now().minus(this.config.getAutoCloseDuration()); |
| 121 | + |
| 122 | + return this.ticketService.findTicketsOlderThan(cutoffTime) |
| 123 | + .thenCompose(tickets -> CompletableFuture.allOf( |
| 124 | + tickets.stream() |
| 125 | + .map(ticket -> this.closeTicket( |
| 126 | + ticket.getChannelId(), |
| 127 | + 0L, |
| 128 | + "Automatically closed due to inactivity") |
| 129 | + .thenApply(result -> (Void) null)) |
| 130 | + .toArray(CompletableFuture[]::new) |
| 131 | + )); |
| 132 | + } |
| 133 | + |
| 134 | + public MessageCreateData createWelcomeMessage( |
| 135 | + long ticketId, |
| 136 | + long userId, |
| 137 | + TicketConfig.TicketCategoryConfig category) { |
| 138 | + |
| 139 | + Instant now = Instant.now(); |
| 140 | + |
| 141 | + return new MessageCreateBuilder() |
| 142 | + .setContent("<@" + userId + ">") |
| 143 | + .setEmbeds(new EmbedBuilder() |
| 144 | + .setTitle("🎫 Ticket #" + ticketId) |
| 145 | + .setDescription(this.config.messages.ticketCreated) |
| 146 | + .addField("📋 Category", category.displayName, true) |
| 147 | + .addField("👤 User", "<@" + userId + ">", true) |
| 148 | + .addField("📅 Created", "<t:" + now.getEpochSecond() + ":F>", true) |
| 149 | + .setColor(Color.decode(this.config.embeds.color)) |
| 150 | + .setTimestamp(this.config.embeds.showTimestamp ? now : null) |
| 151 | + .build()) |
| 152 | + .addActionRow(Button.danger("ticket_close", "🔒 Close")) |
| 153 | + .build(); |
| 154 | + } |
| 155 | + |
| 156 | + public MessageEmbed createEmbed(String title, String description) { |
| 157 | + EmbedBuilder builder = new EmbedBuilder() |
| 158 | + .setTitle(title) |
| 159 | + .setDescription(description) |
| 160 | + .setColor(Color.decode(this.config.embeds.color)); |
| 161 | + |
| 162 | + if (UrlValidator.isValid(this.config.embeds.thumbnail)) { |
| 163 | + builder.setThumbnail(this.config.embeds.thumbnail); |
| 164 | + } |
| 165 | + |
| 166 | + if (this.config.embeds.footerText != null && !this.config.embeds.footerText.trim().isEmpty()) { |
| 167 | + builder.setFooter( |
| 168 | + this.config.embeds.footerText, |
| 169 | + UrlValidator.isValid(this.config.embeds.footerIcon) ? this.config.embeds.footerIcon : null); |
| 170 | + } |
| 171 | + |
| 172 | + if (this.config.embeds.showTimestamp) { |
| 173 | + builder.setTimestamp(Instant.now()); |
| 174 | + } |
| 175 | + |
| 176 | + return builder.build(); |
| 177 | + } |
| 178 | + |
| 179 | + private TextChannel createChannel(long userId, TicketConfig.TicketCategoryConfig category, long ticketId) { |
| 180 | + Category cat = this.jda.getCategoryById(this.config.categoryId); |
| 181 | + if (cat == null) { |
| 182 | + throw new IllegalStateException("Category not found: " + this.config.categoryId); |
| 183 | + } |
| 184 | + |
| 185 | + User user = this.jda.getUserById(userId); |
| 186 | + String userName = user != null ? user.getName() : "user" + userId; |
| 187 | + long count = this.ticketService.countTicketsByUser(userId).join(); |
| 188 | + String channelName = count == 0 ? "ticket-" + userName : "ticket-" + userName + "-" + (count + 1); |
| 189 | + |
| 190 | + TextChannel channel = cat.createTextChannel(channelName) |
| 191 | + .setTopic("Ticket " + category.displayName) |
| 192 | + .complete(); |
| 193 | + |
| 194 | + this.setupPermissions(channel, userId); |
| 195 | + return channel; |
| 196 | + } |
| 197 | + |
| 198 | + private void setupPermissions(TextChannel channel, long userId) { |
| 199 | + Guild guild = channel.getGuild(); |
| 200 | + |
| 201 | + channel.getManager() |
| 202 | + .putPermissionOverride(guild.getPublicRole(), null, List.of(Permission.VIEW_CHANNEL)) |
| 203 | + .queue(); |
| 204 | + |
| 205 | + Member member = guild.getMemberById(userId); |
| 206 | + if (member != null) { |
| 207 | + channel.getManager() |
| 208 | + .putPermissionOverride( |
| 209 | + member, |
| 210 | + List.of(Permission.VIEW_CHANNEL, Permission.MESSAGE_SEND, Permission.MESSAGE_HISTORY), |
| 211 | + null) |
| 212 | + .queue(); |
| 213 | + } |
| 214 | + |
| 215 | + if (this.config.staffRoleId != 0L) { |
| 216 | + Role staffRole = guild.getRoleById(this.config.staffRoleId); |
| 217 | + if (staffRole != null) { |
| 218 | + channel.getManager() |
| 219 | + .putPermissionOverride( |
| 220 | + staffRole, |
| 221 | + List.of( |
| 222 | + Permission.VIEW_CHANNEL, |
| 223 | + Permission.MESSAGE_SEND, |
| 224 | + Permission.MESSAGE_HISTORY, |
| 225 | + Permission.MESSAGE_MANAGE), |
| 226 | + null) |
| 227 | + .queue(); |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments