| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,6 +117,12 @@ public class ModerationConfig extends GuildConfigItem { | |||
| 117 | 117 | */ | |
| 118 | 118 | private String banMessageText = "Looks like you've been banned from the Discord Java Community. If you want to appeal this decision please fill out our form at <https://airtable.com/shrp5V4H1U5TYOXyC>."; | |
| 119 | 119 | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * Text that is sent to users when they're unbanned. | ||
| 123 | + */ | ||
| 124 | + private String unbanMessageText = "You have been unbanned. You can now rejoin at <https://join.discordjug.net/> but please ensure you are following the rules if you do."; | ||
| 125 | + | ||
| 120 | 126 | /** | |
| 121 | 127 | * Text that is sent to users when they're banned. | |
| 122 | 128 | */ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,7 @@ | |||
| 10 | 10 | import net.discordjug.javabot.util.Responses; | |
| 11 | 11 | import net.discordjug.javabot.util.UserUtils; | |
| 12 | 12 | import net.dv8tion.jda.api.EmbedBuilder; | |
| 13 | + import net.dv8tion.jda.api.JDA; | ||
| 13 | 14 | import net.dv8tion.jda.api.entities.*; | |
| 14 | 15 | import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; | |
| 15 | 16 | import net.dv8tion.jda.api.utils.MarkdownUtil; | |
@@ -20,16 +21,20 @@ | |||
| 20 | 21 | ||
| 21 | 22 | import lombok.RequiredArgsConstructor; | |
| 22 | 23 | ||
| 24 | + import javax.annotation.CheckReturnValue; | ||
| 23 | 25 | import javax.annotation.Nonnull; | |
| 24 | 26 | import java.time.Duration; | |
| 25 | 27 | import java.time.Instant; | |
| 26 | 28 | import java.time.LocalDateTime; | |
| 27 | 29 | import java.time.ZoneOffset; | |
| 30 | + import java.util.ArrayList; | ||
| 28 | 31 | import java.util.Collections; | |
| 29 | 32 | import java.util.List; | |
| 30 | 33 | import java.util.Optional; | |
| 34 | + import java.util.concurrent.CompletableFuture; | ||
| 31 | 35 | import java.util.concurrent.ExecutorService; | |
| 32 | 36 | import java.util.concurrent.TimeUnit; | |
| 37 | + import java.util.function.Consumer; | ||
| 33 | 38 | ||
| 34 | 39 | /** | |
| 35 | 40 | * This service provides methods for performing moderation actions, like banning | |
@@ -281,23 +286,44 @@ private void banAndSendGuildNotifications(User user, String reason, Member banne | |||
| 281 | 286 | * @param quiet If true, don't send a message in the channel. | |
| 282 | 287 | * @return Whether the member is banned or not. | |
| 283 | 288 | */ | |
| 284 | - public boolean unban(long userId, String reason, Member bannedBy, MessageChannel channel, boolean quiet) { | ||
| 289 | + @CheckReturnValue | ||
| 290 | + public CompletableFuture<?> unban(long userId, String reason, Member bannedBy, MessageChannel channel, boolean quiet) { | ||
| 285 | 291 | MessageEmbed unbanEmbed = this.buildUnbanEmbed(userId, reason, bannedBy); | |
| 286 | - boolean isBanned = isBanned(bannedBy.getGuild(), userId); | ||
| 287 | 292 | ModerationConfig moderationConfig = getModerationConfig(bannedBy); | |
| 288 | - if (isBanned) { | ||
| 289 | - bannedBy.getGuild().unban(User.fromId(userId)).queue(s -> { | ||
| 290 | - moderationConfig.getLogChannel().sendMessageEmbeds(unbanEmbed).queue(); | ||
| 291 | - if (!quiet) channel.sendMessageEmbeds(unbanEmbed).queue(); | ||
| 292 | - }, ExceptionLogger::capture); | ||
| 293 | - } | ||
| 294 | - return isBanned; | ||
| 293 | + return bannedBy.getGuild().unban(User.fromId(userId)).reason(reason).map(_ -> { | ||
| 294 | + moderationConfig.getLogChannel().sendMessageEmbeds(unbanEmbed).queue(unbanLogMessage -> notifyUserAboutUnban(userId, unbanEmbed, moderationConfig, unbanLogMessage)); | ||
| 295 | + if (!quiet) { | ||
| 296 | + channel.sendMessageEmbeds(unbanEmbed).queue(); | ||
| 297 | + } | ||
| 298 | + return null; | ||
| 299 | + }).submit(); | ||
| 295 | 300 | } | |
| 296 | 301 | ||
| 297 | - private boolean isBanned(@NotNull Guild guild, long userId) { | ||
| 298 | - return guild.retrieveBanList().complete() | ||
| 299 | - .stream().map(Guild.Ban::getUser) | ||
| 300 | - .map(User::getIdLong).toList().contains(userId); | ||
| 302 | + private void notifyUserAboutUnban(long userId, MessageEmbed unbanEmbed, ModerationConfig moderationConfig, Message unbanLogMessage) { | ||
| 303 | + JDA jda = moderationConfig.getGuild().getJDA(); | ||
| 304 | + jda.retrieveUserById(userId) | ||
| 305 | + .flatMap(User::openPrivateChannel) | ||
| 306 | + .flatMap(c -> c.getHistory().retrievePast(1)) | ||
| 307 | + .queue(history -> { | ||
| 308 | + if (history.isEmpty()) { | ||
| 309 | + return; | ||
| 310 | + } | ||
| 311 | + Message banMessage = history.getFirst(); | ||
| 312 | + if (banMessage.getAuthor().getIdLong() != jda.getSelfUser().getIdLong()) { | ||
| 313 | + return; | ||
| 314 | + } | ||
| 315 | + ArrayList<MessageEmbed> embeds = new ArrayList<>(banMessage.getEmbeds()); | ||
| 316 | + embeds.add(new EmbedBuilder(unbanEmbed).setColor(Responses.Type.SUCCESS.getColor()).build()); | ||
| 317 | + banMessage.editMessageEmbeds(embeds).setContent(moderationConfig.getUnbanMessageText()).queue(success -> { | ||
| 318 | + List<MessageEmbed> unbanLogEmbeds = unbanLogMessage.getEmbeds(); | ||
| 319 | + if (unbanLogEmbeds.isEmpty()) { | ||
| 320 | + return; | ||
| 321 | + } | ||
| 322 | + unbanLogMessage.editMessageEmbeds(new EmbedBuilder(unbanLogEmbeds.getLast()) | ||
| 323 | + .addField("User informed", "The ban info in the user's DMs has been updated.", true).build()) | ||
| 324 | + .queue(); | ||
| 325 | + }); | ||
| 326 | + }); | ||
| 301 | 327 | } | |
| 302 | 328 | ||
| 303 | 329 | /** | |
@@ -329,8 +355,19 @@ public void sendBanGuildNotification(User user, String reason, Member moderator) | |||
| 329 | 355 | sendGuildNotification(moderator.getGuild(), buildBanEmbed(user, moderator, reason)); | |
| 330 | 356 | } | |
| 331 | 357 | ||
| 358 | + /** | ||
| 359 | + * Sends an unban notification to the guild log. | ||
| 360 | + * | ||
| 361 | + * This will also try to update the ban notification of the user to say they are unbanned. | ||
| 362 | + * @param user The unbanned user | ||
| 363 | + * @param reason The reason they were unbanned | ||
| 364 | + * @param moderator The moderator unbanning them (That {@link Member}'s {@link Guild} is used to determine the guild log to send the notification to. | ||
| 365 | + */ | ||
| 332 | 366 | public void sendUnbanGuildNotification(User user, String reason, Member moderator) { | |
| 333 | - sendGuildNotification(moderator.getGuild(), buildUnbanEmbed(user.getIdLong(), reason, moderator)); | ||
| 367 | + MessageEmbed unbanEmbed = buildUnbanEmbed(user.getIdLong(), reason, moderator); | ||
| 368 | + sendGuildNotification(moderator.getGuild(), unbanEmbed, msg -> { | ||
| 369 | + notifyUserAboutUnban(user.getIdLong(), unbanEmbed, botConfig.get(moderator.getGuild()).getModerationConfig(), msg); | ||
| 370 | + }); | ||
| 334 | 371 | } | |
| 335 | 372 | ||
| 336 | 373 | public void sendTimeoutGuildNotification(User user, String reason, Member moderator, Duration duration) { | |
@@ -342,12 +379,19 @@ public void sendRemoveTimeoutGuildNotification(User user, String reason, Member | |||
| 342 | 379 | } | |
| 343 | 380 | ||
| 344 | 381 | private void sendGuildNotification(Guild guild, MessageEmbed embed) { | |
| 382 | + sendGuildNotification(guild, embed, _ -> {}); | ||
| 383 | + } | ||
| 384 | + | ||
| 385 | + private void sendGuildNotification(Guild guild, MessageEmbed embed, Consumer<Message> onComplete) { | ||
| 345 | 386 | MessageEmbed newEmbed = new EmbedBuilder(embed) | |
| 346 | 387 | .addField("Source", "This action was executed manually without a bot command.", false) | |
| 347 | 388 | .build(); | |
| 348 | 389 | notificationService | |
| 349 | 390 | .withGuild(guild) | |
| 350 | - .sendToModerationLog(c -> c.sendMessageEmbeds(newEmbed)); | ||
| 391 | + .sendToModerationLog(c -> c.sendMessageEmbeds(newEmbed).map(success -> { | ||
| 392 | + onComplete.accept(success); | ||
| 393 | + return success; | ||
| 394 | + })); | ||
| 351 | 395 | } | |
| 352 | 396 | ||
| 353 | 397 | private @NotNull EmbedBuilder buildModerationEmbed(@NotNull User user, @NotNull Member moderator, String reason) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -43,10 +43,13 @@ protected ReplyCallbackAction handleModerationCommand(@NotNull SlashCommandInter | |||
| 43 | 43 | } | |
| 44 | 44 | long id = idOption.getAsLong(); | |
| 45 | 45 | boolean quiet = ModerateUserCommand.isQuiet(botConfig, event); | |
| 46 | - if (moderationService.unban(id, reasonOption.getAsString(), event.getMember(), event.getChannel(), quiet)) { | ||
| 47 | - return Responses.success(event, "User Unbanned", "User with id `%s` has been unbanned.", id); | ||
| 48 | - } else { | ||
| 49 | - return Responses.warning(event, "Could not find banned User with id `%s`", id); | ||
| 50 | - } | ||
| 46 | + moderationService.unban(id, reasonOption.getAsString(), event.getMember(), event.getChannel(), quiet) | ||
| 47 | + .thenAccept(success -> { | ||
| 48 | + Responses.success(event.getHook(), "User Unbanned", "User with id `%s` has been unbanned.", id).queue(); | ||
| 49 | + }).exceptionally(failed -> { | ||
| 50 | + Responses.warning(event.getHook(), "Could not find banned User with id `%s` or a different error occured: `%s`", id, failed.getMessage()).queue(); | ||
| 51 | + return null; | ||
| 52 | + }); | ||
| 53 | + return event.deferReply(); | ||
| 51 | 54 | } | |
| 52 | 55 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,7 +4,9 @@ | |||
| 4 | 4 | import lombok.RequiredArgsConstructor; | |
| 5 | 5 | import lombok.extern.slf4j.Slf4j; | |
| 6 | 6 | import net.discordjug.javabot.data.config.GuildConfig; | |
| 7 | + import net.dv8tion.jda.api.entities.Message; | ||
| 7 | 8 | import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; | |
| 9 | + import net.dv8tion.jda.api.requests.RestAction; | ||
| 8 | 10 | import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; | |
| 9 | 11 | import org.jetbrains.annotations.NotNull; | |
| 10 | 12 | ||
@@ -22,9 +24,9 @@ public final class GuildNotificationService extends NotificationService.MessageC | |||
| 22 | 24 | /** | |
| 23 | 25 | * Sends a notification to the log channel. | |
| 24 | 26 | * | |
| 25 | - * @param function The {@link Function} to use which MUST return a {@link MessageCreateAction}. | ||
| 27 | + * @param function A {@link Function} sending the message. | ||
| 26 | 28 | */ | |
| 27 | - public void sendToModerationLog(@NotNull Function<MessageChannel, MessageCreateAction> function) { | ||
| 29 | + public void sendToModerationLog(@NotNull Function<MessageChannel, RestAction<? extends Message>> function) { | ||
| 28 | 30 | MessageChannel channel = guildConfig.getModerationConfig().getLogChannel(); | |
| 29 | 31 | if (channel == null) { | |
| 30 | 32 | log.error("Could not send message to LogChannel in guild " + guildConfig.getGuild().getId()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,9 +5,10 @@ | |||
| 5 | 5 | import net.discordjug.javabot.data.config.BotConfig; | |
| 6 | 6 | import net.discordjug.javabot.systems.qotw.QOTWPointsService; | |
| 7 | 7 | import net.dv8tion.jda.api.entities.Guild; | |
| 8 | + import net.dv8tion.jda.api.entities.Message; | ||
| 8 | 9 | import net.dv8tion.jda.api.entities.User; | |
| 9 | 10 | import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; | |
| 10 | - import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; | ||
| 11 | + import net.dv8tion.jda.api.requests.RestAction; | ||
| 11 | 12 | import org.jetbrains.annotations.Contract; | |
| 12 | 13 | import org.jetbrains.annotations.NotNull; | |
| 13 | 14 | import org.springframework.stereotype.Service; | |
@@ -58,7 +59,7 @@ abstract static class MessageChannelNotification { | |||
| 58 | 59 | * @param channel The target {@link MessageChannel}. | |
| 59 | 60 | * @param function The {@link Function} which is used in order to send the message. | |
| 60 | 61 | */ | |
| 61 | - protected void send(MessageChannel channel, @NotNull Function<MessageChannel, MessageCreateAction> function) { | ||
| 62 | + protected void send(MessageChannel channel, @NotNull Function<MessageChannel, ? extends RestAction<? extends Message>> function) { | ||
| 62 | 63 | function.apply(channel).queue(s -> {}, | |
| 63 | 64 | err -> log.error("Could not send message to channel \" " + channel.getName() + "\": ", err) | |
| 64 | 65 | ); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments