11package net .discordjug .javabot .systems .user_commands .format_code ;
22
3- import net .dv8tion .jda .api .interactions .InteractionHook ;
43import xyz .dynxsty .dih4jda .interactions .commands .application .SlashCommand ;
54import net .discordjug .javabot .util .*;
65import net .dv8tion .jda .api .components .actionrow .ActionRow ;
1615import org .jetbrains .annotations .NotNull ;
1716
1817import java .util .Collections ;
19- import java .util .List ;
2018
2119/**
2220 * <h3>This class represents the /format-code command.</h3>
@@ -30,25 +28,7 @@ public FormatCodeCommand() {
3028 .setContexts (InteractionContextType .GUILD )
3129 .addOptions (
3230 new OptionData (OptionType .STRING , "message-id" , "Message to be formatted, last message used if left blank." , false ),
33- new OptionData (OptionType .STRING , "format" , "The language used to format the code, defaults to Java if left blank." , false )
34- .addChoice ("C" , "c" )
35- .addChoice ("C#" , "csharp" )
36- .addChoice ("C++" , "cpp" )
37- .addChoice ("CSS" , "css" )
38- .addChoice ("D" , "d" )
39- .addChoice ("Go" , "go" )
40- .addChoice ("HTML" , "html" )
41- .addChoice ("Java" , "java" )
42- .addChoice ("JavaScript" , "js" )
43- .addChoice ("Kotlin" , "kotlin" )
44- .addChoice ("PHP" , "php" )
45- .addChoice ("Python" , "python" )
46- .addChoice ("Ruby" , "ruby" )
47- .addChoice ("Rust" , "rust" )
48- .addChoice ("SQL" , "sql" )
49- .addChoice ("Swift" , "swift" )
50- .addChoice ("TypeScript" , "typescript" )
51- .addChoice ("XML" , "xml" ),
31+ formatOption (),
5232 new OptionData (OptionType .STRING ,"auto-indent" ,"The type of indentation applied to the message, does not automatically indent if left blank." ,false )
5333 .addChoice ("Four Spaces" ,"FOUR_SPACES" )
5434 .addChoice ("Two Spaces" ,"TWO_SPACES" )
@@ -57,6 +37,29 @@ public FormatCodeCommand() {
5737 );
5838 }
5939
40+ /**
41+ * Builds the {@code format} option, generating one choice per {@link Language} (excluding
42+ * {@link Language#UNKNOWN}) so the enum stays the single source of truth for the language list.
43+ *
44+ * @return the configured {@code format} option
45+ */
46+ private static OptionData formatOption () {
47+ OptionData option = new OptionData (OptionType .STRING , "format" , "The language used to format the code, defaults to Java if left blank." , false );
48+ for (Language language : Language .values ()) {
49+ if (language != Language .UNKNOWN ) { // UNKNOWN is the fallback, not a real choice
50+ option .addChoice (language .getDisplayName (), language .name ()); // value = enum name so valueOf() reverses it
51+ }
52+ }
53+ return option ;
54+ }
55+
56+ /**
57+ * Builds the action row placed on the file-upload message: a delete button and a "View Original" link.
58+ *
59+ * @param target the original message linked by the "View Original" button
60+ * @param requesterId the id of the user permitted to delete the message
61+ * @return an action row containing the delete and "View Original" buttons
62+ */
6063 @ Contract ("_ -> new" )
6164 static @ NotNull ActionRow buildActionRow (@ NotNull Message target , long requesterId ) {
6265 return ActionRow .of (InteractionUtils .createDeleteButton (requesterId ),
@@ -66,9 +69,9 @@ public FormatCodeCommand() {
6669 @ Override
6770 public void execute (@ NotNull SlashCommandInteractionEvent event ) {
6871 OptionMapping idOption = event .getOption ("message-id" );
69- String format = event .getOption ("format" , "java" , OptionMapping :: getAsString );
72+ Language language = event .getOption ("format" , Language . JAVA , o -> Language . fromString ( o . getAsString ()) );
7073 String indentation = event .getOption ("auto-indent" ,"NULL" ,OptionMapping ::getAsString );
71- event . deferReply (). queue ();
74+
7275 if (idOption == null ) {
7376 event .getChannel ().getHistory ()
7477 .retrievePast (10 )
@@ -78,7 +81,7 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
7881 .filter (m -> !m .getAuthor ().isBot ()).findFirst ()
7982 .orElse (null );
8083 if (target != null ) {
81- sendFormattedCode (event , target , format , indentation );
84+ sendFormattedCode (event , target , language , indentation );
8285 } else {
8386 Responses .error (event .getHook (), "Could not find message; please specify a message id." ).queue ();
8487 }
@@ -90,38 +93,18 @@ public void execute(@NotNull SlashCommandInteractionEvent event) {
9093 }
9194 long messageId = idOption .getAsLong ();
9295 event .getChannel ().retrieveMessageById (messageId ).queue (
93- target -> sendFormattedCode (event , target , format , indentation ),
96+ target -> sendFormattedCode (event , target , language , indentation ),
9497 e -> Responses .error (event .getHook (), "Could not retrieve message with id: " + messageId ).queue ());
9598 }
9699 }
97100
98- private void sendFormattedCode (SlashCommandInteractionEvent event , Message target , String format , String indentation ) {
101+ private void sendFormattedCode (SlashCommandInteractionEvent event , Message target , Language language , String indentation ) {
99102 String content = IndentationHelper .formatIndentation (
100103 StringUtils .standardSanitizer ().compute (target .getContentRaw ()),
101104 IndentationHelper .IndentationType .valueOf (indentation ));
102105
103- if (content .isBlank ()) {
104- Responses .error (event .getHook (), "There is no code to format in that message." ).queue ();
105- return ;
106- }
107-
108- Code code = new Code (Language .fromString (format ), content );
109- sendChunksInOrder (event .getHook (), code .toDiscordMessages (), 0 );
110- }
106+ Code code = new Code (language ,content );
111107
112- private void sendChunksInOrder (InteractionHook hook , List <String > messages , int index ) {
113- if (index >= messages .size ()) {
114- return ;
115- }
116- var action = hook .sendMessage (messages .get (index )).setAllowedMentions (List .of ());
117-
118- action .queue (
119- success -> sendChunksInOrder (hook , messages , index + 1 ),
120- error -> {
121- ExceptionLogger .capture (error , getClass ().getSimpleName ());
122- Responses .error (hook , "The message could not be converted into a formatted code block." )
123- .queue ();
124- }
125- );
108+ FormatCodeDispatcher .sendCode (code , event , target );
126109 }
127- }
110+ }
0 commit comments