|
17 | 17 | import java.util.ArrayList; |
18 | 18 | import java.util.Collections; |
19 | 19 | import java.util.List; |
| 20 | +import java.util.function.Consumer; |
| 21 | +import java.util.function.Function; |
20 | 22 |
|
21 | 23 | @Service |
22 | 24 | public class ApplicationArchiveService { |
23 | 25 |
|
24 | | - private final MongoUserService mongoUserService; |
25 | | - private final EnvironmentService environmentService; |
26 | | - private final DiscordLogService discordLogService; |
27 | | - private final MojangAPI mojangAPI; |
28 | | - |
29 | | - public ApplicationArchiveService(MongoUserService mongoUserService, EnvironmentService environmentService, DiscordLogService discordLogService, MojangAPI mojangAPI) { |
30 | | - this.mongoUserService = mongoUserService; |
31 | | - this.environmentService = environmentService; |
32 | | - this.discordLogService = discordLogService; |
33 | | - this.mojangAPI = mojangAPI; |
34 | | - } |
35 | | - |
36 | | - /** |
37 | | - * @param callback guarantee running after archival |
38 | | - */ |
39 | | - public void archiveApplication(TextChannel applicationChannel, Runnable callback) { |
40 | | - // Note: using complete()'s would make this way more pretty and eliminate the need for a callback. However, thank JDA for making that throw an error inside of |
41 | | - // calling callbacks because of some edge-cases -.- |
42 | | - if(applicationChannel == null) return; |
43 | | - |
44 | | - NinetalesUser ntUser = mongoUserService.getUserByApplicationChannelId(applicationChannel.getIdLong()); |
45 | | - if (ntUser == null) { |
46 | | - return; |
47 | | - } |
48 | | - ForumChannel forum = null; |
49 | | - String archContent = null; |
50 | | - String nPrefix = ""; |
51 | | - if (ntUser.getGuildApplicationChannelId() != null) { |
52 | | - String arch = environmentService.getGuildApplicationsArchiveForum(); |
53 | | - if (arch == null) return; |
54 | | - forum = applicationChannel.getGuild().getForumChannelById(arch); |
55 | | - archContent = "Archive of <@" + ntUser.getDiscordId() + ">'s guild application"; |
56 | | - nPrefix = ntUser.isAwaitingHypixelInvite() ? "Accepted - " : "Denied - "; |
57 | | - } |
58 | | - if (ntUser.getDiscordApplicationChannelId() != null) { |
59 | | - String arch = environmentService.getDiscordApplicationsArchiveForum(); |
60 | | - if (arch == null) return; |
61 | | - forum = applicationChannel.getGuild().getForumChannelById(arch); |
62 | | - archContent = "Archive of <@" + ntUser.getDiscordId() + ">'s discord application"; |
63 | | - nPrefix = ntUser.isDiscordMember() ? "Accepted - " : "Denied - "; |
64 | | - } |
65 | | - |
66 | | - final String finalArchContent = archContent; |
67 | | - final ForumChannel finalForum = forum; |
68 | | - final String finalNPrefix = nPrefix; |
69 | | - applicationChannel.getGuild().retrieveMemberById(ntUser.getDiscordId()).queue(ntMember -> { |
70 | | - if (finalForum == null) return; |
71 | | - |
72 | | - String mcName = mojangAPI.getUsername(ntUser.getMinecraftUuid()); |
73 | | - if (mcName == null) mcName = applicationChannel.getName(); |
74 | | - |
75 | | - final String finalChName = finalNPrefix + mcName; |
76 | | - applicationChannel.getHistory().retrievePast(100).queue(ml -> { |
77 | | - List<Message> messages = new ArrayList<>(ml); |
78 | | - Collections.reverse(messages); |
79 | | - |
80 | | - finalForum.createForumPost(finalChName, MessageCreateData.fromContent(finalArchContent)).queue(archiveChannel -> { |
81 | | - List<MessageEmbed> embeds = new ArrayList<>(); |
82 | | - messages.forEach(message -> { |
83 | | - Color c = message.getMember() != null ? message.getMember().getColor() : null; |
84 | | - if(message.getMember() != null) { |
85 | | - if (ntMember != null && ntMember.getId().equals(message.getMember().getId())) c = ntMember.getColor(); // more up-to-date |
86 | | - } |
87 | | - EmbedBuilder eb = new EmbedBuilder() |
88 | | - .setAuthor(message.getAuthor().getEffectiveName(), null, message.getAuthor().getAvatarUrl()) |
89 | | - .setTimestamp(message.getTimeCreated()) |
90 | | - .setDescription(message.getContentDisplay()) |
91 | | - .setColor(c); |
92 | | - embeds.add(eb.build()); |
93 | | - |
94 | | - }); |
95 | | - |
96 | | - // we can send up to 10 embeds at a time, so group. |
97 | | - List<List<MessageEmbed>> partitions = new ArrayList<>(); |
98 | | - for (int i = 0; i < embeds.size(); i += 10) { |
99 | | - partitions.add(embeds.subList(i, Math.min(i + 10, embeds.size()))); |
100 | | - } |
101 | | - if (ntMember != null) { |
102 | | - ntMember.getUser().openPrivateChannel().queue(pc -> { |
103 | | - String ap = ntUser.getDiscordApplicationChannelId() != null ? "discord" : "guild"; |
104 | | - pc.sendMessage("Below is a transcript of your recent application to join the Ninetales %s:".formatted(ap)).queue(); |
105 | | - partitions.forEach(m -> { |
106 | | - pc.sendMessageEmbeds(m).queue(); |
107 | | - }); |
108 | | - }); |
109 | | - } |
110 | | - |
111 | | - partitions.forEach(m -> { |
112 | | - archiveChannel.getThreadChannel().sendMessageEmbeds(m).queue(); |
113 | | - }); |
114 | | - |
115 | | - discordLogService.debug("Application Archive", "Archived <@" + ntUser.getDiscordId() + ">'s application to " + archiveChannel.getThreadChannel().getJumpUrl()); |
116 | | - archiveChannel.getThreadChannel().getManager().setLocked(true).queue(); |
117 | | - |
118 | | - callback.run(); |
119 | | - }); |
120 | | - }); |
121 | | - }); |
122 | | - |
123 | | - } |
124 | | - |
| 26 | + private final MongoUserService mongoUserService; |
| 27 | + private final EnvironmentService environmentService; |
| 28 | + private final DiscordLogService discordLogService; |
| 29 | + private final MojangAPI mojangAPI; |
| 30 | + |
| 31 | + public ApplicationArchiveService(MongoUserService mongoUserService, EnvironmentService environmentService, DiscordLogService discordLogService, MojangAPI mojangAPI) { |
| 32 | + this.mongoUserService = mongoUserService; |
| 33 | + this.environmentService = environmentService; |
| 34 | + this.discordLogService = discordLogService; |
| 35 | + this.mojangAPI = mojangAPI; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param callback guarantee running after archival |
| 40 | + */ |
| 41 | + public void archiveApplication(TextChannel applicationChannel, TextChannel tailChannel, Runnable callback) { |
| 42 | + // Note: using complete()'s would make this way more pretty and eliminate the need for a callback. However, thank JDA for making that throw an error inside of |
| 43 | + // calling callbacks because of some edge-cases -.- |
| 44 | + if (applicationChannel == null || tailChannel == null) return; |
| 45 | + |
| 46 | + NinetalesUser ntUser = mongoUserService.getUserByApplicationChannelId(applicationChannel.getIdLong()); |
| 47 | + if (ntUser == null) { |
| 48 | + return; |
| 49 | + } |
| 50 | + ForumChannel forum = null; |
| 51 | + String archContent = null; |
| 52 | + String nPrefix = ""; |
| 53 | + if (ntUser.getGuildApplicationChannelId() != null) { |
| 54 | + String arch = environmentService.getGuildApplicationsArchiveForum(); |
| 55 | + if (arch == null) return; |
| 56 | + forum = applicationChannel.getGuild().getForumChannelById(arch); |
| 57 | + archContent = "Archive of <@" + ntUser.getDiscordId() + ">'s guild application"; |
| 58 | + nPrefix = ntUser.isAwaitingHypixelInvite() ? "Accepted - " : "Denied - "; |
| 59 | + } |
| 60 | + if (ntUser.getDiscordApplicationChannelId() != null) { |
| 61 | + String arch = environmentService.getDiscordApplicationsArchiveForum(); |
| 62 | + if (arch == null) return; |
| 63 | + forum = applicationChannel.getGuild().getForumChannelById(arch); |
| 64 | + archContent = "Archive of <@" + ntUser.getDiscordId() + ">'s discord application"; |
| 65 | + nPrefix = ntUser.isDiscordMember() ? "Accepted - " : "Denied - "; |
| 66 | + } |
| 67 | + |
| 68 | + final String finalArchContent = archContent; |
| 69 | + final ForumChannel finalForum = forum; |
| 70 | + final String finalNPrefix = nPrefix; |
| 71 | + applicationChannel.getGuild().retrieveMemberById(ntUser.getDiscordId()).queue(ntMember -> { |
| 72 | + if (finalForum == null) return; |
| 73 | + |
| 74 | + String mcName = mojangAPI.getUsername(ntUser.getMinecraftUuid()); |
| 75 | + if (mcName == null) mcName = applicationChannel.getName(); |
| 76 | + |
| 77 | + final String finalChName = finalNPrefix + mcName; |
| 78 | + |
| 79 | + finalForum.createForumPost(finalChName, MessageCreateData.fromContent(finalArchContent)).queue(archiveChannel -> { |
| 80 | + applicationChannel.getHistory().retrievePast(100).queue(appChannelRetrievedMsgs -> { |
| 81 | + List<Message> appChannelMsgs = new ArrayList<>(appChannelRetrievedMsgs); |
| 82 | + Collections.reverse(appChannelMsgs); |
| 83 | + |
| 84 | + // we can send up to 10 embeds at a time, so group. |
| 85 | + Function<List<MessageEmbed>, List<List<MessageEmbed>>> partitionEmbeds = (l) -> { |
| 86 | + List<List<MessageEmbed>> partitions = new ArrayList<>(); |
| 87 | + for (int i = 0; i < l.size(); i += 10) { |
| 88 | + partitions.add(l.subList(i, Math.min(i + 10, l.size()))); |
| 89 | + } |
| 90 | + return partitions; |
| 91 | + }; |
| 92 | + |
| 93 | + Function<List<Message>, List<MessageEmbed>> createMsgEmbeds = (msgs) -> { |
| 94 | + List<MessageEmbed> embeds = new ArrayList<>(); |
| 95 | + msgs.forEach(message -> { |
| 96 | + Color c = message.getMember() != null ? message.getMember().getColor() : null; |
| 97 | + if (message.getMember() != null) { |
| 98 | + if (ntMember != null && ntMember.getId().equals(message.getMember().getId())) { |
| 99 | + c = ntMember.getColor() != null ? ntMember.getColor() : c; // more up-to-date |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + EmbedBuilder eb = new EmbedBuilder() |
| 104 | + .setAuthor(message.getAuthor().getEffectiveName(), null, message.getAuthor().getAvatarUrl()) |
| 105 | + .setTimestamp(message.getTimeCreated()) |
| 106 | + .setDescription(message.getContentDisplay()) |
| 107 | + .setColor(c); |
| 108 | + |
| 109 | + embeds.add(eb.build()); |
| 110 | + }); |
| 111 | + return embeds; |
| 112 | + }; |
| 113 | + |
| 114 | + List<List<MessageEmbed>> appChannelPartitions = partitionEmbeds.apply(createMsgEmbeds.apply(appChannelMsgs)); |
| 115 | + |
| 116 | + if (ntMember != null) { |
| 117 | + ntMember.getUser().openPrivateChannel().queue(pc -> { |
| 118 | + String ap = ntUser.getDiscordApplicationChannelId() != null ? "discord" : "guild"; |
| 119 | + pc.sendMessage("Below is a transcript of your recent application to join the Ninetales %s:".formatted(ap)).queue(); |
| 120 | + appChannelPartitions.forEach(m -> { |
| 121 | + pc.sendMessageEmbeds(m).queue(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + archiveChannel.getThreadChannel().sendMessage("## Application Channel").queue(); |
| 127 | + appChannelPartitions.forEach(m -> { |
| 128 | + archiveChannel.getThreadChannel().sendMessageEmbeds(m).queue(); |
| 129 | + }); |
| 130 | + |
| 131 | + |
| 132 | + tailChannel.getHistory().retrievePast(100).queue(tailChannelRetrievedMsgs -> { |
| 133 | + List<Message> tailChannelMsgs = new ArrayList<>(tailChannelRetrievedMsgs); |
| 134 | + Collections.reverse(tailChannelMsgs); |
| 135 | + |
| 136 | + archiveChannel.getThreadChannel().sendMessage("## Tail Channel").queue(); |
| 137 | + List<List<MessageEmbed>> tailChannelPartitions = partitionEmbeds.apply(createMsgEmbeds.apply(tailChannelMsgs)); |
| 138 | + tailChannelPartitions.forEach(m -> { |
| 139 | + archiveChannel.getThreadChannel().sendMessageEmbeds(m).queue(); |
| 140 | + }); |
| 141 | + |
| 142 | + discordLogService.debug("Application Archive", "Archived <@" + ntUser.getDiscordId() + ">'s application to " + archiveChannel.getThreadChannel().getJumpUrl()); |
| 143 | + archiveChannel.getThreadChannel().getManager().setLocked(true).queue(); |
| 144 | + |
| 145 | + callback.run(); |
| 146 | + }); |
| 147 | + |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + }); |
| 152 | + |
| 153 | + } |
125 | 154 |
|
126 | 155 | } |
0 commit comments