Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
32bb538
Report Command
DevSerendipity Oct 24, 2022
ccc131f
making fields private
DevSerendipity Oct 24, 2022
de77ea1
removing Constant, naming convention since its not a constant
DevSerendipity Oct 24, 2022
3f9e434
Instance methods writing to static fields fix
DevSerendipity Oct 24, 2022
03492df
Adding comments for constructor
DevSerendipity Oct 24, 2022
1cfed5e
Little spelling improvements, comment improvements and capitalization
DevSerendipity Oct 24, 2022
4b136b7
Making improvements, requested
DevSerendipity Oct 27, 2022
f68175e
pushing before it breaks Part 2
DevSerendipity Oct 27, 2022
9f1773e
Pretty sure the last one Part 3
DevSerendipity Oct 27, 2022
0a04270
Little small improvements part 3
DevSerendipity Oct 27, 2022
09e285a
Making some improvements part 4
DevSerendipity Oct 30, 2022
b448b06
missed you
DevSerendipity Oct 30, 2022
e97ffa8
Part 6
DevSerendipity Oct 30, 2022
88f5484
Part 7
DevSerendipity Oct 31, 2022
9138474
cmon finish thissss
DevSerendipity Nov 2, 2022
ac38e55
Spooky month might be over, but you still have to review my code
DevSerendipity Nov 4, 2022
680fd47
boo
DevSerendipity Nov 4, 2022
bcf4ad4
I am ready
DevSerendipity Nov 6, 2022
28bb2b6
improved the positioning of methods and naming of variables
DevSerendipity Nov 8, 2022
dfeb435
The deed is done.
DevSerendipity Nov 8, 2022
8ec64f8
Empty lines are erased.
DevSerendipity Nov 8, 2022
cc583ee
Additional adjustments.
DevSerendipity Nov 8, 2022
4368fc0
Slight changes
DevSerendipity Nov 8, 2022
286e544
the id capitalization
DevSerendipity Nov 8, 2022
8cf465a
Done for good
DevSerendipity Nov 9, 2022
269ccb2
yes
DevSerendipity Nov 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.togetherjava.tjbot.commands.mathcommands.wolframalpha.WolframAlphaCommand;
import org.togetherjava.tjbot.commands.mediaonly.MediaOnlyChannelListener;
import org.togetherjava.tjbot.commands.moderation.*;
import org.togetherjava.tjbot.commands.moderation.ReportCommand;
import org.togetherjava.tjbot.commands.moderation.attachment.BlacklistedAttachmentListener;
import org.togetherjava.tjbot.commands.moderation.modmail.ModMailCommand;
import org.togetherjava.tjbot.commands.moderation.scam.ScamBlocker;
Expand Down Expand Up @@ -132,6 +133,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new AskCommand(config, helpSystemHelper, database));
features.add(new ModMailCommand(jda, config));
features.add(new HelpThreadCommand(config, helpSystemHelper));
features.add(new ReportCommand(config));

// Mixtures
features.add(new HelpThreadOverviewUpdater(config, helpSystemHelper));
Expand Down
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")
.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;
}

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();
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();
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);
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
/**
* Implements the /modmail command, which allows users to contact a moderator within the server
* which forwards messages to moderators in a dedicated channel given by
* {@link Config#getModAuditLogChannelPattern()}.
* {@link Config#getModMailChannelPattern()}.
*/

public final class ModMailCommand extends SlashCommandAdapter {
Expand Down