Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -1,7 +1,7 @@
package org.togetherjava.tjbot.commands.tags;

import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
import net.dv8tion.jda.api.interactions.Interaction;
Expand All @@ -15,11 +15,14 @@
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
import org.togetherjava.tjbot.commands.utils.MessageUtils;
import org.togetherjava.tjbot.config.Config;

import java.util.Objects;
import java.util.OptionalLong;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.regex.Pattern;

/**
* Implements the {@code /tag-manage} command which allows management of tags, such as creating,
Expand All @@ -45,6 +48,7 @@ public final class TagManageCommand extends SlashCommandAdapter {
private static final String MESSAGE_ID_OPTION = "message-id";
private static final String MESSAGE_ID_DESCRIPTION = "the id of the message to refer to";
private final TagSystem tagSystem;
private final Predicate<String> hasRequiredRole;

/**
* Creates a new instance, using the given tag system as base.
Expand All @@ -55,6 +59,8 @@ public TagManageCommand(TagSystem tagSystem) {
super("tag-manage", "Provides commands to manage all tags", SlashCommandVisibility.GUILD);

this.tagSystem = tagSystem;
hasRequiredRole =
Pattern.compile(Config.getInstance().getTagManageRolePattern()).asMatchPredicate();

// TODO Think about adding a "Are you sure"-dialog to 'edit', 'edit-with-message' and
// 'delete'
Expand Down Expand Up @@ -116,11 +122,8 @@ private static OptionalLong parseMessageIdAndHandle(@NotNull String messageId,

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
Member member = Objects.requireNonNull(event.getMember());

if (!member.hasPermission(Permission.MESSAGE_MANAGE)) {
event.reply(
"Tags can only be managed by users who have the 'MESSAGE_MANAGE' permission.")
if (!hasTagManageRole(Objects.requireNonNull(event.getMember()))) {
event.reply("Tags can only be managed by users with a corresponding role.")
.setEphemeral(true)
.queue();
return;
Expand Down Expand Up @@ -277,6 +280,10 @@ private boolean isWrongTagStatusAndHandle(@NotNull TagStatus requiredTagStatus,
return false;
}

private boolean hasTagManageRole(@NotNull Member member) {
return member.getRoles().stream().map(Role::getName).anyMatch(hasRequiredRole);
}

private enum TagStatus {
EXISTS,
NOT_EXISTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class Config {
private final String mutedRolePattern;
private final String heavyModerationRolePattern;
private final String softModerationRolePattern;
private final String tagManageRolePattern;

@SuppressWarnings("ConstructorWithTooManyParameters")
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
Expand All @@ -37,7 +38,8 @@ private Config(@JsonProperty("token") String token,
@JsonProperty("modAuditLogChannelPattern") String modAuditLogChannelPattern,
@JsonProperty("mutedRolePattern") String mutedRolePattern,
@JsonProperty("heavyModerationRolePattern") String heavyModerationRolePattern,
@JsonProperty("softModerationRolePattern") String softModerationRolePattern) {
@JsonProperty("softModerationRolePattern") String softModerationRolePattern,
@JsonProperty("tagManageRolePattern") String tagManageRolePattern) {
this.token = token;
this.databasePath = databasePath;
this.projectWebsite = projectWebsite;
Expand All @@ -46,6 +48,7 @@ private Config(@JsonProperty("token") String token,
this.mutedRolePattern = mutedRolePattern;
this.heavyModerationRolePattern = heavyModerationRolePattern;
this.softModerationRolePattern = softModerationRolePattern;
this.tagManageRolePattern = tagManageRolePattern;
}

/**
Expand Down Expand Up @@ -146,4 +149,14 @@ public String getHeavyModerationRolePattern() {
public String getSoftModerationRolePattern() {
return softModerationRolePattern;
}

/**
* Gets the REGEX pattern used to identify roles that are allowed to use the tag-manage command,
* such as creating or editing tags.
*
* @return the REGEX pattern
*/
public String getTagManageRolePattern() {
return tagManageRolePattern;
}
}