1111import net .dv8tion .jda .api .requests .RestAction ;
1212import net .dv8tion .jda .api .requests .restaction .MessageAction ;
1313import org .jetbrains .annotations .NotNull ;
14+ import org .jetbrains .annotations .Nullable ;
1415import org .slf4j .Logger ;
1516import org .slf4j .LoggerFactory ;
1617import org .togetherjava .tjbot .commands .MessageReceiverAdapter ;
1718import org .togetherjava .tjbot .config .Config ;
1819
1920import java .awt .Color ;
21+ import java .io .InputStream ;
2022import java .time .Instant ;
2123import java .time .temporal .ChronoUnit ;
24+ import java .util .List ;
2225import java .util .Optional ;
2326import java .util .concurrent .TimeUnit ;
2427import 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}
0 commit comments