Skip to content

Commit 5081631

Browse files
committed
improved "how to ask" explanation
1 parent 51cf4cf commit 5081631

3 files changed

Lines changed: 101 additions & 54 deletions

File tree

application/src/main/java/org/togetherjava/tjbot/commands/help/AskCommand.java

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
1010
import net.dv8tion.jda.api.requests.ErrorResponse;
1111
import net.dv8tion.jda.api.requests.RestAction;
12+
import net.dv8tion.jda.api.requests.restaction.MessageAction;
1213
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
1314
import org.jetbrains.annotations.NotNull;
15+
import org.jetbrains.annotations.Nullable;
1416
import org.slf4j.Logger;
1517
import org.slf4j.LoggerFactory;
1618
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
@@ -19,6 +21,8 @@
1921
import org.togetherjava.tjbot.config.HelpSystemConfig;
2022

2123
import java.awt.Color;
24+
import java.io.InputStream;
25+
import java.util.List;
2226
import java.util.Optional;
2327
import java.util.function.Predicate;
2428
import java.util.regex.Pattern;
@@ -31,6 +35,7 @@ public final class AskCommand extends SlashCommandAdapter {
3135
private static final String CATEGORY_OPTION = "category";
3236
// FIXME This constant is duplicated quite a lot...
3337
private static final Color AMBIENT_COLOR = Color.decode("#CCCC00");
38+
private static final String CODE_SYNTAX_EXAMPLE_PATH = "codeSyntaxExample.png";
3439

3540
private final Predicate<String> isStagingChannelName;
3641
private final String categoryRoleSuffix;
@@ -94,7 +99,7 @@ private boolean handleIsStagingChannel(@NotNull IReplyCallback event) {
9499
@NotNull String category) {
95100
return sendInitialMessage(event.getGuild(), threadChannel, author, title, category)
96101
.flatMap(any -> notifyUser(event, threadChannel))
97-
.flatMap(any -> sendExplanationMessage(threadChannel, author));
102+
.flatMap(any -> sendExplanationMessage(threadChannel));
98103
}
99104

100105
private RestAction<Message> sendInitialMessage(@NotNull Guild guild,
@@ -118,31 +123,37 @@ private RestAction<Message> sendInitialMessage(@NotNull Guild guild,
118123
.flatMap(message -> message.editMessage(contentWithRole));
119124
}
120125

121-
private static RestAction<Message> sendExplanationMessage(@NotNull ThreadChannel threadChannel,
122-
@NotNull Member author) {
123-
MessageEmbed embed = new EmbedBuilder().setColor(AMBIENT_COLOR)
124-
.setDescription(
125-
"""
126-
%s, while you are waiting for getting help, here are some tips to improve your experience:
127-
128-
- Code is much easier to read if posted with **syntax highlighting** and proper formatting:
129-
```java
130-
\\`\\`\\`java
131-
// 3 backticks, java, newline, code, newline, 3 backticks
132-
System.out.println("This is how you format code in Discord");
133-
\\`\\`\\`
134-
```
135-
136-
- If your code is **long**, or you have **multiple files** to share, consider posting it on sites \
137-
like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.
138-
139-
- If nobody is calling back, that usually means that your question was **not well asked** and \
140-
hence nobody feels confident enough answering. Try to use your time to elaborate, \
141-
**provide details**, context, more code, examples and maybe some screenshots. \
142-
With enough info, someone knows the answer for sure ."""
143-
.formatted(author.getAsMention()))
144-
.build();
145-
return threadChannel.sendMessageEmbeds(embed);
126+
private static RestAction<Message> sendExplanationMessage(
127+
@NotNull MessageChannel threadChannel) {
128+
boolean useCodeSyntaxExampleImage = true;
129+
InputStream codeSyntaxExampleData =
130+
AskCommand.class.getResourceAsStream("/" + CODE_SYNTAX_EXAMPLE_PATH);
131+
if (codeSyntaxExampleData == null) {
132+
useCodeSyntaxExampleImage = false;
133+
}
134+
135+
String message =
136+
"While you are waiting for getting help, here are some tips to improve your experience:";
137+
138+
List<MessageEmbed> embeds = List.of(embedWith(
139+
"Code is much easier to read if posted with **syntax highlighting** and proper formatting.",
140+
useCodeSyntaxExampleImage ? "attachment://" + CODE_SYNTAX_EXAMPLE_PATH : null),
141+
embedWith(
142+
"""
143+
If your code is **long**, or you have **multiple files** to share, consider posting it on sites \
144+
like https://pastebin.com/ and share the link instead, that is easier to browse for helpers."""),
145+
embedWith(
146+
"""
147+
If nobody is calling back, that usually means that your question was **not well asked** and \
148+
hence nobody feels confident enough answering. Try to use your time to elaborate, \
149+
**provide details**, context, more code, examples and maybe some screenshots. \
150+
With enough info, someone knows the answer for sure ."""));
151+
152+
MessageAction action = threadChannel.sendMessage(message);
153+
if (useCodeSyntaxExampleImage) {
154+
action = action.addFile(codeSyntaxExampleData, CODE_SYNTAX_EXAMPLE_PATH);
155+
}
156+
return action.setEmbeds(embeds);
146157
}
147158

148159
private @NotNull Optional<Role> handleFindRoleForCategory(@NotNull String category,
@@ -180,4 +191,18 @@ private static void handleFailure(@NotNull Throwable exception, @NotNull IReplyC
180191

181192
logger.error("Attempted to create a help thread, but failed", exception);
182193
}
194+
195+
private static @NotNull MessageEmbed embedWith(@NotNull CharSequence message) {
196+
// FIXME Duplication with ImplicitAskListener
197+
return embedWith(message, null);
198+
}
199+
200+
private static @NotNull MessageEmbed embedWith(@NotNull CharSequence message,
201+
@Nullable String imageUrl) {
202+
// FIXME Duplication with ImplicitAskListener
203+
return new EmbedBuilder().setColor(AMBIENT_COLOR)
204+
.setDescription(message)
205+
.setImage(imageUrl)
206+
.build();
207+
}
183208
}

application/src/main/java/org/togetherjava/tjbot/commands/help/ImplicitAskListener.java

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
import net.dv8tion.jda.api.requests.RestAction;
1212
import net.dv8tion.jda.api.requests.restaction.MessageAction;
1313
import org.jetbrains.annotations.NotNull;
14+
import org.jetbrains.annotations.Nullable;
1415
import org.slf4j.Logger;
1516
import org.slf4j.LoggerFactory;
1617
import org.togetherjava.tjbot.commands.MessageReceiverAdapter;
1718
import org.togetherjava.tjbot.config.Config;
1819

1920
import java.awt.Color;
21+
import java.io.InputStream;
2022
import java.time.Instant;
2123
import java.time.temporal.ChronoUnit;
24+
import java.util.List;
2225
import java.util.Optional;
2326
import java.util.concurrent.TimeUnit;
2427
import java.util.regex.Pattern;
@@ -30,6 +33,7 @@ public final class ImplicitAskListener extends MessageReceiverAdapter {
3033
private static final int TITLE_MAX_LENGTH = 30;
3134
// FIXME This constant is duplicated quite a lot...
3235
private static final Color AMBIENT_COLOR = Color.decode("#CCCC00");
36+
private static final String CODE_SYNTAX_EXAMPLE_PATH = "codeSyntaxExample.png";
3337

3438
private static final int COOLDOWN_DURATION_VALUE = 15;
3539
private static final ChronoUnit COOLDOWN_DURATION_UNIT = ChronoUnit.SECONDS;
@@ -131,7 +135,7 @@ private Optional<HelpThread> getLastHelpThreadIfOnCooldown(long userId) {
131135
return sendInitialMessage(threadChannel, message, title)
132136
.flatMap(any -> notifyUser(threadChannel, message))
133137
.flatMap(any -> message.delete())
134-
.flatMap(any -> sendExplanationMessage(threadChannel, author));
138+
.flatMap(any -> sendExplanationMessage(threadChannel));
135139
}
136140

137141
private static @NotNull RestAction<Void> inviteUsersToThread(
@@ -172,34 +176,38 @@ private Optional<HelpThread> getLastHelpThreadIfOnCooldown(long userId) {
172176
threadChannel.getAsMention()));
173177
}
174178

175-
private static RestAction<Message> sendExplanationMessage(@NotNull ThreadChannel threadChannel,
176-
@NotNull Member author) {
177-
// FIXME Code duplication with AskCommand
178-
MessageEmbed embed = new EmbedBuilder().setColor(AMBIENT_COLOR)
179-
.setDescription(
180-
"""
181-
%s, while you are waiting for getting help, here are some tips to improve your experience:
182-
183-
- Code is much easier to read if posted with **syntax highlighting** and proper formatting:
184-
```java
185-
\\`\\`\\`java
186-
// 3 backticks, java, newline, code, newline, 3 backticks
187-
System.out.println("This is how you format code in Discord");
188-
\\`\\`\\`
189-
```
190-
191-
- If your code is **long**, or you have **multiple files** to share, consider posting it on sites \
192-
like https://pastebin.com/ and share the link instead, that is easier to browse for helpers.
193-
194-
- If nobody is calling back, that usually means that your question was **not well asked** and \
195-
hence nobody feels confident enough answering. Try to use your time to elaborate, \
196-
**provide details**, context, more code, examples and maybe some screenshots. \
197-
With enough info, someone knows the answer for sure ."""
198-
.formatted(author.getAsMention()))
199-
.build();
200-
return threadChannel.sendMessageEmbeds(embed);
201-
}
179+
private static RestAction<Message> sendExplanationMessage(
180+
@NotNull MessageChannel threadChannel) {
181+
boolean useCodeSyntaxExampleImage = true;
182+
InputStream codeSyntaxExampleData =
183+
AskCommand.class.getResourceAsStream("/" + CODE_SYNTAX_EXAMPLE_PATH);
184+
if (codeSyntaxExampleData == null) {
185+
useCodeSyntaxExampleImage = false;
186+
}
202187

188+
String message =
189+
"While you are waiting for getting help, here are some tips to improve your experience:";
190+
191+
List<MessageEmbed> embeds = List.of(embedWith(
192+
"Code is much easier to read if posted with **syntax highlighting** and proper formatting.",
193+
useCodeSyntaxExampleImage ? "attachment://" + CODE_SYNTAX_EXAMPLE_PATH : null),
194+
embedWith(
195+
"""
196+
If your code is **long**, or you have **multiple files** to share, consider posting it on sites \
197+
like https://pastebin.com/ and share the link instead, that is easier to browse for helpers."""),
198+
embedWith(
199+
"""
200+
If nobody is calling back, that usually means that your question was **not well asked** and \
201+
hence nobody feels confident enough answering. Try to use your time to elaborate, \
202+
**provide details**, context, more code, examples and maybe some screenshots. \
203+
With enough info, someone knows the answer for sure ."""));
204+
205+
MessageAction action = threadChannel.sendMessage(message);
206+
if (useCodeSyntaxExampleImage) {
207+
action = action.addFile(codeSyntaxExampleData, CODE_SYNTAX_EXAMPLE_PATH);
208+
}
209+
return action.setEmbeds(embeds);
210+
}
203211

204212
private static void handleFailure(@NotNull Throwable exception) {
205213
if (exception instanceof ErrorResponseException responseException) {
@@ -213,6 +221,20 @@ private static void handleFailure(@NotNull Throwable exception) {
213221
logger.error("Attempted to create a help thread, but failed", exception);
214222
}
215223

224+
private static @NotNull MessageEmbed embedWith(@NotNull CharSequence message) {
225+
// FIXME Duplication with AskCommand
226+
return embedWith(message, null);
227+
}
228+
229+
private static @NotNull MessageEmbed embedWith(@NotNull CharSequence message,
230+
@Nullable String imageUrl) {
231+
// FIXME Duplication with AskCommand
232+
return new EmbedBuilder().setColor(AMBIENT_COLOR)
233+
.setDescription(message)
234+
.setImage(imageUrl)
235+
.build();
236+
}
237+
216238
private record HelpThread(long channelId, long authorId, @NotNull Instant creationTime) {
217239
}
218240
}
7.31 KB
Loading

0 commit comments

Comments
 (0)