22
33import xyz .dynxsty .dih4jda .interactions .commands .application .SlashCommand ;
44import net .discordjug .javabot .util .*;
5- import net .dv8tion .jda .api .components .actionrow .ActionRow ;
6- import net .dv8tion .jda .api .components .buttons .Button ;
75import net .dv8tion .jda .api .entities .Message ;
86import net .dv8tion .jda .api .events .interaction .command .SlashCommandInteractionEvent ;
97import net .dv8tion .jda .api .interactions .InteractionContextType ;
108import net .dv8tion .jda .api .interactions .commands .OptionMapping ;
119import net .dv8tion .jda .api .interactions .commands .OptionType ;
1210import net .dv8tion .jda .api .interactions .commands .build .Commands ;
1311import net .dv8tion .jda .api .interactions .commands .build .OptionData ;
14- import org .jetbrains .annotations .Contract ;
15- import org .jetbrains .annotations .NotNull ;
1612
17- import java .util .Collections ;
18- import java .util .List ;
13+ import org .jetbrains .annotations .NotNull ;
1914
2015/**
2116 * <h3>This class represents the /format-code command.</h3>
@@ -29,25 +24,7 @@ public FormatCodeCommand() {
2924 .setContexts (InteractionContextType .GUILD )
3025 .addOptions (
3126 new OptionData (OptionType .STRING , "message-id" , "Message to be formatted, last message used if left blank." , false ),
32- new OptionData (OptionType .STRING , "format" , "The language used to format the code, defaults to Java if left blank." , false )
33- .addChoice ("C" , "c" )
34- .addChoice ("C#" , "csharp" )
35- .addChoice ("C++" , "cpp" )
36- .addChoice ("CSS" , "css" )
37- .addChoice ("D" , "d" )
38- .addChoice ("Go" , "go" )
39- .addChoice ("HTML" , "html" )
40- .addChoice ("Java" , "java" )
41- .addChoice ("JavaScript" , "js" )
42- .addChoice ("Kotlin" , "kotlin" )
43- .addChoice ("PHP" , "php" )
44- .addChoice ("Python" , "python" )
45- .addChoice ("Ruby" , "ruby" )
46- .addChoice ("Rust" , "rust" )
47- .addChoice ("SQL" , "sql" )
48- .addChoice ("Swift" , "swift" )
49- .addChoice ("TypeScript" , "typescript" )
50- .addChoice ("XML" , "xml" ),
27+ formatOption (),
5128 new OptionData (OptionType .STRING ,"auto-indent" ,"The type of indentation applied to the message, does not automatically indent if left blank." ,false )
5229 .addChoice ("Four Spaces" ,"FOUR_SPACES" )
5330 .addChoice ("Two Spaces" ,"TWO_SPACES" )
@@ -56,47 +33,66 @@ public FormatCodeCommand() {
5633 );
5734 }
5835
59- @ Contract ("_ -> new" )
60- static @ NotNull ActionRow buildActionRow (@ NotNull Message target , long requesterId ) {
61- return ActionRow .of (InteractionUtils .createDeleteButton (requesterId ),
62- Button .link (target .getJumpUrl (), "View Original" ));
36+ /**
37+ * Builds the {@code format} option, generating one choice per {@link Language} (excluding
38+ * {@link Language#UNKNOWN}) so the enum stays the single source of truth for the language list.
39+ *
40+ * @return the configured {@code format} option
41+ */
42+ private static OptionData formatOption () {
43+ OptionData option = new OptionData (OptionType .STRING , "format" , "The language used to format the code, defaults to Java if left blank." , false );
44+ for (Language language : Language .values ()) {
45+ if (language != Language .UNKNOWN ) { // UNKNOWN is the fallback, not a real choice
46+ option .addChoice (language .getDisplayName (), language .name ()); // value = enum name so valueOf() reverses it
47+ }
48+ }
49+ return option ;
6350 }
6451
6552 @ Override
6653 public void execute (@ NotNull SlashCommandInteractionEvent event ) {
6754 OptionMapping idOption = event .getOption ("message-id" );
68- String format = event .getOption ("format" , "java" , OptionMapping :: getAsString );
55+ Language language = event .getOption ("format" , Language . JAVA , o -> Language . fromString ( o . getAsString ()) );
6956 String indentation = event .getOption ("auto-indent" ,"NULL" ,OptionMapping ::getAsString );
70- event . deferReply (). queue ();
57+
7158 if (idOption == null ) {
72- event .getChannel ().getHistory ()
73- .retrievePast (10 )
74- .queue (messages -> {
75- Collections .reverse (messages );
76- Message target = messages .stream ()
77- .filter (m -> !m .getAuthor ().isBot ()).findFirst ()
78- .orElse (null );
79- if (target != null ) {
80- event .getHook ().sendMessageFormat ("```%s\n %s\n ```" , format , IndentationHelper .formatIndentation (StringUtils .standardSanitizer ().compute (target .getContentRaw ()),IndentationHelper .IndentationType .valueOf (indentation )))
81- .setAllowedMentions (List .of ())
82- .setComponents (buildActionRow (target , event .getUser ().getIdLong ()))
83- .queue ();
84- } else {
85- Responses .error (event .getHook (), "Could not find message; please specify a message id." ).queue ();
86- }
87- });
59+ event .deferReply ().queue (_ -> {
60+ event .getChannel ().getHistory ()
61+ .retrievePast (10 )
62+ .queue (messages -> {
63+ Message target = messages .stream ()
64+ .filter (m -> !m .getAuthor ().isBot ()).findFirst ()
65+ .orElse (null );
66+ if (target != null ) {
67+ sendFormattedCode (event , target , language , indentation );
68+ } else {
69+ Responses .errorWithTitle (event .getHook (), "Message Not Found" , "No recent user message could be found. Please specify a message ID." )
70+ .queue ();
71+ }
72+ });
73+ });
8874 } else {
8975 if (Checks .isInvalidLongInput (idOption )) {
90- Responses .error (event .getHook (), "Please provide a valid message id!" ).queue ();
76+ Responses .errorWithTitle (event , "Invalid Message ID" , "Please provide a valid Discord message ID." )
77+ .queue ();
9178 return ;
9279 }
9380 long messageId = idOption .getAsLong ();
94- event .getChannel ().retrieveMessageById (messageId ).queue (
95- target -> event .getHook ().sendMessageFormat ("```%s\n %s\n ```" , format , IndentationHelper .formatIndentation (StringUtils .standardSanitizer ().compute (target .getContentRaw ()), IndentationHelper .IndentationType .valueOf (indentation )))
96- .setAllowedMentions (List .of ())
97- .setComponents (buildActionRow (target , event .getUser ().getIdLong ()))
98- .queue (),
99- e -> Responses .error (event .getHook (), "Could not retrieve message with id: " + messageId ).queue ());
81+ event .deferReply ().queue (_ -> {
82+ event .getChannel ().retrieveMessageById (messageId ).queue (
83+ target -> sendFormattedCode (event , target , language , indentation ),
84+ error -> Responses .errorWithTitle (event .getHook (), "Message Not Found" , "Could not retrieve the message with ID `" + messageId + "`. Make sure the message exists and is accessible." ).queue ());
85+ });
10086 }
10187 }
88+
89+ private void sendFormattedCode (SlashCommandInteractionEvent event , Message target , Language language , String indentation ) {
90+ String content = IndentationHelper .formatIndentation (
91+ StringUtils .standardSanitizer ().compute (target .getContentRaw ()),
92+ IndentationHelper .IndentationType .valueOf (indentation ));
93+
94+ Code code = new Code (language ,content );
95+
96+ FormatCodeDispatcher .sendCode (code , event , target );
97+ }
10298}
0 commit comments