Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
d8a08fb
Adding the /modmail command
DevSerendipity Aug 13, 2022
36cbbc9
Removing comments , plus signs and commented out code
DevSerendipity Aug 13, 2022
e8c68d2
Adding modmail channel to config and reflecting those changes in command
DevSerendipity Aug 14, 2022
c132467
Removing comments, unnecessary lines and making methods
DevSerendipity Aug 15, 2022
e2b4fd0
Improving the naming, removing require not null and additional ...
DevSerendipity Aug 20, 2022
238bea4
Adding the time remaining since command called and bunch ...
DevSerendipity Aug 20, 2022
1f28832
Removing print statement also adding an else if the value is not present
DevSerendipity Aug 20, 2022
9e1027f
Changing from Math.toExactInt to a int cast instead for sonar
DevSerendipity Aug 20, 2022
d8b0012
Different math operant sonar , attempt #2
DevSerendipity Aug 20, 2022
6cbdcbb
Removing the guild id and adding guildNamePredicate instead
DevSerendipity Aug 22, 2022
69279a2
Making the class final
DevSerendipity Sep 16, 2022
4d097dc
Removing @NotNull
DevSerendipity Sep 16, 2022
0446811
Removing BlackListedListener
DevSerendipity Sep 18, 2022
4da1ef3
Adding a modmail command message to all channels which handles DM's
DevSerendipity Sep 28, 2022
1b55f9e
Adding the changes mentioned in the PR
DevSerendipity Sep 30, 2022
536890c
Adding the choice for the user guild
DevSerendipity Sep 30, 2022
d6bbeeb
Adding param into the comment
DevSerendipity Sep 30, 2022
fefc27e
Adding the improvements from the code review
DevSerendipity Oct 3, 2022
68723e0
Removing unnecessary imports
DevSerendipity Oct 3, 2022
433621f
Resolving all conflicts and making improvements
DevSerendipity Oct 5, 2022
5d3a3b4
Making more changes
DevSerendipity Oct 6, 2022
3edf7ef
Removing unused imports
DevSerendipity Oct 6, 2022
8dcc750
Adding a package.info
DevSerendipity Oct 6, 2022
6cf7926
Improving the message package info
DevSerendipity Oct 6, 2022
8f17dad
Adding to the config template, adding the little UX improvements
DevSerendipity Oct 8, 2022
ccc2641
Merging two queues into one
DevSerendipity Oct 8, 2022
73f09a3
Removing description
DevSerendipity Oct 8, 2022
75c61de
Improving the things mentioned
DevSerendipity Oct 11, 2022
5711010
Making an ModerationUtil for Dm advices after ModerationAction
DevSerendipity Oct 12, 2022
dc616bc
Removing unnecessary imports
DevSerendipity Oct 12, 2022
ce287df
Improving the logger warning
DevSerendipity Oct 12, 2022
93d91f0
Removing unnecessary import, ran spotless
DevSerendipity Oct 12, 2022
8b5394d
Manually fixing spotless errors
DevSerendipity Oct 12, 2022
9f3b0f6
Quick before zabu changes his mind
DevSerendipity Oct 13, 2022
c98d66e
Push quick
DevSerendipity Oct 13, 2022
9445c30
Fixing the Global Command Problem
DevSerendipity Oct 18, 2022
e71b326
please
DevSerendipity Oct 18, 2022
9dff436
UNUSED METHOD
DevSerendipity Oct 18, 2022
c1a7140
little by little I am over
DevSerendipity Oct 19, 2022
0d1a2e3
Closer and closer
DevSerendipity Oct 19, 2022
2b94ea1
Improved the Return type
DevSerendipity Oct 19, 2022
c193b56
Finish it
DevSerendipity Oct 19, 2022
0e46dcd
Fixed a few bugs here and there
Zabuzard Oct 19, 2022
8da2eca
Shortened name in javadoc
Zabuzard Oct 19, 2022
9403005
Added missing command mentions
Zabuzard Oct 20, 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
1 change: 1 addition & 0 deletions application/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"projectWebsite": "https://github.com/Together-Java/TJ-Bot",
"discordGuildInvite": "https://discord.com/invite/XXFUXzK",
"modAuditLogChannelPattern": "mod_audit_log",
"modMailChannelPattern": "modmail",
"mutedRolePattern": "Muted",
"heavyModerationRolePattern": "Moderator",
"softModerationRolePattern": "Moderator|Staff Assistant",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.togetherjava.tjbot.commands.mediaonly.MediaOnlyChannelListener;
import org.togetherjava.tjbot.commands.moderation.*;
import org.togetherjava.tjbot.commands.moderation.attachment.BlacklistedAttachmentListener;
import org.togetherjava.tjbot.commands.moderation.modmail.ModMailCommand;
import org.togetherjava.tjbot.commands.moderation.scam.ScamBlocker;
import org.togetherjava.tjbot.commands.moderation.scam.ScamHistoryPurgeRoutine;
import org.togetherjava.tjbot.commands.moderation.scam.ScamHistoryStore;
Expand Down Expand Up @@ -126,6 +127,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new WhoIsCommand());
features.add(new WolframAlphaCommand(config));
features.add(new AskCommand(config, helpSystemHelper));
features.add(new ModMailCommand(jda, config));
features.add(new HelpThreadCommand(config, helpSystemHelper));

// Mixtures
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public HelpSystemHelper(JDA jda, Config config, Database database) {

RestAction<Message> sendExplanationMessage(GuildMessageChannel threadChannel) {
return MessageUtils
.mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME,
.mentionGuildSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME,
HelpThreadCommand.Subcommand.CLOSE.getCommandName())
.flatMap(closeCommandMention -> sendExplanationMessage(threadChannel,
closeCommandMention));
Expand Down Expand Up @@ -313,7 +313,7 @@ private void executeUncategorizedAdviceCheck(long threadChannelId, long authorId

// Still no category, send advice
return MessageUtils
.mentionSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME,
.mentionGuildSlashCommand(threadChannel.getGuild(), HelpThreadCommand.COMMAND_NAME,
HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP,
HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName())
.flatMap(command -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private boolean handleIsNotOnCooldown(Message message) {
String threadDescription = lastHelpThread == null ? "your previously created help thread"
: lastHelpThread.getAsMention();

MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME)
MessageUtils.mentionGuildSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME)
.flatMap(command -> message.getChannel()
.sendMessage("""
%s Please use %s to follow up on your question, \
Expand Down Expand Up @@ -185,7 +185,7 @@ private static RestAction<Message> sendInitialMessage(ThreadChannel threadChanne
.build();

return MessageUtils
.mentionSlashCommand(originalMessage.getGuild(), HelpThreadCommand.COMMAND_NAME,
.mentionGuildSlashCommand(originalMessage.getGuild(), HelpThreadCommand.COMMAND_NAME,
HelpThreadCommand.CHANGE_SUBCOMMAND_GROUP,
HelpThreadCommand.Subcommand.CHANGE_CATEGORY.getCommandName())
.flatMap(command -> {
Expand All @@ -204,7 +204,7 @@ private static RestAction<Message> sendInitialMessage(ThreadChannel threadChanne
}

private static RestAction<Message> notifyUser(IMentionable threadChannel, Message message) {
return MessageUtils.mentionSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME)
return MessageUtils.mentionGuildSlashCommand(message.getGuild(), AskCommand.COMMAND_NAME)
.flatMap(command -> message.getChannel()
.sendMessage(
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel;
import net.dv8tion.jda.api.interactions.callbacks.IReplyCallback;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.commands.moderation.modmail.ModMailCommand;
import org.togetherjava.tjbot.commands.utils.MessageUtils;
import org.togetherjava.tjbot.config.Config;

import javax.annotation.Nullable;
Expand All @@ -16,8 +20,11 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -45,6 +52,25 @@ private ModerationUtils() {
*/
static final Color AMBIENT_COLOR = Color.decode("#895FE8");

/**
* Actions with timely constraint, like being muted for 1 hour.
*/
private static final Set<ModerationAction> TEMPORARY_ACTIONS =
EnumSet.of(ModerationAction.MUTE);
/**
* Actions with revoking previously made actions on the user, like unmuting the user after it
* has been muted.
*/
private static final Set<ModerationAction> REVOKE_ACTIONS =
EnumSet.of(ModerationAction.UNMUTE, ModerationAction.UNQUARANTINE);
/**
* Soft violations were the user still remains member of the guild, such as a warning
*/
private static final Set<ModerationAction> SOFT_ACTIONS =
EnumSet.of(ModerationAction.WARN, ModerationAction.QUARANTINE);



/**
* Checks whether the given reason is valid. If not, it will handle the situation and respond to
* the user.
Expand Down Expand Up @@ -372,4 +398,56 @@ static Optional<TemporaryData> computeTemporaryData(String durationText) {
*/
record TemporaryData(Instant expiresAt, String duration) {
}

/**
* Gives out advice depending on the {@link ModerationAction} and the parameters passed into it.
*
* @param action the action that is being performed, such as banning a user.
* @param temporaryData if the action is a temporary action, such as a 1 hour mute.
* @param additionalDescription any extra description that should be part of the message, if
* desired
* @param guild for which the action was triggered.
* @param reason for the action.
* @param textChannel for which messages are being sent to.
*
* @return the appropriate advice.
*/
public static RestAction<Message> sendDmAdvice(ModerationAction action,
@Nullable TemporaryData temporaryData, @Nullable String additionalDescription,
Guild guild, String reason, PrivateChannel textChannel) {
String additionalDescriptionInfix =
additionalDescription == null ? "" : "\n" + additionalDescription;

if (REVOKE_ACTIONS.contains(action)) {
return textChannel.sendMessage("""
Hey there, you have been %s in the server %s.%s
The reason for being %s is: %s
""".formatted(action.getVerb(), guild.getName(), additionalDescriptionInfix,
action.getVerb(), reason));
}
String durationMessage;
if (SOFT_ACTIONS.contains(action)) {
durationMessage = "";
} else if (TEMPORARY_ACTIONS.contains(action)) {
durationMessage =
temporaryData == null ? " permanently" : " for " + temporaryData.duration();
} else {
throw new IllegalArgumentException(
"Action '%s' is not supported by this method".formatted(action));
}

UnaryOperator<String> createDmMessage =
commandMention -> """
Hey there, sorry to tell you but unfortunately you have been %s%s in the server %s.%s
To get in touch with a moderator, you can simply use the %s command here in this chat. \
Your message will then be forwarded and a moderator will get back to you soon 😊
The reason for being %s is: %s
"""
.formatted(action.getVerb(), durationMessage, guild.getName(),
additionalDescriptionInfix, commandMention, action.getVerb(), reason);

return MessageUtils.mentionGlobalSlashCommand(guild.getJDA(), ModMailCommand.COMMAND_NAME)
.map(createDmMessage)
.flatMap(textChannel::sendMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,11 @@ private static void handleAlreadyMutedTarget(IReplyCallback event) {
private static RestAction<Boolean> sendDm(ISnowflake target,
@Nullable ModerationUtils.TemporaryData temporaryData, String reason, Guild guild,
GenericEvent event) {
String durationMessage =
temporaryData == null ? "permanently" : "for " + temporaryData.duration();
String dmMessage =
"""
Hey there, sorry to tell you but unfortunately you have been muted %s in the server %s.
This means you can no longer send any messages in the server until you have been unmuted again.
If you think this was a mistake, please contact a moderator or admin of the server.
The reason for the mute is: %s
"""
.formatted(durationMessage, guild.getName(), reason);
return event.getJDA()
.openPrivateChannelById(target.getId())
.flatMap(channel -> channel.sendMessage(dmMessage))
.flatMap(channel -> ModerationUtils.sendDmAdvice(ModerationAction.MUTE, temporaryData,
"This means you can no longer send any messages in the server until you have been unmuted again.",
guild, reason, channel))
.mapToResult()
.map(Result::isSuccess);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,11 @@ private static void handleAlreadyQuarantinedTarget(IReplyCallback event) {

private static RestAction<Boolean> sendDm(ISnowflake target, String reason, Guild guild,
GenericEvent event) {
String dmMessage =
"""
Hey there, sorry to tell you but unfortunately you have been put under quarantine in the server %s.
This means you can no longer interact with anyone in the server until you have been unquarantined again.
If you think this was a mistake, or the reason no longer applies, please contact a moderator or admin of the server.
The reason for the quarantine is: %s
"""
.formatted(guild.getName(), reason);

return event.getJDA()
.openPrivateChannelById(target.getIdLong())
.flatMap(channel -> channel.sendMessage(dmMessage))
.flatMap(channel -> ModerationUtils.sendDmAdvice(ModerationAction.QUARANTINE, null,
"This means you can no longer interact with anyone in the server until you have been unquarantined again.",
guild, reason, channel))
.mapToResult()
.map(Result::isSuccess);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,11 @@ private static void handleNotMutedTarget(IReplyCallback event) {

private static RestAction<Boolean> sendDm(ISnowflake target, String reason, Guild guild,
GenericEvent event) {
String dmMessage = """
Hey there, you have been unmuted in the server %s.
This means you can now send messages in the server again.
The reason for the unmute is: %s
""".formatted(guild.getName(), reason);
return event.getJDA()
.openPrivateChannelById(target.getId())
.flatMap(channel -> channel.sendMessage(dmMessage))
.flatMap(channel -> ModerationUtils.sendDmAdvice(ModerationAction.UNMUTE, null,
"This means you can now send messages in the server again 👌", guild, reason,
channel))
.mapToResult()
.map(Result::isSuccess);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,11 @@ private static void handleNotQuarantinedTarget(IReplyCallback event) {

private static RestAction<Boolean> sendDm(ISnowflake target, String reason, Guild guild,
GenericEvent event) {
String dmMessage = """
Hey there, you have been put out of quarantine in the server %s.
This means you can now interact with others in the server again.
The reason for the unquarantine is: %s
""".formatted(guild.getName(), reason);

return event.getJDA()
.openPrivateChannelById(target.getIdLong())
.flatMap(channel -> channel.sendMessage(dmMessage))
.flatMap(channel -> ModerationUtils.sendDmAdvice(ModerationAction.UNQUARANTINE, null,
"This means you can now interact with others in the server again 👌", guild,
reason, channel))
.mapToResult()
.map(Result::isSuccess);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,8 @@ private static RestAction<Boolean> dmUser(ISnowflake target, String reason, Guil
SlashCommandInteractionEvent event) {
return event.getJDA()
.openPrivateChannelById(target.getId())
.flatMap(channel -> channel.sendMessage(
"""
Hey there, sorry to tell you but unfortunately you have been warned in the server %s.
If you think this was a mistake, please contact a moderator or admin of the server.
The reason for the warning is: %s
"""
.formatted(guild.getName(), reason)))
.flatMap(channel -> ModerationUtils.sendDmAdvice(ModerationAction.WARN, null, null,
guild, reason, channel))
.mapToResult()
.map(Result::isSuccess);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.channel.concrete.PrivateChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;

import org.togetherjava.tjbot.commands.MessageReceiverAdapter;
import org.togetherjava.tjbot.commands.moderation.modmail.ModMailCommand;
import org.togetherjava.tjbot.commands.utils.MessageUtils;
import org.togetherjava.tjbot.config.Config;
import org.togetherjava.tjbot.moderation.ModAuditLogWriter;

import java.awt.Color;
import java.util.List;
import java.util.Locale;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -53,28 +57,36 @@ private void handleBadMessage(Message message) {
private RestAction<Message> dmUser(Message message) {
return message.getAuthor()
.openPrivateChannel()
.flatMap(privateChannel -> privateChannel.sendMessage(createDmMessage(message)));
.flatMap(privateChannel -> sendDmMessage(message, privateChannel));
}

private MessageCreateData createDmMessage(Message originalMessage) {
private RestAction<Message> sendDmMessage(Message originalMessage, PrivateChannel channel) {
String contentRaw = originalMessage.getContentRaw();
String blacklistedAttachments =
String.join(", ", getBlacklistedAttachmentsFromMessage(originalMessage));

String dmMessageContent =
"""
UnaryOperator<String> createDmText =
commandMention -> """
Hey there, you posted a message containing a blacklisted file attachment: %s.
We had to delete your message for security reasons.

To get in touch with a moderator, you can simply use the %s command here in this chat. \
Your message will then be forwarded and a moderator will get back to you soon 👍
Feel free to repost your message without, or with a different file instead. Sorry for any inconvenience caused by this 🙇️
"""
.formatted(blacklistedAttachments);

// No embed needed if there was no message from the user
if (contentRaw.isEmpty()) {
return new MessageCreateBuilder().setContent(dmMessageContent).build();
}
return createBaseResponse(contentRaw, dmMessageContent);
.formatted(blacklistedAttachments, commandMention);

return MessageUtils
.mentionGlobalSlashCommand(originalMessage.getJDA(), ModMailCommand.COMMAND_NAME)
.map(createDmText)
.map(dmMessageContent -> {
// No embed needed if there was no message from the user
if (contentRaw.isEmpty()) {
return new MessageCreateBuilder().setContent(dmMessageContent).build();
}
return createBaseResponse(contentRaw, dmMessageContent);
})
.flatMap(channel::sendMessage);
}

private MessageCreateData createBaseResponse(String originalMessageContent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/**
* This packages offers all the functionality for checking attachment safety.
* This packages offers all the functionality for checking attachment safety see
* {@link org.togetherjava.tjbot.commands.moderation.attachment.BlacklistedAttachmentListener
* BlacklistedAttachmentListener} as main entry point.
*/
@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
Expand Down
Loading