Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 548e14a

Browse files
committed
Added checkstyle and fixed issues in classes.
1 parent faad5e5 commit 548e14a

23 files changed

+271
-29
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
.idea/
22
.gradle/
33
build/
4-
/config/
4+
/javabot_config/

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
id 'java'
3+
id 'checkstyle'
34
}
45

56
group 'net.javadiscord'
@@ -16,7 +17,7 @@ dependencies {
1617
implementation 'org.javacord:javacord:3.3.2'
1718

1819
implementation 'org.yaml:snakeyaml:1.29'
19-
implementation 'com.google.code.gson:gson:2.8.8'
20+
implementation 'com.google.code.gson:gson:2.8.9'
2021

2122
// Persistence Dependencies
2223
implementation 'org.mongodb:mongodb-driver:3.12.10'
@@ -45,4 +46,9 @@ jar {
4546

4647
test {
4748
useJUnitPlatform()
49+
}
50+
51+
checkstyle {
52+
toolVersion '9.1'
53+
configDirectory.set(new File("checkstyle"))
4854
}

checkstyle/checkstyle.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE module PUBLIC
3+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
4+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
5+
<module name="Checker">
6+
<module name="TreeWalker">
7+
<property name="fileExtensions" value="java"/>
8+
<module name="ArrayTypeStyle"/>
9+
10+
<!-- Javadoc Checks -->
11+
<module name="AtclauseOrder"/>
12+
<module name="NonEmptyAtclauseDescription"/>
13+
<module name="JavadocStyle"/>
14+
<module name="SummaryJavadocCheck"/>
15+
<module name="JavadocMethod"/>
16+
<module name="JavadocType"/>
17+
<module name="MissingJavadocType">
18+
<property name="scope" value="protected"/>
19+
</module>
20+
<module name="MissingJavadocMethod">
21+
<property name="scope" value="protected"/>
22+
<property name="allowMissingPropertyJavadoc" value="true"/>
23+
<property name="minLineCount" value="2"/>
24+
</module>
25+
<module name="JavadocVariable">
26+
<property name="scope" value="protected"/>
27+
</module>
28+
<module name="JavadocContentLocationCheck"/>
29+
<module name="JavadocBlockTagLocation"/>
30+
<module name="JavadocMissingLeadingAsterisk"/>
31+
<module name="JavadocMissingWhitespaceAfterAsterisk"/>
32+
33+
<!-- Code Quality -->
34+
<module name="EmptyBlock"/>
35+
<module name="LeftCurly"/>
36+
<module name="NeedBraces">
37+
<property name="allowSingleLineStatement" value="true"/>
38+
</module>
39+
<module name="RightCurly"/>
40+
<module name="DeclarationOrder"/>
41+
<module name="FallThrough"/>
42+
<module name="IllegalCatch"/>
43+
<module name="IllegalThrows"/>
44+
<module name="IllegalToken"/>
45+
<module name="IllegalType"/>
46+
<module name="InnerAssignment"/>
47+
<module name="MagicNumber"/>
48+
<module name="MultipleStringLiterals">
49+
<property name="allowedDuplicates" value="3"/>
50+
</module>
51+
<module name="MultipleVariableDeclarations"/>
52+
<module name="NoClone"/>
53+
<module name="OneStatementPerLine"/>
54+
<module name="PackageDeclaration"/>
55+
<module name="ReturnCount">
56+
<property name="max" value="5"/>
57+
<property name="maxForVoid" value="8"/>
58+
</module>
59+
<module name="SimplifyBooleanExpression"/>
60+
<module name="SimplifyBooleanReturn"/>
61+
<module name="StringLiteralEquality"/>
62+
<module name="UnnecessaryParentheses"/>
63+
<module name="UnnecessarySemicolonAfterOuterTypeDeclaration"/>
64+
<module name="UnnecessarySemicolonAfterTypeMemberDeclaration"/>
65+
<module name="UnnecessarySemicolonInEnumeration"/>
66+
<module name="UnnecessarySemicolonInTryWithResources"/>
67+
<module name="InnerTypeLast"/>
68+
<module name="HideUtilityClassConstructor"/>
69+
<module name="InterfaceIsType"/>
70+
<module name="MutableException"/>
71+
<module name="OneTopLevelClass"/>
72+
<module name="ModifierOrder"/>
73+
<module name="RedundantModifier"/>
74+
<!-- Custom regex for finding space indentation violations. -->
75+
<module name="RegexpSinglelineJava">
76+
<property name="format" value="^\t* +\t*\S"/>
77+
<property name="message" value="Line has leading space characters; indentation should be performed with tabs only."/>
78+
<property name="ignoreComments" value="true"/>
79+
</module>
80+
<module name="MethodLength">
81+
<property name="max" value="50"/>
82+
</module>
83+
</module>
84+
</module>

checkstyle/suppressions.xml

Whitespace-only changes.

src/main/java/net/javadiscord/javabot2/Bot.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public class Bot {
3737
*/
3838
public static ScheduledExecutorService asyncPool;
3939

40+
// Hide constructor.
41+
private Bot() {}
42+
43+
/**
44+
* Starts the bot.
45+
* @param args Command-line arguments.
46+
*/
4047
public static void main(String[] args) {
4148
initDataSources();
4249
asyncPool = Executors.newScheduledThreadPool(config.getSystems().getAsyncPoolSize());
@@ -56,7 +63,7 @@ public static void main(String[] args) {
5663
* with the Discord API.
5764
*/
5865
private static void initDataSources() {
59-
config = new BotConfig(Path.of("config"));
66+
config = new BotConfig(Path.of("javabot_config"));
6067
if (config.getSystems().getDiscordBotToken() == null || config.getSystems().getDiscordBotToken().isBlank()) {
6168
throw new IllegalStateException("Missing required Discord bot token! Please edit config/systems.json to add it, then run again.");
6269
}

src/main/java/net/javadiscord/javabot2/command/ResponseException.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,37 @@
1111
* advantage of throwing an exception up the call stack.
1212
*/
1313
public class ResponseException extends Exception {
14+
/**
15+
* The response builder that's used to respond to an interaction in the case
16+
* of an exception.
17+
*/
1418
@Getter
1519
private final InteractionImmediateResponseBuilder responseBuilder;
1620

21+
/**
22+
* Constructs the exception.
23+
* @param responseBuilder The response builder to use.
24+
*/
1725
public ResponseException(InteractionImmediateResponseBuilder responseBuilder) {
1826
this.responseBuilder = responseBuilder;
1927
}
2028

29+
/**
30+
* Gets a supplier that supplies a response exception with a warning
31+
* response. This is especially useful with {@link java.util.Optional#orElseThrow(Supplier)}.
32+
* @param message The warning message.
33+
* @return The exception supplier.
34+
*/
2135
public static Supplier<ResponseException> warning(String message) {
2236
return () -> new ResponseException(Responses.deferredWarningBuilder().message(message).build());
2337
}
2438

39+
/**
40+
* Gets a supplier that supplies a response exception with an error
41+
* response. This is especially useful with {@link java.util.Optional#orElseThrow(Supplier)}.
42+
* @param message The error message.
43+
* @return The exception supplier.
44+
*/
2545
public static Supplier<ResponseException> error(String message) {
2646
return () -> new ResponseException(Responses.deferredErrorBuilder().message(message).build());
2747
}

src/main/java/net/javadiscord/javabot2/command/Responses.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
* Provides methods for standardized replies to interaction events.
1515
*/
1616
public class Responses {
17+
private Responses() {}
18+
1719
public static InteractionImmediateResponseBuilder success(InteractionBase interaction, String title, String message) {
1820
return reply(interaction, title, message, Color.GREEN, true);
1921
}
@@ -112,35 +114,69 @@ private ResponseBuilder(Color color) {
112114
this(null, color);
113115
}
114116

117+
/**
118+
* Sets the title of the response.
119+
* @param title The title to show.
120+
* @return The response builder.
121+
*/
115122
public ResponseBuilder title(String title) {
116123
this.title = title;
117124
return this;
118125
}
119126

127+
/**
128+
* Sets the message of the response.
129+
* @param message The message to show.
130+
* @return The response builder.
131+
*/
120132
public ResponseBuilder message(String message) {
121133
this.message = message;
122134
return this;
123135
}
124136

137+
/**
138+
* Makes this response publicly visible, i.e. not ephemeral.
139+
* @return The response builder.
140+
*/
125141
public ResponseBuilder makePublic() {
126142
this.ephemeral = false;
127143
return this;
128144
}
129145

146+
/**
147+
* Builds a Javacord response builder.
148+
* @return A Javacord response builder.
149+
*/
130150
public InteractionImmediateResponseBuilder build() {
131151
return reply(interaction, title, message, color, ephemeral);
132152
}
133153

154+
/**
155+
* Builds a Javacord response builder using the given interaction.
156+
* @param interaction The interaction that will be responded to.
157+
* @return A Javacord response builder.
158+
*/
134159
public InteractionImmediateResponseBuilder build(InteractionBase interaction) {
135160
this.interaction = interaction;
136161
return build();
137162
}
138163

164+
/**
165+
* Sends a Javacord response to the given interaction, using this
166+
* builder's settings.
167+
* @param interaction The interaction to send the response to.
168+
* @return A future that completes once the response is sent.
169+
*/
139170
public CompletableFuture<InteractionOriginalResponseUpdater> respond(InteractionBase interaction) {
140171
this.interaction = interaction;
141172
return build().respond();
142173
}
143174

175+
/**
176+
* Sends a Javacord response as a reply to this builder's configured
177+
* interaction.
178+
* @return A future that completes once the response is sent.
179+
*/
144180
public CompletableFuture<InteractionOriginalResponseUpdater> respond() {
145181
return this.build().respond();
146182
}

src/main/java/net/javadiscord/javabot2/command/SlashCommandHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@
88
* handler in any command configuration file.
99
*/
1010
public interface SlashCommandHandler {
11-
InteractionImmediateResponseBuilder handle(SlashCommandInteraction interaction) throws Exception;
11+
/**
12+
* Handles a slash command interaction.
13+
* @param interaction The interaction.
14+
* @return An immediate response to the interaction.
15+
* @throws ResponseException If an error occurs while handling the event.
16+
*/
17+
InteractionImmediateResponseBuilder handle(SlashCommandInteraction interaction) throws ResponseException;
1218
}

src/main/java/net/javadiscord/javabot2/command/SlashCommandListener.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
*/
2121
@Slf4j
2222
public final class SlashCommandListener implements SlashCommandCreateListener {
23+
/**
24+
* The set of all slash command handlers, mapped by their ids.
25+
*/
2326
private final Map<Long, SlashCommandHandler> commandHandlers = new HashMap<>();
2427

2528
/**
@@ -42,11 +45,6 @@ public void onSlashCommandCreate(SlashCommandCreateEvent event) {
4245
handler.handle(event.getSlashCommandInteraction()).respond();
4346
} catch (ResponseException e) {
4447
e.getResponseBuilder().respond();
45-
} catch (Exception e) {
46-
log.error("An error occurred while handling a slash command.", e);
47-
Responses.errorBuilder(event)
48-
.message("An error occurred while executing the command.")
49-
.respond();
5048
}
5149
} else {
5250
Responses.warningBuilder(event)
@@ -85,7 +83,7 @@ private Map<String, SlashCommandHandler> initializeHandlers(CommandConfig[] comm
8583
try {
8684
Class<?> handlerClass = Class.forName(commandConfig.getHandler());
8785
handlers.put(commandConfig.getName(), (SlashCommandHandler) handlerClass.getConstructor().newInstance());
88-
} catch (Exception e) {
86+
} catch (ReflectiveOperationException e) {
8987
log.error("An error occurred when trying to get new instance of a slash command handler class.", e);
9088
}
9189
} else {

src/main/java/net/javadiscord/javabot2/command/data/CommandConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public class CommandConfig {
2020
private SubCommandGroupConfig[] subCommandGroups;
2121
private String handler;
2222

23+
/**
24+
* Converts this config data into data that's ready for the Discord API.
25+
* @return The prepared data.
26+
*/
2327
public SlashCommandBuilder toData() {
2428
var builder = new SlashCommandBuilder()
2529
.setName(this.name)

0 commit comments

Comments
 (0)