|
| 1 | +package org.togetherjava.tjbot.features.jshell.aws; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import net.dv8tion.jda.api.EmbedBuilder; |
| 6 | +import net.dv8tion.jda.api.entities.Member; |
| 7 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; |
| 8 | +import net.dv8tion.jda.api.interactions.InteractionHook; |
| 9 | +import net.dv8tion.jda.api.interactions.commands.OptionMapping; |
| 10 | +import net.dv8tion.jda.api.interactions.commands.OptionType; |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | + |
| 14 | +import org.togetherjava.tjbot.features.CommandVisibility; |
| 15 | +import org.togetherjava.tjbot.features.SlashCommandAdapter; |
| 16 | +import org.togetherjava.tjbot.features.jshell.aws.exceptions.JShellAPIException; |
| 17 | + |
| 18 | +import java.awt.*; |
| 19 | + |
| 20 | +public class JShellAWSCommand extends SlashCommandAdapter { |
| 21 | + private static final Logger logger = LogManager.getLogger(JShellAWSCommand.class); |
| 22 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
| 23 | + private static final String CODE_PARAMETER = "code"; |
| 24 | + private final JShellService jShellService; |
| 25 | + |
| 26 | + public JShellAWSCommand(JShellService jShellService) { |
| 27 | + super("jshell-aws", "Execute Java code in Discord!", CommandVisibility.GUILD); |
| 28 | + getData().addOption(OptionType.STRING, CODE_PARAMETER, "The code to execute using JShell"); |
| 29 | + this.jShellService = jShellService; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void onSlashCommand(SlashCommandInteractionEvent event) { |
| 34 | + Member member = event.getMember(); |
| 35 | + |
| 36 | + if (member == null) { |
| 37 | + event.reply("Member that executed the command is no longer available, won't execute") |
| 38 | + .queue(); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + logger.info("JShell AWS invoked by {} in channel {}", member.getAsMention(), event.getChannelId()); |
| 43 | + |
| 44 | + OptionMapping input = event.getOption(CODE_PARAMETER); |
| 45 | + |
| 46 | + if (input == null) { |
| 47 | + EmbedBuilder eb = new EmbedBuilder(); |
| 48 | + eb.setDescription(member.getAsMention() + ", you forgot to provide the code for JShell to evaluate!\nTry running the command again and make sure to select the code option"); |
| 49 | + eb.setColor(Color.ORANGE); |
| 50 | + event.replyEmbeds(eb.build()).queue(); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + event.deferReply().queue(); |
| 55 | + |
| 56 | + InteractionHook hook = event.getHook(); |
| 57 | + |
| 58 | + try { |
| 59 | + respondWithJShellOutput(hook, |
| 60 | + jShellService.sendRequest(new JShellRequest(input.getAsString())), input.getAsString()); |
| 61 | + } catch (JShellAPIException jShellAPIException) { |
| 62 | + if (jShellAPIException.getStatusCode() == 400) { |
| 63 | + logger.warn("HTTP 400 error occurred with the JShell AWS API {}", |
| 64 | + jShellAPIException.getBody()); |
| 65 | + respondWithInputError(hook, jShellAPIException.getBody()); |
| 66 | + } else if (jShellAPIException.getStatusCode() == 408) { |
| 67 | + respondWithTimeout(hook, member, input.getAsString()); |
| 68 | + } else { |
| 69 | + logger.error("HTTP {} received from JShell AWS API {}", |
| 70 | + jShellAPIException.getStatusCode(), jShellAPIException.getBody()); |
| 71 | + respondWithSevereAPIError(hook); |
| 72 | + } |
| 73 | + } catch (Exception e) { |
| 74 | + logger.error( |
| 75 | + "An error occurred while sending/receiving request from the AWS JShell API", e); |
| 76 | + respondWithSevereAPIError(hook); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static void respondWithJShellOutput(InteractionHook hook, |
| 81 | + JShellResponse response, String code) { |
| 82 | + // Extracted as fields to be compliant with Sonar |
| 83 | + final String SNIPPET_SECTION_TITLE = "## Snippets\n"; |
| 84 | + final String BACKTICK = "`"; |
| 85 | + final String NEWLINE = "\n"; |
| 86 | + final String DOUBLE_NEWLINE = "\n\n"; |
| 87 | + final String STATUS = "**Status**: "; |
| 88 | + final String OUTPUT_SECTION_TITLE = "**Output**\n"; |
| 89 | + final String JAVA_CODE_BLOCK_START = "```java\n"; |
| 90 | + final String CODE_BLOCK_END = "```\n"; |
| 91 | + final String DIAGNOSTICS_SECTION_TITLE = "**Diagnostics**\n"; |
| 92 | + final String CONSOLE_OUTPUT_SECTION_TITLE = "## Console Output\n"; |
| 93 | + final String ERROR_OUTPUT_SECTION_TITLE = "## Error Output\n"; |
| 94 | + |
| 95 | + StringBuilder sb = new StringBuilder(); |
| 96 | + sb.append(SNIPPET_SECTION_TITLE); |
| 97 | + |
| 98 | + for (JShellSnippet snippet : response.events()) { |
| 99 | + sb.append(BACKTICK); |
| 100 | + sb.append(snippet.statement()); |
| 101 | + sb.append(BACKTICK).append(DOUBLE_NEWLINE); |
| 102 | + sb.append(STATUS); |
| 103 | + sb.append(snippet.status()); |
| 104 | + sb.append(NEWLINE); |
| 105 | + |
| 106 | + if (snippet.value() != null && !snippet.value().isEmpty()) { |
| 107 | + sb.append(OUTPUT_SECTION_TITLE); |
| 108 | + sb.append(JAVA_CODE_BLOCK_START); |
| 109 | + sb.append(snippet.value()); |
| 110 | + sb.append(CODE_BLOCK_END); |
| 111 | + } |
| 112 | + |
| 113 | + if (!snippet.diagnostics().isEmpty()) { |
| 114 | + sb.append(DIAGNOSTICS_SECTION_TITLE); |
| 115 | + for (String diagnostic : snippet.diagnostics()) { |
| 116 | + sb.append(BACKTICK).append(diagnostic).append(BACKTICK).append(NEWLINE); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (response.outputStream() != null && !response.outputStream().isEmpty()) { |
| 122 | + sb.append(CONSOLE_OUTPUT_SECTION_TITLE); |
| 123 | + sb.append(JAVA_CODE_BLOCK_START); |
| 124 | + sb.append(response.outputStream()); |
| 125 | + sb.append(CODE_BLOCK_END); |
| 126 | + } |
| 127 | + |
| 128 | + if (response.errorStream() != null && !response.errorStream().isEmpty()) { |
| 129 | + sb.append(ERROR_OUTPUT_SECTION_TITLE); |
| 130 | + sb.append(JAVA_CODE_BLOCK_START); |
| 131 | + sb.append(response.errorStream()); |
| 132 | + sb.append(CODE_BLOCK_END); |
| 133 | + } |
| 134 | + |
| 135 | + EmbedBuilder eb = new EmbedBuilder(); |
| 136 | + eb.setFooter("Code that was executed:\n" + code); |
| 137 | + if (sb.length() > 4000) { |
| 138 | + eb.setDescription(sb.substring(0, 500) + "\n... truncated " + (sb.length() - 500) + " characters\n```"); |
| 139 | + } else { |
| 140 | + eb.setDescription(sb.toString()); |
| 141 | + } |
| 142 | + eb.setColor(Color.GREEN); |
| 143 | + |
| 144 | + hook.editOriginalEmbeds(eb.build()).queue(); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + private static void respondWithInputError(InteractionHook hook, String response) { |
| 149 | + JShellErrorResponse errorResponse; |
| 150 | + try { |
| 151 | + errorResponse = OBJECT_MAPPER.readValue(response, JShellErrorResponse.class); |
| 152 | + } catch (JsonProcessingException e) { |
| 153 | + errorResponse = new JShellErrorResponse( |
| 154 | + "There was a problem with the input you provided, please check and try again"); |
| 155 | + } |
| 156 | + EmbedBuilder eb = new EmbedBuilder(); |
| 157 | + eb.setDescription(errorResponse.error()); |
| 158 | + eb.setColor(Color.ORANGE); |
| 159 | + hook.editOriginalEmbeds(eb.build()).queue(); |
| 160 | + } |
| 161 | + |
| 162 | + private static void respondWithTimeout(InteractionHook hook, Member member, String code) { |
| 163 | + EmbedBuilder eb = new EmbedBuilder(); |
| 164 | + eb.setDescription(member.getAsMention() + " the code you provided took too long and the request has timed out! Consider tweaking your code to run a little faster."); |
| 165 | + eb.setColor(Color.ORANGE); |
| 166 | + eb.setFooter("Code that was executed:\n" + code); |
| 167 | + hook.editOriginalEmbeds(eb.build()).queue(); |
| 168 | + } |
| 169 | + |
| 170 | + private static void respondWithSevereAPIError(InteractionHook hook) { |
| 171 | + EmbedBuilder eb = new EmbedBuilder(); |
| 172 | + eb.setDescription("An internal error occurred, please try again later"); |
| 173 | + eb.setColor(Color.RED); |
| 174 | + hook.editOriginalEmbeds(eb.build()).queue(); |
| 175 | + } |
| 176 | +} |
0 commit comments