-
-
Notifications
You must be signed in to change notification settings - Fork 102
Report Command #653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Report Command #653
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
32bb538
Report Command
DevSerendipity ccc131f
making fields private
DevSerendipity de77ea1
removing Constant, naming convention since its not a constant
DevSerendipity 3f9e434
Instance methods writing to static fields fix
DevSerendipity 03492df
Adding comments for constructor
DevSerendipity 1cfed5e
Little spelling improvements, comment improvements and capitalization
DevSerendipity 4b136b7
Making improvements, requested
DevSerendipity f68175e
pushing before it breaks Part 2
DevSerendipity 9f1773e
Pretty sure the last one Part 3
DevSerendipity 0a04270
Little small improvements part 3
DevSerendipity 09e285a
Making some improvements part 4
DevSerendipity b448b06
missed you
DevSerendipity e97ffa8
Part 6
DevSerendipity 88f5484
Part 7
DevSerendipity 9138474
cmon finish thissss
DevSerendipity ac38e55
Spooky month might be over, but you still have to review my code
DevSerendipity 680fd47
boo
DevSerendipity bcf4ad4
I am ready
DevSerendipity 28bb2b6
improved the positioning of methods and naming of variables
DevSerendipity dfeb435
The deed is done.
DevSerendipity 8ec64f8
Empty lines are erased.
DevSerendipity cc583ee
Additional adjustments.
DevSerendipity 4368fc0
Slight changes
DevSerendipity 286e544
the id capitalization
DevSerendipity 8cf465a
Done for good
DevSerendipity 269ccb2
yes
DevSerendipity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
application/src/main/java/org/togetherjava/tjbot/commands/moderation/ReportCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
package org.togetherjava.tjbot.commands.moderation; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.Guild; | ||
import net.dv8tion.jda.api.entities.Message; | ||
import net.dv8tion.jda.api.entities.MessageEmbed; | ||
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; | ||
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent; | ||
import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent; | ||
import net.dv8tion.jda.api.interactions.InteractionHook; | ||
import net.dv8tion.jda.api.interactions.commands.build.Commands; | ||
import net.dv8tion.jda.api.interactions.components.Modal; | ||
import net.dv8tion.jda.api.interactions.components.text.TextInput; | ||
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle; | ||
import net.dv8tion.jda.api.requests.restaction.MessageCreateAction; | ||
import net.dv8tion.jda.api.utils.Result; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.togetherjava.tjbot.commands.BotCommandAdapter; | ||
import org.togetherjava.tjbot.commands.CommandVisibility; | ||
import org.togetherjava.tjbot.commands.MessageContextCommand; | ||
import org.togetherjava.tjbot.commands.utils.DiscordClientAction; | ||
import org.togetherjava.tjbot.commands.utils.MessageUtils; | ||
import org.togetherjava.tjbot.config.Config; | ||
|
||
import java.awt.*; | ||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
/** | ||
* Implements the /report command, which allows users to report a selected offensive message from | ||
* another user. The message is then forwarded to moderators in a dedicated channel given by | ||
* {@link Config#getModMailChannelPattern()}. | ||
*/ | ||
public final class ReportCommand extends BotCommandAdapter implements MessageContextCommand { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ReportCommand.class); | ||
private static final String COMMAND_NAME = "report"; | ||
private static final String REPORT_REASON_INPUT_ID = "reportReason"; | ||
private static final int COOLDOWN_DURATION_VALUE = 3; | ||
private static final ChronoUnit COOLDOWN_DURATION_UNIT = ChronoUnit.MINUTES; | ||
private static final Color AMBIENT_COLOR = Color.BLACK; | ||
private final Cache<Long, Instant> authorToLastReportInvocation = createCooldownCache(); | ||
private final Predicate<String> modMailChannelNamePredicate; | ||
private final String configModMailChannelPattern; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param config to get the channel to forward reports to | ||
*/ | ||
public ReportCommand(Config config) { | ||
super(Commands.message(COMMAND_NAME), CommandVisibility.GUILD); | ||
|
||
modMailChannelNamePredicate = | ||
Pattern.compile(config.getModMailChannelPattern()).asMatchPredicate(); | ||
|
||
configModMailChannelPattern = config.getModMailChannelPattern(); | ||
} | ||
|
||
private Cache<Long, Instant> createCooldownCache() { | ||
return Caffeine.newBuilder() | ||
.maximumSize(1_000) | ||
.expireAfterAccess(COOLDOWN_DURATION_VALUE, TimeUnit.of(COOLDOWN_DURATION_UNIT)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void onMessageContext(MessageContextInteractionEvent event) { | ||
long userID = event.getUser().getIdLong(); | ||
String reportedMessageTimestamp = event.getTarget().getTimeCreated().toInstant().toString(); | ||
|
||
if (handleIsOnCooldown(event)) { | ||
return; | ||
} | ||
authorToLastReportInvocation.put(userID, Instant.now()); | ||
|
||
String reportedMessage = event.getTarget().getContentRaw(); | ||
String reportedMessageID = event.getTarget().getId(); | ||
String reportedMessageChannel = event.getTarget().getChannel().getId(); | ||
String reportedAuthorName = event.getTarget().getAuthor().getName(); | ||
String reportedAuthorAvatarURL = event.getTarget().getAuthor().getAvatarUrl(); | ||
String reportedAuthorID = event.getTarget().getAuthor().getId(); | ||
|
||
TextInput modalTextInput = TextInput | ||
.create(REPORT_REASON_INPUT_ID, "Anonymous report to the moderators", | ||
TextInputStyle.PARAGRAPH) | ||
.setPlaceholder("Why do you want to report this message?") | ||
.setRequiredRange(3, 200) | ||
.build(); | ||
|
||
String reportModalComponentID = generateComponentId(reportedMessage, reportedMessageID, | ||
reportedMessageChannel, reportedMessageTimestamp, reportedAuthorName, | ||
reportedAuthorAvatarURL, reportedAuthorID); | ||
Modal reportModal = Modal.create(reportModalComponentID, "Report this to a moderator") | ||
Taz03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.addActionRow(modalTextInput) | ||
.build(); | ||
|
||
event.replyModal(reportModal).queue(); | ||
} | ||
|
||
private boolean handleIsOnCooldown(MessageContextInteractionEvent event) { | ||
if (!isAuthorOnCooldown(event.getUser().getIdLong())) { | ||
return false; | ||
} | ||
event | ||
.reply("You can only report a message once per %s minutes." | ||
.formatted(COOLDOWN_DURATION_VALUE)) | ||
.setEphemeral(true) | ||
.queue(); | ||
return true; | ||
} | ||
|
||
private boolean isAuthorOnCooldown(long userId) { | ||
return Optional.ofNullable(authorToLastReportInvocation.getIfPresent(userId)) | ||
.map(sinceCommandInvoked -> sinceCommandInvoked.plus(COOLDOWN_DURATION_VALUE, | ||
COOLDOWN_DURATION_UNIT)) | ||
.filter(Instant.now()::isBefore) | ||
.isPresent(); | ||
} | ||
|
||
@Override | ||
public void onModalSubmitted(ModalInteractionEvent event, List<String> args) { | ||
Optional<TextChannel> modMailAuditLog = handleRequireModMailChannel(event); | ||
|
||
if (modMailAuditLog.isEmpty()) { | ||
return; | ||
} | ||
Nxllpointer marked this conversation as resolved.
Show resolved
Hide resolved
Taz03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
sendModMessage(event, args, modMailAuditLog.orElseThrow()); | ||
} | ||
|
||
private Optional<TextChannel> handleRequireModMailChannel(ModalInteractionEvent event) { | ||
long guildID = Objects | ||
.requireNonNull(event.getGuild(), | ||
"Guild is null for ModalInteractionEvent in ReportCommand.") | ||
.getIdLong(); | ||
Optional<TextChannel> modMailAuditLog = event.getJDA() | ||
.getTextChannelCache() | ||
.stream() | ||
.filter(channel -> modMailChannelNamePredicate.test(channel.getName())) | ||
.findAny(); | ||
if (modMailAuditLog.isEmpty()) { | ||
event.reply( | ||
"Sorry, there was an issue sending your report to the moderators. We are investigating.") | ||
.setEphemeral(true) | ||
.queue(); | ||
logger.warn( | ||
"Cannot find the designated modmail channel in server by id {} with the pattern {}", | ||
guildID, configModMailChannelPattern); | ||
} | ||
return modMailAuditLog; | ||
} | ||
|
||
private MessageCreateAction createModMessage(String reportReason, | ||
ReportedMessage reportedMessage, Guild guild, TextChannel modMailAuditLog) { | ||
|
||
MessageEmbed reportedMessageEmbed = new EmbedBuilder().setTitle("Report") | ||
.setDescription(MessageUtils.abbreviate(reportedMessage.content, | ||
MessageEmbed.DESCRIPTION_MAX_LENGTH)) | ||
.setAuthor(reportedMessage.authorName, null, reportedMessage.authorAvatarUrl) | ||
.setTimestamp(reportedMessage.timestamp) | ||
.setColor(AMBIENT_COLOR) | ||
.build(); | ||
|
||
MessageEmbed reportReasonEmbed = new EmbedBuilder().setTitle("Reason") | ||
.setDescription(reportReason) | ||
.setColor(AMBIENT_COLOR) | ||
.build(); | ||
Taz03 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return modMailAuditLog.sendMessageEmbeds(reportedMessageEmbed, reportReasonEmbed) | ||
.addActionRow(DiscordClientAction.Channels.GUILD_CHANNEL_MESSAGE.asLinkButton( | ||
"Go to Message", guild.getId(), reportedMessage.channelID, reportedMessage.id)); | ||
} | ||
|
||
private void sendModMessage(ModalInteractionEvent event, List<String> args, | ||
TextChannel modMailAuditLog) { | ||
Guild guild = event.getGuild(); | ||
event.deferReply().setEphemeral(true).queue(); | ||
|
||
InteractionHook hook = event.getHook(); | ||
Zabuzard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
String reportReason = event.getValue(REPORT_REASON_INPUT_ID).getAsString(); | ||
|
||
ReportedMessage reportedMessage = ReportedMessage.ofArgs(args); | ||
|
||
createModMessage(reportReason, reportedMessage, guild, modMailAuditLog).mapToResult() | ||
.map(this::createUserReply) | ||
.flatMap(hook::editOriginal) | ||
.queue(); | ||
} | ||
|
||
private String createUserReply(Result<Message> result) { | ||
if (result.isFailure()) { | ||
logger.warn("Unable to forward a message report to modmail channel.", | ||
result.getFailure()); | ||
return "Sorry, there was an issue sending your report to the moderators. We are investigating."; | ||
} | ||
return "Thank you for reporting this message. A moderator will take care of the matter as soon as possible."; | ||
} | ||
|
||
private record ReportedMessage(String content, String id, String channelID, Instant timestamp, | ||
String authorName, String authorAvatarUrl) { | ||
static ReportedMessage ofArgs(List<String> args) { | ||
String content = args.get(0); | ||
String id = args.get(1); | ||
String channelID = args.get(2); | ||
Instant timestamp = Instant.parse(args.get(3)); | ||
String authorName = args.get(4); | ||
String authorAvatarUrl = args.get(5); | ||
return new ReportedMessage(content, id, channelID, timestamp, authorName, | ||
authorAvatarUrl); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.