Skip to content

Commit

Permalink
custom eval engine (jagrosh#1530)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagrosh authored May 10, 2024
1 parent 0afb3db commit 6f12c33
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/BotConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class BotConfig

private Path path = null;
private String token, prefix, altprefix, helpWord, playlistsFolder, logLevel,
successEmoji, warningEmoji, errorEmoji, loadingEmoji, searchingEmoji;
successEmoji, warningEmoji, errorEmoji, loadingEmoji, searchingEmoji,
evalEngine;
private boolean stayInChannel, songInGame, npImages, updatealerts, useEval, dbots;
private long owner, maxSeconds, aloneTimeUntilStop;
private double skipratio;
Expand Down Expand Up @@ -88,6 +89,7 @@ public void load()
updatealerts = config.getBoolean("updatealerts");
logLevel = config.getString("loglevel");
useEval = config.getBoolean("eval");
evalEngine = config.getString("evalengine");
maxSeconds = config.getLong("maxtime");
aloneTimeUntilStop = config.getLong("alonetimeuntilstop");
playlistsFolder = config.getString("playlistsfolder");
Expand Down Expand Up @@ -324,6 +326,11 @@ public boolean useEval()
return useEval;
}

public String getEvalEngine()
{
return evalEngine;
}

public boolean useNPImages()
{
return npImages;
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/jagrosh/jmusicbot/commands/owner/EvalCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,28 @@
public class EvalCmd extends OwnerCommand
{
private final Bot bot;
private final String engine;

public EvalCmd(Bot bot)
{
this.bot = bot;
this.name = "eval";
this.help = "evaluates nashorn code";
this.aliases = bot.getConfig().getAliases(this.name);
this.engine = bot.getConfig().getEvalEngine();
this.guildOnly = false;
}

@Override
protected void execute(CommandEvent event)
{
ScriptEngine se = new ScriptEngineManager().getEngineByName("Nashorn");
ScriptEngine se = new ScriptEngineManager().getEngineByName(engine);
if(se == null)
{
event.replyError("The eval engine provided in the config (`"+engine+"`) doesn't exist. This could be due to an invalid "
+ "engine name, or the engine not existing in your version of java (`"+System.getProperty("java.version")+"`).");
return;
}
se.put("bot", bot);
se.put("event", event);
se.put("jda", event.getJDA());
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ transforms = {}
// IF SOMEONE ASKS YOU TO ENABLE THIS, THERE IS AN 11/10 CHANCE THEY ARE TRYING TO SCAM YOU

eval=false
evalengine="Nashorn"


/// END OF JMUSICBOT CONFIG ///

0 comments on commit 6f12c33

Please sign in to comment.