|
3 | 3 | import net.dv8tion.jda.api.EmbedBuilder;
|
4 | 4 | import net.dv8tion.jda.api.JDA;
|
5 | 5 | import net.dv8tion.jda.api.entities.Guild;
|
| 6 | +import net.dv8tion.jda.api.entities.Member; |
| 7 | +import net.dv8tion.jda.api.entities.Message; |
6 | 8 | import net.dv8tion.jda.api.entities.MessageEmbed;
|
7 | 9 | import net.dv8tion.jda.api.entities.channel.concrete.ForumChannel;
|
8 | 10 | import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel;
|
9 | 11 | import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
|
10 |
| -import net.dv8tion.jda.api.exceptions.ErrorResponseException; |
| 12 | +import net.dv8tion.jda.api.requests.RestAction; |
| 13 | +import net.dv8tion.jda.api.utils.Result; |
11 | 14 | import net.dv8tion.jda.api.utils.TimeUtil;
|
12 | 15 | import org.slf4j.Logger;
|
13 | 16 | import org.slf4j.LoggerFactory;
|
|
19 | 22 | import java.util.List;
|
20 | 23 | import java.util.Optional;
|
21 | 24 | import java.util.concurrent.TimeUnit;
|
22 |
| -import java.util.function.Consumer; |
| 25 | +import java.util.function.Function; |
| 26 | +import java.util.function.Supplier; |
23 | 27 |
|
24 | 28 | /**
|
25 | 29 | * Routine, which periodically checks all help threads and archives them if there has not been any
|
@@ -122,18 +126,27 @@ private static boolean shouldBeArchived(MessageChannel channel, Instant archiveA
|
122 | 126 | }
|
123 | 127 |
|
124 | 128 | private void handleArchiveFlow(ThreadChannel threadChannel, MessageEmbed embed) {
|
125 |
| - Consumer<Throwable> handleFailure = error -> { |
126 |
| - if (error instanceof ErrorResponseException) { |
127 |
| - logger.warn("Unknown error occurred during help thread auto archive routine", |
128 |
| - error); |
129 |
| - } |
130 |
| - }; |
| 129 | + |
| 130 | + Function<Result<Member>, RestAction<Message>> sendEmbedWithMention = |
| 131 | + member -> threadChannel.sendMessage(member.get().getAsMention()).addEmbeds(embed); |
| 132 | + |
| 133 | + Supplier<RestAction<Message>> sendEmbedWithoutMention = |
| 134 | + () -> threadChannel.sendMessageEmbeds(embed); |
131 | 135 |
|
132 | 136 | threadChannel.getGuild()
|
133 | 137 | .retrieveMemberById(threadChannel.getOwnerIdLong())
|
134 |
| - .flatMap(author -> threadChannel.sendMessage(author.getAsMention()).addEmbeds(embed)) |
| 138 | + .mapToResult() |
| 139 | + .flatMap(foundMember -> { |
| 140 | + if (foundMember.isSuccess()) { |
| 141 | + return sendEmbedWithMention.apply(foundMember); |
| 142 | + } |
| 143 | + logger.info( |
| 144 | + "Owner of thread with id: {} left the server, sending embed without mention", |
| 145 | + threadChannel.getId(), foundMember.getFailure()); |
| 146 | + |
| 147 | + return sendEmbedWithoutMention.get(); |
| 148 | + }) |
135 | 149 | .flatMap(any -> threadChannel.getManager().setArchived(true))
|
136 |
| - .queue(any -> { |
137 |
| - }, handleFailure); |
| 150 | + .queue(); |
138 | 151 | }
|
139 | 152 | }
|
0 commit comments