|
3 | 3 | import net.dv8tion.jda.api.EmbedBuilder;
|
4 | 4 | import net.dv8tion.jda.api.Permission;
|
5 | 5 | import net.dv8tion.jda.api.entities.*;
|
6 |
| -import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel; |
7 | 6 | import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
|
8 | 7 | import net.dv8tion.jda.api.requests.RestAction;
|
9 | 8 | import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
|
| 9 | +import net.dv8tion.jda.api.utils.Result; |
| 10 | +import net.dv8tion.jda.internal.requests.CompletedRestAction; |
10 | 11 | import org.slf4j.Logger;
|
11 | 12 | import org.slf4j.LoggerFactory;
|
12 | 13 |
|
|
20 | 21 | import java.time.Instant;
|
21 | 22 | import java.time.temporal.ChronoUnit;
|
22 | 23 | import java.time.temporal.TemporalUnit;
|
23 |
| -import java.util.EnumSet; |
24 | 24 | import java.util.Optional;
|
25 |
| -import java.util.Set; |
26 | 25 | import java.util.function.Predicate;
|
27 |
| -import java.util.function.UnaryOperator; |
28 | 26 | import java.util.regex.Pattern;
|
29 | 27 |
|
30 | 28 | /**
|
@@ -52,25 +50,6 @@ private ModerationUtils() {
|
52 | 50 | */
|
53 | 51 | public static final Color AMBIENT_COLOR = Color.decode("#895FE8");
|
54 | 52 |
|
55 |
| - /** |
56 |
| - * Actions with timely constraint, like being muted for 1 hour. |
57 |
| - */ |
58 |
| - private static final Set<ModerationAction> TEMPORARY_ACTIONS = |
59 |
| - EnumSet.of(ModerationAction.MUTE); |
60 |
| - /** |
61 |
| - * Actions with revoking previously made actions on the user, like unmuting the user after it |
62 |
| - * has been muted. |
63 |
| - */ |
64 |
| - private static final Set<ModerationAction> REVOKE_ACTIONS = |
65 |
| - EnumSet.of(ModerationAction.UNMUTE, ModerationAction.UNQUARANTINE); |
66 |
| - /** |
67 |
| - * Soft violations were the user still remains member of the guild, such as a warning |
68 |
| - */ |
69 |
| - private static final Set<ModerationAction> SOFT_ACTIONS = |
70 |
| - EnumSet.of(ModerationAction.WARN, ModerationAction.QUARANTINE); |
71 |
| - |
72 |
| - |
73 |
| - |
74 | 53 | /**
|
75 | 54 | * Checks whether the given reason is valid. If not, it will handle the situation and respond to
|
76 | 55 | * the user.
|
@@ -390,64 +369,71 @@ static Optional<TemporaryData> computeTemporaryData(String durationText) {
|
390 | 369 | }
|
391 | 370 |
|
392 | 371 | /**
|
393 |
| - * Wrapper to hold data relevant to temporary actions, for example the time it expires. |
| 372 | + * Creates a nice looking embed for the mod action taken. |
394 | 373 | *
|
395 |
| - * @param expiresAt the time the temporary action expires |
396 |
| - * @param duration a human-readable text representing the duration of the temporary action, such |
397 |
| - * as {@code "1 day"}. |
| 374 | + * @param guild the guild in which the action has been taken |
| 375 | + * @param actionTitle the mod action as title e.g, Ban |
| 376 | + * @param description a short description explaining the action |
| 377 | + * @param reason reason for the action taken |
| 378 | + * @param showModmailAdvice whether to advice on how to use the modmail command |
| 379 | + * @return the embed |
398 | 380 | */
|
399 |
| - record TemporaryData(Instant expiresAt, String duration) { |
| 381 | + static RestAction<EmbedBuilder> getModActionEmbed(Guild guild, String actionTitle, |
| 382 | + String description, String reason, boolean showModmailAdvice) { |
| 383 | + EmbedBuilder modActionEmbed = |
| 384 | + new EmbedBuilder().setAuthor(guild.getName(), null, guild.getIconUrl()) |
| 385 | + .setTitle(actionTitle) |
| 386 | + .setDescription(description) |
| 387 | + .addField("Reason", reason, false) |
| 388 | + .setColor(ModerationUtils.AMBIENT_COLOR); |
| 389 | + |
| 390 | + if (!showModmailAdvice) { |
| 391 | + return new CompletedRestAction<>(guild.getJDA(), modActionEmbed); |
| 392 | + } |
| 393 | + |
| 394 | + return MessageUtils.mentionGlobalSlashCommand(guild.getJDA(), ModMailCommand.COMMAND_NAME) |
| 395 | + .map(commandMention -> modActionEmbed.appendDescription( |
| 396 | + "%n%nTo get in touch with a moderator, you can use the %s command here in this chat. Your message will then be forwarded and a moderator will get back to you soon 😊" |
| 397 | + .formatted(commandMention))); |
400 | 398 | }
|
401 | 399 |
|
402 | 400 | /**
|
403 |
| - * Gives out advice depending on the {@link ModerationAction} and the parameters passed into it. |
| 401 | + * Creates a nice looking embed for the mod action taken with duration |
404 | 402 | *
|
405 |
| - * @param action the action that is being performed, such as banning a user. |
406 |
| - * @param temporaryData if the action is a temporary action, such as a 1 hour mute. |
407 |
| - * @param additionalDescription any extra description that should be part of the message, if |
408 |
| - * desired |
409 |
| - * @param guild for which the action was triggered. |
410 |
| - * @param reason for the action. |
411 |
| - * @param textChannel for which messages are being sent to. |
412 |
| - * |
413 |
| - * @return the appropriate advice. |
| 403 | + * @param guild the guild in which the action has been taken |
| 404 | + * @param actionTitle the mod action itself |
| 405 | + * @param description a short description explaining the action |
| 406 | + * @param reason reason for the action taken |
| 407 | + * @param duration the duration of mod action |
| 408 | + * @param showModmailAdvice whether to advice on how to use the modmail command |
| 409 | + * @return the embed |
414 | 410 | */
|
415 |
| - public static RestAction<Message> sendDmAdvice(ModerationAction action, |
416 |
| - @Nullable TemporaryData temporaryData, @Nullable String additionalDescription, |
417 |
| - Guild guild, String reason, PrivateChannel textChannel) { |
418 |
| - String additionalDescriptionInfix = |
419 |
| - additionalDescription == null ? "" : "\n" + additionalDescription; |
420 |
| - |
421 |
| - if (REVOKE_ACTIONS.contains(action)) { |
422 |
| - return textChannel.sendMessage(""" |
423 |
| - Hey there, you have been %s in the server %s.%s |
424 |
| - The reason for being %s is: %s |
425 |
| - """.formatted(action.getVerb(), guild.getName(), additionalDescriptionInfix, |
426 |
| - action.getVerb(), reason)); |
427 |
| - } |
428 |
| - String durationMessage; |
429 |
| - if (SOFT_ACTIONS.contains(action)) { |
430 |
| - durationMessage = ""; |
431 |
| - } else if (TEMPORARY_ACTIONS.contains(action)) { |
432 |
| - durationMessage = |
433 |
| - temporaryData == null ? " permanently" : " for " + temporaryData.duration(); |
434 |
| - } else { |
435 |
| - throw new IllegalArgumentException( |
436 |
| - "Action '%s' is not supported by this method".formatted(action)); |
437 |
| - } |
| 411 | + static RestAction<EmbedBuilder> getModActionEmbed(Guild guild, String actionTitle, |
| 412 | + String description, String reason, String duration, boolean showModmailAdvice) { |
| 413 | + return getModActionEmbed(guild, actionTitle, description, reason, showModmailAdvice) |
| 414 | + .map(embedBuilder -> embedBuilder.addField("Duration", duration, false)); |
| 415 | + } |
438 | 416 |
|
439 |
| - UnaryOperator<String> createDmMessage = |
440 |
| - commandMention -> """ |
441 |
| - Hey there, sorry to tell you but unfortunately you have been %s%s in the server %s.%s |
442 |
| - To get in touch with a moderator, you can simply use the %s command here in this chat. \ |
443 |
| - Your message will then be forwarded and a moderator will get back to you soon 😊 |
444 |
| - The reason for being %s is: %s |
445 |
| - """ |
446 |
| - .formatted(action.getVerb(), durationMessage, guild.getName(), |
447 |
| - additionalDescriptionInfix, commandMention, action.getVerb(), reason); |
| 417 | + /** |
| 418 | + * @param embedBuilder rest action to generate embed from |
| 419 | + * @param target the user to send the generated embed |
| 420 | + * @return boolean rest action, weather the dm is sent successfully |
| 421 | + */ |
| 422 | + static RestAction<Boolean> sendModActionDm(RestAction<EmbedBuilder> embedBuilder, User target) { |
| 423 | + return embedBuilder.map(EmbedBuilder::build) |
| 424 | + .flatMap(embed -> target.openPrivateChannel() |
| 425 | + .flatMap(channel -> channel.sendMessageEmbeds(embed))) |
| 426 | + .mapToResult() |
| 427 | + .map(Result::isSuccess); |
| 428 | + } |
448 | 429 |
|
449 |
| - return MessageUtils.mentionGlobalSlashCommand(guild.getJDA(), ModMailCommand.COMMAND_NAME) |
450 |
| - .map(createDmMessage) |
451 |
| - .flatMap(textChannel::sendMessage); |
| 430 | + /** |
| 431 | + * Wrapper to hold data relevant to temporary actions, for example the time it expires. |
| 432 | + * |
| 433 | + * @param expiresAt the time the temporary action expires |
| 434 | + * @param duration a human-readable text representing the duration of the temporary action, such |
| 435 | + * as {@code "1 day"}. |
| 436 | + */ |
| 437 | + record TemporaryData(Instant expiresAt, String duration) { |
452 | 438 | }
|
453 | 439 | }
|
0 commit comments