Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f8a264
commit d5d0552
Showing
2 changed files
with
85 additions
and
0 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
84 changes: 84 additions & 0 deletions
84
src/main/java/net/dv8tion/discord/commands/EvalCommand.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,84 @@ | ||
package net.dv8tion.discord.commands; | ||
|
||
import net.dv8tion.discord.Permissions; | ||
import net.dv8tion.jda.events.message.MessageReceivedEvent; | ||
|
||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
import javax.script.ScriptException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class EvalCommand extends Command | ||
{ | ||
private ScriptEngine engine; | ||
|
||
public EvalCommand() | ||
{ | ||
engine = new ScriptEngineManager().getEngineByName("nashorn"); | ||
try | ||
{ | ||
engine.eval("var imports = new JavaImporter(java.io, java.lang, java.util);"); | ||
} | ||
catch (ScriptException e) | ||
{ | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onCommand(MessageReceivedEvent e, String[] args) | ||
{ | ||
if (!Permissions.getPermissions().isOp(e.getAuthor())) | ||
{ | ||
sendMessage(e, "Sorry, this command is OP only!"); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
engine.put("event", e); | ||
engine.put("channel", e.getChannel()); | ||
engine.put("args", args); | ||
engine.put("api", e.getJDA()); | ||
Object out = engine.eval( | ||
"(function() {" + | ||
"with (imports) {" + | ||
e.getMessage().getContent().substring(args[0].length()) + | ||
"}" + | ||
"})();"); | ||
sendMessage(e, out == null ? "Executed without error." : out.toString()); | ||
} | ||
catch (ScriptException e1) | ||
{ | ||
sendMessage(e, e1.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> getAliases() | ||
{ | ||
return Collections.singletonList(".eval"); | ||
} | ||
|
||
@Override | ||
public String getDescription() | ||
{ | ||
return "Takes Java or Javascript and executes it."; | ||
} | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return "Evaluate"; | ||
} | ||
|
||
@Override | ||
public List<String> getUsageInstructions() | ||
{ | ||
return Collections.singletonList( | ||
".eval <Java code>\n" + | ||
" Example: `.eval return \"5 + 5 is: \" + (5 + 5);\n" + | ||
" This will print: 5 + 5 is: 10"); | ||
} | ||
} |