Skip to content

Commit 2623c2e

Browse files
committed
Change server to guild for JDA classes to adapt to JDA terminology
1 parent 6a488f1 commit 2623c2e

File tree

16 files changed

+464
-464
lines changed

16 files changed

+464
-464
lines changed

config/spotbugs/spotbugs-exclude.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
<Class name="net.kautler.command.api.restriction.javacord.ServerJavacord"/>
8080
<Class name="net.kautler.command.api.restriction.javacord.UserJavacord"/>
8181
<Class name="net.kautler.command.api.restriction.jda.ChannelJda"/>
82+
<Class name="net.kautler.command.api.restriction.jda.GuildJda"/>
8283
<Class name="net.kautler.command.api.restriction.jda.RoleJda"/>
83-
<Class name="net.kautler.command.api.restriction.jda.ServerJda"/>
8484
<Class name="net.kautler.command.api.restriction.jda.UserJda"/>
8585
</Or>
8686
<Or>

src/main/java/net/kautler/command/api/restriction/javacord/ServerJavacord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public abstract class ServerJavacord implements Restriction<Message> {
5757
private final Pattern serverPattern;
5858

5959
/**
60-
* Constructs a new server restriction for checking the channel ID.
60+
* Constructs a new server restriction for checking the server ID.
6161
*
6262
* @param serverId the ID of the server where a command should be allowed
6363
*/

src/main/java/net/kautler/command/api/restriction/javacord/UserJavacord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public abstract class UserJavacord implements Restriction<Message> {
5757
private final Pattern userPattern;
5858

5959
/**
60-
* Constructs a new user restriction for checking the channel ID.
60+
* Constructs a new user restriction for checking the user ID.
6161
*
6262
* @param userId the ID of the user for whom a command should be allowed
6363
*/
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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+
}

src/main/java/net/kautler/command/api/restriction/jda/ServerOwnerJda.java renamed to src/main/java/net/kautler/command/api/restriction/jda/GuildOwnerJda.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
import static java.lang.Boolean.FALSE;
2727

2828
/**
29-
* A restriction that allows a command for the server owner and is evaluated by the JDA command handler.
30-
* If a message is not sent on a server, this restriction always denies.
29+
* A restriction that allows a command for the guild owner and is evaluated by the JDA command handler.
30+
* If a message is not sent on a guild, this restriction always denies.
3131
*/
3232
@ApplicationScoped
33-
public class ServerOwnerJda implements Restriction<Message> {
33+
public class GuildOwnerJda implements Restriction<Message> {
3434
/**
35-
* Constructs a new server owner restriction.
35+
* Constructs a new guild owner restriction.
3636
*/
37-
private ServerOwnerJda() {
37+
private GuildOwnerJda() {
3838
}
3939

4040
@Override

src/main/java/net/kautler/command/api/restriction/jda/NsfwChannelJda.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
/**
3030
* A restriction that allows a command for NSFW channels and is evaluated by the JDA command handler.
31-
* If a message is not sent on a server, this restriction always denies.
31+
* If a message is not sent on a guild, this restriction always denies.
3232
*/
3333
@ApplicationScoped
3434
public class NsfwChannelJda implements Restriction<Message> {

src/main/java/net/kautler/command/api/restriction/jda/RoleJda.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private boolean allowCommandByAtLeastRoleId(Message message) {
283283
return Optional.of(message)
284284
.filter(msg -> msg.getChannelType().isGuild())
285285
.map(Message::getGuild)
286-
.flatMap(server -> Optional.ofNullable(server.getRoleById(roleId))
286+
.flatMap(guild -> Optional.ofNullable(guild.getRoleById(roleId))
287287
.flatMap(role -> message
288288
.getMember()
289289
.getRoles()
@@ -340,13 +340,13 @@ private boolean allowCommandByAtLeastRoleName(Message message) {
340340
return Optional.of(message)
341341
.filter(msg -> msg.getChannelType().isGuild())
342342
.map(Message::getGuild)
343-
.flatMap(server -> {
343+
.flatMap(guild -> {
344344
Stream<Role> roleStream;
345345
if (this.roleName == null) {
346-
roleStream = server.getRoles().stream()
346+
roleStream = guild.getRoles().stream()
347347
.filter(role -> rolePattern.matcher(role.getName()).matches());
348348
} else {
349-
roleStream = server.getRolesByName(roleName, !caseSensitive).stream();
349+
roleStream = guild.getRolesByName(roleName, !caseSensitive).stream();
350350
}
351351
return roleStream
352352
.min(naturalOrder())

0 commit comments

Comments
 (0)