diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index e6326df1d5..fb4b93baae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -2,13 +2,13 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.requests.GatewayIntent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.togetherjava.tjbot.commands.Commands; import org.togetherjava.tjbot.commands.system.CommandSystem; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; -import org.togetherjava.tjbot.routines.ModAuditLogRoutine; import javax.security.auth.login.LoginException; import java.io.IOException; @@ -77,15 +77,12 @@ public static void runBot(String token, Path databasePath) { Database database = new Database("jdbc:sqlite:" + databasePath.toAbsolutePath()); JDA jda = JDABuilder.createDefault(token) - .addEventListeners(new CommandSystem(database)) + .enableIntents(GatewayIntent.GUILD_MEMBERS) .build(); + jda.addEventListener(new CommandSystem(jda, database)); jda.awaitReady(); logger.info("Bot is ready"); - // TODO This should be moved into some proper command system instead (see GH issue #235 - // which adds support for routines) - new ModAuditLogRoutine(jda, database).start(); - Runtime.getRuntime().addShutdownHook(new Thread(Application::onShutdown)); } catch (LoginException e) { logger.error("Failed to login", e); diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java index 7980e34733..c748f68bf0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/Commands.java @@ -1,5 +1,6 @@ package org.togetherjava.tjbot.commands; +import net.dv8tion.jda.api.JDA; import org.jetbrains.annotations.NotNull; import org.togetherjava.tjbot.commands.basic.DatabaseCommand; import org.togetherjava.tjbot.commands.basic.PingCommand; @@ -12,6 +13,7 @@ import org.togetherjava.tjbot.commands.tags.TagSystem; import org.togetherjava.tjbot.commands.tags.TagsCommand; import org.togetherjava.tjbot.db.Database; +import org.togetherjava.tjbot.routines.ModAuditLogRoutine; import java.util.ArrayList; import java.util.Collection; @@ -22,7 +24,7 @@ * pick it up from and register it with the system. *

* To add a new slash command, extend the commands returned by - * {@link #createSlashCommands(Database)}. + * {@link #createSlashCommands(JDA, Database)}. */ public enum Commands { ; @@ -33,13 +35,23 @@ public enum Commands { * Calling this method multiple times will result in multiple commands being created, which * generally should be avoided. * + * @param jda the JDA instance commands will be registered at * @param database the database of the application, which commands can use to persist data * @return a collection of all slash commands */ - public static @NotNull Collection createSlashCommands( + public static @NotNull Collection createSlashCommands(@NotNull JDA jda, @NotNull Database database) { TagSystem tagSystem = new TagSystem(database); ModerationActionsStore actionsStore = new ModerationActionsStore(database); + + // TODO This should be moved into some proper command system instead (see GH issue #235 + // which adds support for routines) + new ModAuditLogRoutine(jda, database).start(); + + // TODO This should be moved into some proper command system instead (see GH issue #236 + // which adds support for listeners) + jda.addEventListener(new RejoinMuteListener(actionsStore)); + // NOTE The command system can add special system relevant commands also by itself, // hence this list may not necessarily represent the full list of all commands actually // available. @@ -57,6 +69,8 @@ public enum Commands { commands.add(new UnbanCommand(actionsStore)); commands.add(new FreeCommand()); commands.add(new AuditCommand(actionsStore)); + commands.add(new MuteCommand(actionsStore)); + commands.add(new UnmuteCommand(actionsStore)); return commands; } diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java index deaa4a6c95..339541f8e0 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/ModerationUtils.java @@ -8,15 +8,18 @@ import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.config.Config; import java.awt.*; import java.time.Instant; +import java.util.Optional; import java.util.function.Predicate; +import java.util.regex.Pattern; /** * Utility class offering helpers revolving around user moderation, such as banning or kicking. */ -enum ModerationUtils { +public enum ModerationUtils { ; private static final Logger logger = LoggerFactory.getLogger(ModerationUtils.class); @@ -26,6 +29,8 @@ enum ModerationUtils { */ private static final int REASON_MAX_LENGTH = 512; static final Color AMBIENT_COLOR = Color.decode("#895FE8"); + public static final Predicate isMuteRole = + Pattern.compile(Config.getInstance().getMutedRolePattern()).asMatchPredicate(); /** * Checks whether the given reason is valid. If not, it will handle the situation and respond to @@ -85,6 +90,41 @@ static boolean handleCanInteractWithTarget(@NotNull String actionVerb, @NotNull return true; } + /** + * Checks whether the given author and bot can interact with the given role. For example whether + * they have enough permissions to add or remove this role to users. + *

+ * If not, it will handle the situation and respond to the user. + * + * @param bot the bot attempting to interact with the user + * @param author the author triggering the command + * @param role the role to interact with + * @param event the event used to respond to the user + * @return Whether the author and bot can interact with the role + */ + @SuppressWarnings("BooleanMethodNameMustStartWithQuestion") + static boolean handleCanInteractWithRole(@NotNull Member bot, @NotNull Member author, + @NotNull Role role, @NotNull Interaction event) { + if (!author.canInteract(role)) { + event + .reply("The role %s is too powerful for you to interact with." + .formatted(role.getAsMention())) + .setEphemeral(true) + .queue(); + return false; + } + + if (!bot.canInteract(role)) { + event + .reply("The role %s is too powerful for me to interact with." + .formatted(role.getAsMention())) + .setEphemeral(true) + .queue(); + return false; + } + return true; + } + /** * Checks whether the given bot has enough permission to execute the given action. For example * whether it has enough permissions to ban users. @@ -115,6 +155,81 @@ static boolean handleHasBotPermissions(@NotNull String actionVerb, return true; } + private static void handleAbsentTarget(@NotNull String actionVerb, @NotNull Interaction event) { + event + .reply("I can not %s the given user since they are not part of the guild anymore." + .formatted(actionVerb)) + .setEphemeral(true) + .queue(); + } + + /** + * Checks whether the given bot and author have enough permission to change the roles of a given + * target. For example whether they have enough permissions to add a role to a user. + *

+ * If not, it will handle the situation and respond to the user. + *

+ * The checks include: + *

+ * + * @param role the role to change, or {@code null} if it does not exist on the guild + * @param actionVerb the interaction as verb, for example {@code "mute"} or {@code "unmute"} + * @param target the target user to change roles from, or {@code null} if the user is not member + * of the guild + * @param bot the bot executing this interaction + * @param author the author attempting to interact with the target + * @param guild the guild this interaction is executed on + * @param hasRequiredRole a predicate used to identify required roles by their name + * @param reason the reason for this interaction + * @param event the event used to respond to the user + * @return Whether the bot and the author have enough permission + */ + @SuppressWarnings({"MethodWithTooManyParameters", "BooleanMethodNameMustStartWithQuestion", + "squid:S107"}) + static boolean handleRoleChangeChecks(@Nullable Role role, @NotNull String actionVerb, + @Nullable Member target, @NotNull Member bot, @NotNull Member author, + @NotNull Guild guild, @NotNull Predicate hasRequiredRole, + @NotNull CharSequence reason, @NotNull Interaction event) { + if (role == null) { + event + .reply("Can not %s the user, unable to find the corresponding role on this server" + .formatted(actionVerb)) + .setEphemeral(true) + .queue(); + logger.warn("The guild '{}' does not have a role to {} users.", guild.getName(), + actionVerb); + return false; + } + + // Member doesn't exist if attempting to change roles of a user who is not part of the guild + // anymore. + if (target == null) { + handleAbsentTarget(actionVerb, event); + return false; + } + if (!handleCanInteractWithTarget(actionVerb, bot, author, target, event)) { + return false; + } + if (!handleCanInteractWithRole(bot, author, role, event)) { + return false; + } + if (!handleHasAuthorRole(actionVerb, hasRequiredRole, author, event)) { + return false; + } + if (!handleHasBotPermissions(actionVerb, Permission.MANAGE_ROLES, bot, guild, event)) { + return false; + } + return ModerationUtils.handleReason(reason, event); + } + /** * Checks whether the given author has enough permission to execute the given action. For * example whether they have enough permissions to ban users. @@ -171,7 +286,7 @@ static boolean handleHasAuthorRole(@NotNull String actionVerb, /** * Creates a message to be displayed as response to a moderation action. - * + *

* Essentially, it informs others about the action, such as "John banned Bob for playing with * the fire.". * @@ -200,6 +315,16 @@ static boolean handleHasAuthorRole(@NotNull String actionVerb, .build(); } + /** + * Gets the role used to mute a member in a guild. + * + * @param guild the guild to get the muted role from + * @return the muted role, if found + */ + public static @NotNull Optional getMutedRole(@NotNull Guild guild) { + return guild.getRoles().stream().filter(role -> isMuteRole.test(role.getName())).findAny(); + } + /** * All available moderation actions. */ @@ -215,7 +340,15 @@ enum Action { /** * When a user kicks another user. */ - KICK("kicked"); + KICK("kicked"), + /** + * When a user mutes another user. + */ + MUTE("muted"), + /** + * When a user unmutes another user. + */ + UNMUTE("unmuted"); private final String verb; diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java new file mode 100644 index 0000000000..cb363190fc --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/MuteCommand.java @@ -0,0 +1,147 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.GenericEvent; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.Interaction; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.requests.RestAction; +import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; +import net.dv8tion.jda.api.utils.Result; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.config.Config; + +import java.util.Objects; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +/** + * This command can mute users. Muting can also be paired with a reason. The command will also try + * to DM the user to inform them about the action and the reason. + *

+ * The command fails if the user triggering it is lacking permissions to either mute other users or + * to mute the specific given user (for example a moderator attempting to mute an admin). + */ +public final class MuteCommand extends SlashCommandAdapter { + private static final Logger logger = LoggerFactory.getLogger(MuteCommand.class); + private static final String TARGET_OPTION = "user"; + private static final String REASON_OPTION = "reason"; + private static final String COMMAND_NAME = "mute"; + private static final String ACTION_VERB = "mute"; + private final Predicate hasRequiredRole; + private final ModerationActionsStore actionsStore; + + /** + * Constructs an instance. + * + * @param actionsStore used to store actions issued by this command + */ + public MuteCommand(@NotNull ModerationActionsStore actionsStore) { + super(COMMAND_NAME, "Mutes the given user so that they can not send messages anymore", + SlashCommandVisibility.GUILD); + + getData().addOption(OptionType.USER, TARGET_OPTION, "The user who you want to mute", true) + .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be muted", true); + + hasRequiredRole = Pattern.compile(Config.getInstance().getSoftModerationRolePattern()) + .asMatchPredicate(); + this.actionsStore = Objects.requireNonNull(actionsStore); + } + + private static void handleAlreadyMutedTarget(@NotNull Interaction event) { + event.reply("The user is already muted.").setEphemeral(true).queue(); + } + + private static RestAction sendDm(@NotNull ISnowflake target, @NotNull String reason, + @NotNull Guild guild, @NotNull GenericEvent event) { + String dmMessage = + """ + Hey there, sorry to tell you but unfortunately you have been muted 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(guild.getName(), reason); + return event.getJDA() + .openPrivateChannelById(target.getId()) + .flatMap(channel -> channel.sendMessage(dmMessage)) + .mapToResult() + .map(Result::isSuccess); + } + + private static @NotNull MessageEmbed sendFeedback(boolean hasSentDm, @NotNull Member target, + @NotNull Member author, @NotNull String reason) { + String dmNoticeText = ""; + if (!hasSentDm) { + dmNoticeText = "(Unable to send them a DM.)"; + } + return ModerationUtils.createActionResponse(author.getUser(), ModerationUtils.Action.MUTE, + target.getUser(), dmNoticeText, reason); + } + + private AuditableRestAction muteUser(@NotNull Member target, @NotNull Member author, + @NotNull String reason, @NotNull Guild guild) { + logger.info("'{}' ({}) muted the user '{}' ({}) in guild '{}' for reason '{}'.", + author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(), + target.getId(), guild.getName(), reason); + + actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(), + ModerationUtils.Action.MUTE, null, reason); + + return guild.addRoleToMember(target, ModerationUtils.getMutedRole(guild).orElseThrow()) + .reason(reason); + } + + private void muteUserFlow(@NotNull Member target, @NotNull Member author, + @NotNull String reason, @NotNull Guild guild, @NotNull SlashCommandEvent event) { + sendDm(target, reason, guild, event) + .flatMap(hasSentDm -> muteUser(target, author, reason, guild) + .map(banResult -> hasSentDm)) + .map(hasSentDm -> sendFeedback(hasSentDm, target, author, reason)) + .flatMap(event::replyEmbeds) + .queue(); + } + + @SuppressWarnings({"BooleanMethodNameMustStartWithQuestion", "MethodWithTooManyParameters"}) + private boolean handleChecks(@NotNull Member bot, @NotNull Member author, + @Nullable Member target, @NotNull CharSequence reason, @NotNull Guild guild, + @NotNull Interaction event) { + if (!ModerationUtils.handleRoleChangeChecks( + ModerationUtils.getMutedRole(guild).orElse(null), ACTION_VERB, target, bot, author, + guild, hasRequiredRole, reason, event)) { + return false; + } + if (Objects.requireNonNull(target) + .getRoles() + .stream() + .map(Role::getName) + .anyMatch(ModerationUtils.isMuteRole)) { + handleAlreadyMutedTarget(event); + return false; + } + return true; + } + + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + Member target = Objects.requireNonNull(event.getOption(TARGET_OPTION), "The target is null") + .getAsMember(); + Member author = Objects.requireNonNull(event.getMember(), "The author is null"); + String reason = Objects.requireNonNull(event.getOption(REASON_OPTION), "The reason is null") + .getAsString(); + + Guild guild = Objects.requireNonNull(event.getGuild()); + Member bot = guild.getSelfMember(); + + if (!handleChecks(bot, author, target, reason, guild, event)) { + return; + } + + muteUserFlow(Objects.requireNonNull(target), author, reason, guild, event); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/RejoinMuteListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/RejoinMuteListener.java new file mode 100644 index 0000000000..0278ce1c7c --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/RejoinMuteListener.java @@ -0,0 +1,81 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.IPermissionHolder; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nonnull; +import java.util.*; + +/** + * Reapplies existing mutes to users who have left and rejoined a guild. + *

+ * Mutes are realized with roles and roles are removed upon leaving a guild, making it possible for + * users to otherwise bypass a mute by simply leaving and rejoining a guild. This class listens for + * join events and reapplies the mute role in case the user is supposed to be muted still (according + * to the {@link ModerationActionsStore}). + */ +public final class RejoinMuteListener extends ListenerAdapter { + private static final Logger logger = LoggerFactory.getLogger(RejoinMuteListener.class); + + private final ModerationActionsStore actionsStore; + + /** + * Constructs an instance. + * + * @param actionsStore used to store actions issued by this command and to retrieve whether a + * user should be muted + */ + public RejoinMuteListener(@NotNull ModerationActionsStore actionsStore) { + this.actionsStore = Objects.requireNonNull(actionsStore); + } + + private static void muteMember(@NotNull Member member) { + Guild guild = member.getGuild(); + logger.info("Reapplied existing mute to user '{}' ({}) in guild '{}' after rejoining.", + member.getUser().getAsTag(), member.getId(), guild.getName()); + + guild.addRoleToMember(member, ModerationUtils.getMutedRole(guild).orElseThrow()) + .reason("Reapplied existing mute after rejoining the server") + .queue(); + } + + @Override + public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) { + Member member = event.getMember(); + if (!shouldMemberBeMuted(member)) { + return; + } + muteMember(member); + } + + private boolean shouldMemberBeMuted(@NotNull IPermissionHolder member) { + List actions = new ArrayList<>(actionsStore + .getActionsByTargetAscending(member.getGuild().getIdLong(), member.getIdLong())); + Collections.reverse(actions); + + Optional lastMute = actions.stream() + .filter(action -> action.actionType() == ModerationUtils.Action.MUTE) + .findFirst(); + if (lastMute.isEmpty()) { + // User was never muted + return false; + } + + Optional lastUnmute = actions.stream() + .filter(action -> action.actionType() == ModerationUtils.Action.UNMUTE) + .findFirst(); + if (lastUnmute.isEmpty()) { + // User was never unmuted + return true; + } + + // The last issued action takes priority + return lastMute.orElseThrow().issuedAt().isAfter(lastUnmute.orElseThrow().issuedAt()); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java new file mode 100644 index 0000000000..6828981c68 --- /dev/null +++ b/application/src/main/java/org/togetherjava/tjbot/commands/moderation/UnmuteCommand.java @@ -0,0 +1,144 @@ +package org.togetherjava.tjbot.commands.moderation; + +import net.dv8tion.jda.api.entities.*; +import net.dv8tion.jda.api.events.GenericEvent; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.Interaction; +import net.dv8tion.jda.api.interactions.commands.OptionType; +import net.dv8tion.jda.api.requests.RestAction; +import net.dv8tion.jda.api.requests.restaction.AuditableRestAction; +import net.dv8tion.jda.api.utils.Result; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.commands.SlashCommandAdapter; +import org.togetherjava.tjbot.commands.SlashCommandVisibility; +import org.togetherjava.tjbot.config.Config; + +import java.util.Objects; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +/** + * This command can unmute muted users. Unmuting can also be paired with a reason. The command will + * also try to DM the user to inform them about the action and the reason. + *

+ * The command fails if the user triggering it is lacking permissions to either unmute other users + * or to unmute the specific given user (for example a moderator attempting to unmute an admin). + */ +public final class UnmuteCommand extends SlashCommandAdapter { + private static final Logger logger = LoggerFactory.getLogger(UnmuteCommand.class); + private static final String TARGET_OPTION = "user"; + private static final String REASON_OPTION = "reason"; + private static final String COMMAND_NAME = "unmute"; + private static final String ACTION_VERB = "unmute"; + private final Predicate hasRequiredRole; + private final ModerationActionsStore actionsStore; + + /** + * Constructs an instance. + * + * @param actionsStore used to store actions issued by this command + */ + public UnmuteCommand(@NotNull ModerationActionsStore actionsStore) { + super(COMMAND_NAME, + "Unmutes the given already muted user so that they can send messages again", + SlashCommandVisibility.GUILD); + + getData().addOption(OptionType.USER, TARGET_OPTION, "The user who you want to unmute", true) + .addOption(OptionType.STRING, REASON_OPTION, "Why the user should be unmuted", true); + + hasRequiredRole = Pattern.compile(Config.getInstance().getSoftModerationRolePattern()) + .asMatchPredicate(); + this.actionsStore = Objects.requireNonNull(actionsStore); + } + + private static void handleNotMutedTarget(@NotNull Interaction event) { + event.reply("The user is not muted.").setEphemeral(true).queue(); + } + + private static RestAction sendDm(@NotNull ISnowflake target, @NotNull String reason, + @NotNull Guild guild, @NotNull 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)) + .mapToResult() + .map(Result::isSuccess); + } + + private static @NotNull MessageEmbed sendFeedback(boolean hasSentDm, @NotNull Member target, + @NotNull Member author, @NotNull String reason) { + String dmNoticeText = ""; + if (!hasSentDm) { + dmNoticeText = "(Unable to send them a DM.)"; + } + return ModerationUtils.createActionResponse(author.getUser(), ModerationUtils.Action.UNMUTE, + target.getUser(), dmNoticeText, reason); + } + + private AuditableRestAction unmuteUser(@NotNull Member target, @NotNull Member author, + @NotNull String reason, @NotNull Guild guild) { + logger.info("'{}' ({}) unmuted the user '{}' ({}) in guild '{}' for reason '{}'.", + author.getUser().getAsTag(), author.getId(), target.getUser().getAsTag(), + target.getId(), guild.getName(), reason); + + actionsStore.addAction(guild.getIdLong(), author.getIdLong(), target.getIdLong(), + ModerationUtils.Action.UNMUTE, null, reason); + + return guild.removeRoleFromMember(target, ModerationUtils.getMutedRole(guild).orElseThrow()) + .reason(reason); + } + + private void unmuteUserFlow(@NotNull Member target, @NotNull Member author, + @NotNull String reason, @NotNull Guild guild, @NotNull SlashCommandEvent event) { + sendDm(target, reason, guild, event) + .flatMap(hasSentDm -> unmuteUser(target, author, reason, guild) + .map(banResult -> hasSentDm)) + .map(hasSentDm -> sendFeedback(hasSentDm, target, author, reason)) + .flatMap(event::replyEmbeds) + .queue(); + } + + @SuppressWarnings({"BooleanMethodNameMustStartWithQuestion", "MethodWithTooManyParameters"}) + private boolean handleChecks(@NotNull Member bot, @NotNull Member author, + @Nullable Member target, @NotNull CharSequence reason, @NotNull Guild guild, + @NotNull Interaction event) { + if (!ModerationUtils.handleRoleChangeChecks( + ModerationUtils.getMutedRole(guild).orElse(null), ACTION_VERB, target, bot, author, + guild, hasRequiredRole, reason, event)) { + return false; + } + if (Objects.requireNonNull(target) + .getRoles() + .stream() + .map(Role::getName) + .noneMatch(ModerationUtils.isMuteRole)) { + handleNotMutedTarget(event); + return false; + } + return true; + } + + @Override + public void onSlashCommand(@NotNull SlashCommandEvent event) { + Member target = Objects.requireNonNull(event.getOption(TARGET_OPTION), "The target is null") + .getAsMember(); + Member author = Objects.requireNonNull(event.getMember(), "The author is null"); + String reason = Objects.requireNonNull(event.getOption(REASON_OPTION), "The reason is null") + .getAsString(); + + Guild guild = Objects.requireNonNull(event.getGuild()); + Member bot = guild.getSelfMember(); + + if (!handleChecks(bot, author, target, reason, guild, event)) { + return; + } + unmuteUserFlow(Objects.requireNonNull(target), author, reason, guild, event); + } +} diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java index e829790546..4be65e20ae 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/system/CommandSystem.java @@ -1,5 +1,6 @@ package org.togetherjava.tjbot.commands.system; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.TextChannel; @@ -55,11 +56,12 @@ public final class CommandSystem extends ListenerAdapter implements SlashCommand *

* Commands are fetched from {@link Commands}. * + * @param jda the JDA instance that this command system will be used with * @param database the database that commands may use to persist data */ @SuppressWarnings("ThisEscapedInObjectConstruction") - public CommandSystem(@NotNull Database database) { - nameToSlashCommands = Commands.createSlashCommands(database) + public CommandSystem(@NotNull JDA jda, @NotNull Database database) { + nameToSlashCommands = Commands.createSlashCommands(jda, database) .stream() .collect(Collectors.toMap(SlashCommand::getName, Function.identity())); diff --git a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java index e4ef54ce36..c32b8b5d10 100644 --- a/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java +++ b/application/src/main/java/org/togetherjava/tjbot/routines/ModAuditLogRoutine.java @@ -15,6 +15,7 @@ import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.togetherjava.tjbot.commands.moderation.ModerationUtils; import org.togetherjava.tjbot.config.Config; import org.togetherjava.tjbot.db.Database; import org.togetherjava.tjbot.db.generated.tables.ModAuditLogGuildProcess; @@ -49,7 +50,6 @@ public final class ModAuditLogRoutine { private static final Color AMBIENT_COLOR = Color.decode("#4FC3F7"); private final Predicate isAuditLogChannel; - private final Predicate isMutedRole; private final Database database; private final JDA jda; private final ScheduledExecutorService checkAuditLogService = @@ -67,8 +67,6 @@ public ModAuditLogRoutine(@NotNull JDA jda, @NotNull Database database) { .asMatchPredicate(); isAuditLogChannel = channel -> isAuditLogChannelName.test(channel.getName()); - isMutedRole = - Pattern.compile(Config.getInstance().getMutedRolePattern()).asMatchPredicate(); this.database = database; this.jda = jda; } @@ -293,8 +291,8 @@ private void handleAuditLogs(@NotNull MessageChannel auditLogChannel, }); } - private Optional> handleAuditLog(@NotNull MessageChannel auditLogChannel, - @NotNull AuditLogEntry entry) { + private static Optional> handleAuditLog( + @NotNull MessageChannel auditLogChannel, @NotNull AuditLogEntry entry) { Optional> maybeMessage = switch (entry.getType()) { case BAN -> handleBanEntry(entry); case UNBAN -> handleUnbanEntry(entry); @@ -306,7 +304,7 @@ private Optional> handleAuditLog(@NotNull MessageChannel aud return maybeMessage.map(message -> message.flatMap(auditLogChannel::sendMessageEmbeds)); } - private @NotNull Optional> handleRoleUpdateEntry( + private static @NotNull Optional> handleRoleUpdateEntry( @NotNull AuditLogEntry entry) { if (containsMutedRole(entry, AuditLogKey.MEMBER_ROLES_ADD)) { return handleMuteEntry(entry); @@ -317,7 +315,8 @@ private Optional> handleAuditLog(@NotNull MessageChannel aud return Optional.empty(); } - private boolean containsMutedRole(@NotNull AuditLogEntry entry, @NotNull AuditLogKey key) { + private static boolean containsMutedRole(@NotNull AuditLogEntry entry, + @NotNull AuditLogKey key) { List> roleChanges = Optional.ofNullable(entry.getChangeByKey(key)) .>>map(AuditLogChange::getNewValue) .orElse(List.of()); @@ -326,7 +325,7 @@ private boolean containsMutedRole(@NotNull AuditLogEntry entry, @NotNull AuditLo .flatMap(Collection::stream) .filter(changeEntry -> "name".equals(changeEntry.getKey())) .map(Map.Entry::getValue) - .anyMatch(isMutedRole); + .anyMatch(ModerationUtils.isMuteRole); } private Optional getModAuditLogChannel(@NotNull Guild guild) {