-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feature/jshell from fork (#869) * JShell feature added * Added option to get the snippets of another user * Refactored JShell eval into its own class * Changed RateLimiter so it's a global limiter and not a per user limiter + merged user and oneOffSession together * Added context action for running java code * Fixing sonar wranings for jshell * Added startupt scripts for jshell * Formating jshell classes * JShell javadoc and many minor code improvements * JShell more minor code improvements * Other minor code changes * JShell now allows wrong uri and dead server, will give correct response to the user and will log them as warns * Forgot one exception javadoc for jshell feature * JShell refactored snippets send conditions
- Loading branch information
Showing
26 changed files
with
1,229 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
application/src/main/java/org/togetherjava/tjbot/config/JShellConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.togetherjava.tjbot.config; | ||
|
||
|
||
import org.togetherjava.tjbot.features.utils.RateLimiter; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* JShell config. | ||
* | ||
* @param baseUrl the base url of the JShell REST API | ||
* @param rateLimitWindowSeconds the number of seconds of the {@link RateLimiter rate limiter} for | ||
* jshell commands and code actions | ||
* @param rateLimitRequestsInWindow the number of requests of the {@link RateLimiter rate limiter} | ||
* for jshell commands and code actions | ||
*/ | ||
public record JShellConfig(String baseUrl, int rateLimitWindowSeconds, | ||
int rateLimitRequestsInWindow) { | ||
/** | ||
* Creates a JShell config. | ||
* | ||
* @param baseUrl the base url of the JShell REST API, must be not null | ||
* @param rateLimitWindowSeconds the number of seconds of the {@link RateLimiter rate limiter} | ||
* for jshell commands and code actions, must be higher than 0 | ||
* @param rateLimitRequestsInWindow the number of requests of the {@link RateLimiter rate | ||
* limiter} for jshell commands and code actions, must be higher than 0 | ||
*/ | ||
public JShellConfig { | ||
Objects.requireNonNull(baseUrl); | ||
if (rateLimitWindowSeconds < 0) { | ||
throw new IllegalArgumentException( | ||
"Illegal rateLimitWindowSeconds : " + rateLimitWindowSeconds); | ||
} | ||
if (rateLimitRequestsInWindow < 0) { | ||
throw new IllegalArgumentException( | ||
"Illegal rateLimitRequestsInWindow : " + rateLimitRequestsInWindow); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
application/src/main/java/org/togetherjava/tjbot/features/code/EvalCodeCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.togetherjava.tjbot.features.code; | ||
|
||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.MessageEmbed; | ||
|
||
import org.togetherjava.tjbot.features.jshell.JShellEval; | ||
import org.togetherjava.tjbot.features.utils.CodeFence; | ||
import org.togetherjava.tjbot.features.utils.Colors; | ||
import org.togetherjava.tjbot.features.utils.ConnectionFailedException; | ||
import org.togetherjava.tjbot.features.utils.RequestFailedException; | ||
|
||
/** | ||
* Evaluates the given code with jshell. | ||
* <p> | ||
* It will not work of the code isn't valid java or jshell compatible code. | ||
*/ | ||
final class EvalCodeCommand implements CodeAction { | ||
private final JShellEval jshellEval; | ||
|
||
EvalCodeCommand(JShellEval jshellEval) { | ||
this.jshellEval = jshellEval; | ||
} | ||
|
||
@Override | ||
public String getLabel() { | ||
return "Run code"; | ||
} | ||
|
||
@Override | ||
public MessageEmbed apply(CodeFence codeFence) { | ||
if (codeFence.code().isEmpty()) { | ||
return new EmbedBuilder().setColor(Colors.ERROR_COLOR) | ||
.setDescription("There is nothing to evaluate") | ||
.build(); | ||
} | ||
try { | ||
return jshellEval.evaluateAndRespond(null, codeFence.code(), false, false); | ||
} catch (RequestFailedException | ConnectionFailedException e) { | ||
return new EmbedBuilder().setColor(Colors.ERROR_COLOR) | ||
.setDescription("Request failed: " + e.getMessage()) | ||
.build(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.