|
| 1 | +/* |
| 2 | + * Copyright 2019 Björn Kautler |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.kautler.command.api.restriction.jda; |
| 18 | + |
| 19 | +import net.dv8tion.jda.api.entities.Guild; |
| 20 | +import net.dv8tion.jda.api.entities.ISnowflake; |
| 21 | +import net.dv8tion.jda.api.entities.Message; |
| 22 | +import net.kautler.command.api.restriction.Restriction; |
| 23 | + |
| 24 | +import javax.enterprise.context.ApplicationScoped; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.StringJoiner; |
| 27 | +import java.util.regex.Pattern; |
| 28 | + |
| 29 | +import static java.lang.Boolean.FALSE; |
| 30 | +import static java.lang.String.format; |
| 31 | + |
| 32 | +/** |
| 33 | + * A restriction that allows a command in certain guilds and is evaluated by the JDA command handler. |
| 34 | + * To use it, create a trivial subclass of this class and make it a discoverable CDI bean, |
| 35 | + * for example by annotating it with {@link ApplicationScoped @ApplicationScoped}. |
| 36 | + */ |
| 37 | +public abstract class GuildJda implements Restriction<Message> { |
| 38 | + /** |
| 39 | + * The ID of the guild where a command is allowed. |
| 40 | + */ |
| 41 | + private final long guildId; |
| 42 | + |
| 43 | + /** |
| 44 | + * The name of the guild where a command is allowed. |
| 45 | + */ |
| 46 | + private final String guildName; |
| 47 | + |
| 48 | + /** |
| 49 | + * Whether the {@code guildName} should be case sensitive or not. |
| 50 | + * This does not apply to the {@code guildPattern}, |
| 51 | + * where an embedded flag can be used to control case sensitivity. |
| 52 | + */ |
| 53 | + private final boolean caseSensitive; |
| 54 | + |
| 55 | + /** |
| 56 | + * The pattern guild names are matched against to determine whether a command is allowed. |
| 57 | + */ |
| 58 | + private final Pattern guildPattern; |
| 59 | + |
| 60 | + /** |
| 61 | + * Constructs a new guild restriction for checking the guild ID. |
| 62 | + * |
| 63 | + * @param guildId the ID of the guild where a command should be allowed |
| 64 | + */ |
| 65 | + protected GuildJda(long guildId) { |
| 66 | + this(guildId, null, true, null); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Constructs a new guild restriction for checking the guild name case-sensitively against a fixed name. |
| 71 | + * |
| 72 | + * @param guildName the case-sensitive name of the guild where a command should be allowed |
| 73 | + */ |
| 74 | + protected GuildJda(String guildName) { |
| 75 | + this(0, guildName, true, null); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Constructs a new guild restriction for checking the guild name against a fixed name. |
| 80 | + * |
| 81 | + * @param guildName the name of the guild where a command should be allowed |
| 82 | + * @param caseSensitive whether the name should be matched case-sensitively or not |
| 83 | + */ |
| 84 | + protected GuildJda(String guildName, boolean caseSensitive) { |
| 85 | + this(0, guildName, caseSensitive, null); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Constructs a new guild restriction for checking the guild name against a regular expression. |
| 90 | + * |
| 91 | + * @param guildPattern the pattern against which the guild name is matched |
| 92 | + * to determine where a command should be allowed |
| 93 | + */ |
| 94 | + protected GuildJda(Pattern guildPattern) { |
| 95 | + this(0, null, true, guildPattern); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Constructs a new guild restriction. |
| 100 | + * |
| 101 | + * @param guildId the ID of the guild where a command should be allowed |
| 102 | + * @param guildName the name of the guild where a command should be allowed |
| 103 | + * @param caseSensitive whether the name should be matched case-sensitively or not |
| 104 | + * @param guildPattern the pattern against which the guild name is matched |
| 105 | + * to determine where a command should be allowed |
| 106 | + */ |
| 107 | + private GuildJda(long guildId, String guildName, boolean caseSensitive, Pattern guildPattern) { |
| 108 | + this.guildId = guildId; |
| 109 | + this.guildName = guildName; |
| 110 | + this.caseSensitive = caseSensitive; |
| 111 | + this.guildPattern = guildPattern; |
| 112 | + ensureInvariants(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Checks the invariants of this instance and raises |
| 117 | + * an {@link IllegalStateException} if they are violated. |
| 118 | + */ |
| 119 | + private void ensureInvariants() { |
| 120 | + ensureAtMostOneConditionIsSet(); |
| 121 | + ensureAtLeastOneConditionIsSet(); |
| 122 | + ensureCaseSensitiveIfNameIsNotSet(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Checks that at most one condition is set and raises an {@link IllegalStateException} otherwise. |
| 127 | + */ |
| 128 | + private void ensureAtMostOneConditionIsSet() { |
| 129 | + boolean guildIdSet = guildId != 0; |
| 130 | + boolean guildNameSet = guildName != null; |
| 131 | + boolean guildPatternSet = guildPattern != null; |
| 132 | + |
| 133 | + boolean guildNamelySet = guildNameSet || guildPatternSet; |
| 134 | + boolean guildIdAndNamelySet = guildIdSet && guildNamelySet; |
| 135 | + boolean bothGuildNamelySet = guildNameSet && guildPatternSet; |
| 136 | + boolean multipleConditionsSet = guildIdAndNamelySet || bothGuildNamelySet; |
| 137 | + |
| 138 | + if (multipleConditionsSet) { |
| 139 | + StringJoiner stringJoiner = new StringJoiner(", "); |
| 140 | + if (guildIdSet) { |
| 141 | + stringJoiner.add("guildId"); |
| 142 | + } |
| 143 | + if (guildNameSet) { |
| 144 | + stringJoiner.add("guildName"); |
| 145 | + } |
| 146 | + if (guildPatternSet) { |
| 147 | + stringJoiner.add("guildPattern"); |
| 148 | + } |
| 149 | + throw new IllegalStateException(format( |
| 150 | + "Only one of guildId, guildName and guildPattern should be given (%s)", |
| 151 | + stringJoiner)); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Checks that at least one condition is set and raises an {@link IllegalStateException} otherwise. |
| 157 | + */ |
| 158 | + private void ensureAtLeastOneConditionIsSet() { |
| 159 | + boolean guildIdSet = guildId != 0; |
| 160 | + boolean guildNameSet = guildName != null; |
| 161 | + boolean guildPatternSet = guildPattern != null; |
| 162 | + |
| 163 | + boolean guildNamelySet = guildNameSet || guildPatternSet; |
| 164 | + |
| 165 | + boolean atLeastOneConditionSet = guildIdSet || guildNamelySet; |
| 166 | + |
| 167 | + if (!atLeastOneConditionSet) { |
| 168 | + throw new IllegalStateException( |
| 169 | + "One of guildId, guildName and guildPattern should be given"); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Checks that {@link #caseSensitive} is {@code true} if {@link #guildName} |
| 175 | + * is not set and raises an {@link IllegalStateException} otherwise. |
| 176 | + */ |
| 177 | + private void ensureCaseSensitiveIfNameIsNotSet() { |
| 178 | + if ((guildName == null) && !caseSensitive) { |
| 179 | + throw new IllegalStateException( |
| 180 | + "If guildName is not set, caseSensitive should be true"); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean allowCommand(Message message) { |
| 186 | + return ((guildName == null) && (guildPattern == null)) |
| 187 | + ? allowCommandByGuildId(message) |
| 188 | + : allowCommandByGuildName(message); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Returns whether a command is allowed according to the configured guild ID. |
| 193 | + * |
| 194 | + * @param message the message of the command to check |
| 195 | + * @return whether a command is allowed according to the configured guild ID |
| 196 | + */ |
| 197 | + private boolean allowCommandByGuildId(Message message) { |
| 198 | + return Optional.of(message) |
| 199 | + .filter(msg -> msg.getChannelType().isGuild()) |
| 200 | + .map(Message::getGuild) |
| 201 | + .map(ISnowflake::getIdLong) |
| 202 | + .map(guildId -> guildId == this.guildId) |
| 203 | + .orElse(FALSE); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Returns whether a command is allowed according to the configured guild name or pattern. |
| 208 | + * |
| 209 | + * @param message the message of the command to check |
| 210 | + * @return whether a command is allowed according to the configured guild name or pattern |
| 211 | + */ |
| 212 | + private boolean allowCommandByGuildName(Message message) { |
| 213 | + return Optional.of(message) |
| 214 | + .filter(msg -> msg.getChannelType().isGuild()) |
| 215 | + .map(Message::getGuild) |
| 216 | + .map(Guild::getName) |
| 217 | + .map(guildName -> { |
| 218 | + if (this.guildName == null) { |
| 219 | + return guildPattern.matcher(guildName).matches(); |
| 220 | + } else if (caseSensitive) { |
| 221 | + return this.guildName.equals(guildName); |
| 222 | + } else { |
| 223 | + return this.guildName.equalsIgnoreCase(guildName); |
| 224 | + } |
| 225 | + }) |
| 226 | + .orElse(FALSE); |
| 227 | + } |
| 228 | +} |
0 commit comments