Skip to content

Commit d1fba01

Browse files
committed
Make Tag-Manage based on roles
1 parent f4593eb commit d1fba01

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

application/src/main/java/org/togetherjava/tjbot/commands/tags/TagManageCommand.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.togetherjava.tjbot.commands.tags;
22

3-
import net.dv8tion.jda.api.Permission;
43
import net.dv8tion.jda.api.entities.Member;
4+
import net.dv8tion.jda.api.entities.Role;
55
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
66
import net.dv8tion.jda.api.exceptions.ErrorResponseException;
77
import net.dv8tion.jda.api.interactions.Interaction;
@@ -15,11 +15,14 @@
1515
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
1616
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
1717
import org.togetherjava.tjbot.commands.utils.MessageUtils;
18+
import org.togetherjava.tjbot.config.Config;
1819

1920
import java.util.Objects;
2021
import java.util.OptionalLong;
2122
import java.util.function.BiConsumer;
2223
import java.util.function.Consumer;
24+
import java.util.function.Predicate;
25+
import java.util.regex.Pattern;
2326

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

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

5761
this.tagSystem = tagSystem;
62+
hasRequiredRole =
63+
Pattern.compile(Config.getInstance().getTagManageRolePattern()).asMatchPredicate();
5864

5965
// TODO Think about adding a "Are you sure"-dialog to 'edit', 'edit-with-message' and
6066
// 'delete'
@@ -118,9 +124,8 @@ private static OptionalLong parseMessageIdAndHandle(@NotNull String messageId,
118124
public void onSlashCommand(@NotNull SlashCommandEvent event) {
119125
Member member = Objects.requireNonNull(event.getMember());
120126

121-
if (!member.hasPermission(Permission.MESSAGE_MANAGE)) {
122-
event.reply(
123-
"Tags can only be managed by users who have the 'MESSAGE_MANAGE' permission.")
127+
if (member.getRoles().stream().map(Role::getName).noneMatch(hasRequiredRole)) {
128+
event.reply("Tags can only be managed by users with a corresponding role.")
124129
.setEphemeral(true)
125130
.queue();
126131
return;

application/src/main/java/org/togetherjava/tjbot/config/Config.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public final class Config {
2727
private final String mutedRolePattern;
2828
private final String heavyModerationRolePattern;
2929
private final String softModerationRolePattern;
30+
private final String tagManageRolePattern;
3031

3132
@SuppressWarnings("ConstructorWithTooManyParameters")
3233
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
@@ -37,7 +38,8 @@ private Config(@JsonProperty("token") String token,
3738
@JsonProperty("modAuditLogChannelPattern") String modAuditLogChannelPattern,
3839
@JsonProperty("mutedRolePattern") String mutedRolePattern,
3940
@JsonProperty("heavyModerationRolePattern") String heavyModerationRolePattern,
40-
@JsonProperty("softModerationRolePattern") String softModerationRolePattern) {
41+
@JsonProperty("softModerationRolePattern") String softModerationRolePattern,
42+
@JsonProperty("tagManageRolePattern") String tagManageRolePattern) {
4143
this.token = token;
4244
this.databasePath = databasePath;
4345
this.projectWebsite = projectWebsite;
@@ -46,6 +48,7 @@ private Config(@JsonProperty("token") String token,
4648
this.mutedRolePattern = mutedRolePattern;
4749
this.heavyModerationRolePattern = heavyModerationRolePattern;
4850
this.softModerationRolePattern = softModerationRolePattern;
51+
this.tagManageRolePattern = tagManageRolePattern;
4952
}
5053

5154
/**
@@ -146,4 +149,14 @@ public String getHeavyModerationRolePattern() {
146149
public String getSoftModerationRolePattern() {
147150
return softModerationRolePattern;
148151
}
152+
153+
/**
154+
* Gets the REGEX pattern used to identify roles that are allowed to use the tag-manage command,
155+
* such as creating or editing tags.
156+
*
157+
* @return the REGEX pattern
158+
*/
159+
public String getTagManageRolePattern() {
160+
return tagManageRolePattern;
161+
}
149162
}

0 commit comments

Comments
 (0)