Skip to content

Commit 2d4e2a9

Browse files
committed
checkstyle is now happy
1 parent 93840d8 commit 2d4e2a9

File tree

4 files changed

+81
-60
lines changed

4 files changed

+81
-60
lines changed

src/main/java/net/javadiscord/javabot/systems/help/AutoCodeFormatter.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class AutoCodeFormatter {
3030
/**
3131
* Method responsible for finding a codeblock, if present.
3232
*
33+
* @param event a {@link MessageReceivedEvent}.
3334
* @return a MessageCodeblock instance, holding a startIndex, content and an endIndex
3435
*/
3536
@Nullable
@@ -45,26 +46,35 @@ private static AutoCodeFormatter.CodeBlock findCodeblock(@NotNull MessageReceive
4546
return new CodeBlock(startIndex, endIndex);
4647
}
4748

49+
/**
50+
* called by {@link HelpListener#onMessageReceived(MessageReceivedEvent)} on every message.
51+
* It is worth noting that this class can't register as an event handler itself due to there being no way of
52+
* setting its methods to be strictly called after {@link HelpListener}.
53+
*
54+
* @param event a {@link MessageReceivedEvent}
55+
*/
4856
public void handleMessageEvent(@Nonnull MessageReceivedEvent event) {
4957
if (!event.isFromGuild()) return;
5058
if (event.getAuthor().isBot() || event.getMessage().getAuthor().isSystem()) return;
5159
if (autoMod.hasSuspiciousLink(event.getMessage()) || autoMod.hasAdvertisingLink(event.getMessage())) return;
5260
if (event.isWebhookMessage()) return;
5361
if (!event.isFromThread()) return;
54-
if (event.getChannel().asThreadChannel().getParentChannel().getIdLong() != botConfig.get(event.getGuild()).getHelpConfig().getHelpForumChannelId())
62+
if (event.getChannel().asThreadChannel().getParentChannel().getIdLong() != botConfig.get(event.getGuild()).getHelpConfig().getHelpForumChannelId()) {
5563
return;
56-
if (!Boolean.parseBoolean(preferenceService.getOrCreate(Objects.requireNonNull(event.getMember()).getIdLong(), Preference.FORMAT_UNFORMATTED_CODE).getState()))
64+
}
65+
if (!Boolean.parseBoolean(preferenceService.getOrCreate(Objects.requireNonNull(event.getMember()).getIdLong(), Preference.FORMAT_UNFORMATTED_CODE).getState())) {
5766
return;
67+
}
5868

5969

6070
if (event.getMessage().getContentRaw().contains("```")) return; // exit if already contains codeblock
6171

6272
CodeBlock code = findCodeblock(event);
6373
if (code == null) return;
6474

65-
if (event.getMessage().getMentions().getUsers().isEmpty() && event.getChannel().asThreadChannel().getTotalMessageCount() > 1)
75+
if (event.getMessage().getMentions().getUsers().isEmpty() && event.getChannel().asThreadChannel().getTotalMessageCount() > 1) {
6676
replaceUnformattedCode(event.getMessage().getContentRaw(), code.startIndex(), code.endIndex(), event);
67-
else sendFormatHint(event);
77+
} else sendFormatHint(event);
6878
}
6979

7080
private void sendFormatHint(MessageReceivedEvent event) {
@@ -80,7 +90,7 @@ private void replaceUnformattedCode(String msg, int codeStartIndex, int codeEndI
8090
}
8191
String messageContent = msg.substring(0, codeStartIndex) + " ```" + msg.substring(codeStartIndex, codeEndIndex) + " ```" + msg.substring(codeEndIndex);
8292
EmbedBuilder autoformatInfo = new EmbedBuilder().setDescription(botConfig.get(event.getGuild()).getHelpConfig().getAutoformatInfoMessage());
83-
WebhookUtil.ensureWebhookExists(event.getChannel().asThreadChannel().getParentChannel().asForumChannel(), (wh) -> WebhookUtil.replaceMemberMessage(wh, event.getMessage(), messageContent, event.getChannel().getIdLong(), autoformatInfo.build(), formatHintEmbed(event.getGuild())), (e) -> ExceptionLogger.capture(e, "Error creating webhook for UnformattedCodeListener"));
93+
WebhookUtil.ensureWebhookExists(event.getChannel().asThreadChannel().getParentChannel().asForumChannel(), wh -> WebhookUtil.replaceMemberMessage(wh, event.getMessage(), messageContent, event.getChannel().getIdLong(), autoformatInfo.build(), formatHintEmbed(event.getGuild())), e -> ExceptionLogger.capture(e, "Error creating webhook for UnformattedCodeListener"));
8494
}
8595

8696
private MessageEmbed formatHintEmbed(Guild guild) {

src/main/java/net/javadiscord/javabot/systems/help/HelpListener.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ protected boolean removeEldestEntry(Map.Entry<Long, Long> eldest) {
6464
};
6565
private final AutoCodeFormatter autoCodeFormatter;
6666

67+
/**
68+
* The constructor of this class, which is set corresponding to lombok.
69+
*
70+
* @param autoMod automod reference.
71+
* @param preferenceService UserPreferenceService reference
72+
* @param botConfig BotConfig reference
73+
* @param helpTransactionRepository HelpAccountRepository reference
74+
* @param helpAccountRepository HelpAccountRepository reference
75+
* @param dbActions DbActions reference
76+
* @param experienceService ExperienceService reference
77+
*/
6778
public HelpListener(AutoMod autoMod, UserPreferenceService preferenceService, BotConfig botConfig, HelpAccountRepository helpAccountRepository, HelpTransactionRepository helpTransactionRepository, HelpExperienceService experienceService, DbActions dbActions) {
6879
this.preferenceService = preferenceService;
6980
this.botConfig = botConfig;

src/main/java/net/javadiscord/javabot/systems/user_preferences/model/Preference.java

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@
66
* Contains all preferences users can set.
77
*/
88
public enum Preference {
9-
/**
10-
* Enables/Disables QOTW reminders.
11-
*/
12-
QOTW_REMINDER("Question of the Week Reminder", "false", new BooleanPreference()),
13-
/**
14-
* Enables/Disables DM notifications for dormant help posts.
15-
*/
16-
PRIVATE_DORMANT_NOTIFICATIONS("Send notifications about dormant help post via DM", "true", new BooleanPreference()),
9+
/**
10+
* Enables/Disables QOTW reminders.
11+
*/
12+
QOTW_REMINDER("Question of the Week Reminder", "false", new BooleanPreference()),
13+
/**
14+
* Enables/Disables DM notifications for dormant help posts.
15+
*/
16+
PRIVATE_DORMANT_NOTIFICATIONS("Send notifications about dormant help post via DM", "true", new BooleanPreference()),
1717

18-
/**
19-
* Enables/Disables DM notifications for closed help posts.
20-
*/
21-
PRIVATE_CLOSE_NOTIFICATIONS("Send notifications about help posts closed by other users via DM", "true", new BooleanPreference()),
22-
/**
23-
* Enables / Disables AutoCodeFormatter for help posts.
24-
* Used by {@link net.javadiscord.javabot.systems.help.AutoCodeFormatter}
25-
*/
26-
FORMAT_UNFORMATTED_CODE("Automatically detect and add missing code syntax highlighting", "true", new BooleanPreference());
27-
private final String name;
28-
@Getter
18+
/**
19+
* Enables/Disables DM notifications for closed help posts.
20+
*/
21+
PRIVATE_CLOSE_NOTIFICATIONS("Send notifications about help posts closed by other users via DM", "true", new BooleanPreference()),
22+
/**
23+
* Enables / Disables AutoCodeFormatter for help posts.
24+
* Used by {@link net.javadiscord.javabot.systems.help.AutoCodeFormatter}
25+
*/
26+
FORMAT_UNFORMATTED_CODE("Automatically detect and add missing code syntax highlighting", "true", new BooleanPreference());
27+
private final String name;
28+
@Getter
2929
private final String defaultState;
30-
@Getter
30+
@Getter
3131
private final PreferenceType type;
3232

33-
Preference(String name, String defaultState, PreferenceType type) {
34-
this.name = name;
35-
this.defaultState = defaultState;
36-
this.type = type;
37-
}
33+
Preference(String name, String defaultState, PreferenceType type) {
34+
this.name = name;
35+
this.defaultState = defaultState;
36+
this.type = type;
37+
}
3838

39-
@Override
40-
public String toString() {
41-
return name;
42-
}
39+
@Override
40+
public String toString() {
41+
return name;
42+
}
4343

4444
}

src/main/java/net/javadiscord/javabot/util/WebhookUtil.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ public static void ensureWebhookExists(@NotNull IWebhookContainer channel, @NotN
6363
}
6464
};
6565
channel.retrieveWebhooks().queue(webhooks -> {
66-
Optional<Webhook> hook = webhooks.stream()
67-
.filter(webhook -> webhook.getChannel().getIdLong() == channel.getIdLong())
68-
.filter(wh -> wh.getToken() != null).findAny();
66+
Optional<Webhook> hook = webhooks.stream().filter(webhook -> webhook.getChannel().getIdLong() == channel.getIdLong()).filter(wh -> wh.getToken() != null).findAny();
6967
if (hook.isPresent()) {
7068
safeCallback.accept(hook.get());
7169
} else {
@@ -88,12 +86,8 @@ public static void ensureWebhookExists(@NotNull IWebhookContainer channel, @NotN
8886
* the message
8987
*/
9088
public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNull Webhook webhook, @NotNull Message originalMessage, String newMessageContent, long threadId, @Nullable List<LayoutComponent> components, @Nullable List<MessageEmbed> embeds) {
91-
JDAWebhookClient client = new WebhookClientBuilder(webhook.getIdLong(), webhook.getToken())
92-
.setThreadId(threadId).buildJDA();
93-
WebhookMessageBuilder message = new WebhookMessageBuilder().setContent(newMessageContent)
94-
.setAllowedMentions(AllowedMentions.none())
95-
.setAvatarUrl(transformOrNull(originalMessage.getMember(), Member::getEffectiveAvatarUrl))
96-
.setUsername(transformOrNull(originalMessage.getMember(), Member::getEffectiveName));
89+
JDAWebhookClient client = new WebhookClientBuilder(webhook.getIdLong(), webhook.getToken()).setThreadId(threadId).buildJDA();
90+
WebhookMessageBuilder message = new WebhookMessageBuilder().setContent(newMessageContent).setAllowedMentions(AllowedMentions.none()).setAvatarUrl(transformOrNull(originalMessage.getMember(), Member::getEffectiveAvatarUrl)).setUsername(transformOrNull(originalMessage.getMember(), Member::getEffectiveName));
9791
if (components != null && !components.isEmpty()) {
9892
message.addComponents(components);
9993
}
@@ -103,37 +97,43 @@ public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNull
10397
}
10498
message.addEmbeds(embeds.stream().map(e -> WebhookEmbedBuilder.fromJDA(e).build()).toList());
10599
List<Attachment> attachments = originalMessage.getAttachments();
106-
@SuppressWarnings("unchecked")
107-
CompletableFuture<?>[] futures = new CompletableFuture<?>[attachments.size()];
100+
@SuppressWarnings("unchecked") CompletableFuture<?>[] futures = new CompletableFuture<?>[attachments.size()];
108101
for (int i = 0; i < attachments.size(); i++) {
109102
Attachment attachment = attachments.get(i);
110-
futures[i] = attachment.getProxy().download().thenAccept(
111-
is -> message.addFile((attachment.isSpoiler() ? "SPOILER_" : "") + attachment.getFileName(), is));
103+
futures[i] = attachment.getProxy().download().thenAccept(is -> message.addFile((attachment.isSpoiler() ? "SPOILER_" : "") + attachment.getFileName(), is));
112104
}
113-
return CompletableFuture.allOf(futures).thenCompose(unused -> sendMessage(client, message))
114-
.whenComplete((result, err) -> {
115-
client.close();
116-
if( err != null) {
117-
ExceptionLogger.capture(err, WebhookUtil.class.getSimpleName());
118-
}
119-
});
105+
return CompletableFuture.allOf(futures).thenCompose(unused -> sendMessage(client, message)).whenComplete((result, err) -> {
106+
client.close();
107+
if (err != null) {
108+
ExceptionLogger.capture(err, WebhookUtil.class.getSimpleName());
109+
}
110+
});
120111
}
121112

122113
private static <T, R> R transformOrNull(T toTransform, Function<T, R> transformer) {
123114
return toTransform == null ? null : transformer.apply(toTransform);
124115
}
125116

126117
private static @NotNull CompletableFuture<ReadonlyMessage> sendMessage(JDAWebhookClient client, WebhookMessageBuilder message) {
127-
if(message.isEmpty()) {
118+
if (message.isEmpty()) {
128119
message.setContent("<empty message>");
129120
}
130121
return client.send(message.build());
131122
}
123+
124+
/**
125+
* Method for replacing a user's guild message through a webhook.
126+
*
127+
* @param webhook a reference to a webhook
128+
* @param originalMessage a reference to the {@link Message} object that should be replaced
129+
* @param newMessageContent a String containing the new message's content
130+
* @param threadId id of the thread in which the message should be replaced
131+
* @param embeds optional additional embeds to be added
132+
*/
132133
public static void replaceMemberMessage(Webhook webhook, Message originalMessage, String newMessageContent, long threadId, MessageEmbed... embeds) {
133-
WebhookUtil.mirrorMessageToWebhook(webhook, originalMessage, newMessageContent, threadId, null, List.of(embeds))
134-
.thenAccept(unused -> originalMessage.delete().queue()).exceptionally(e -> {
135-
ExceptionLogger.capture(e, WebhookUtil.class.getSimpleName());
136-
return null;
137-
});
134+
WebhookUtil.mirrorMessageToWebhook(webhook, originalMessage, newMessageContent, threadId, null, List.of(embeds)).thenAccept(unused -> originalMessage.delete().queue()).exceptionally(e -> {
135+
ExceptionLogger.capture(e, WebhookUtil.class.getSimpleName());
136+
return null;
137+
});
138138
}
139139
}

0 commit comments

Comments
 (0)