3131import com .jagrosh .jdautilities .command .CommandEvent ;
3232import com .jagrosh .jdautilities .command .SlashCommand ;
3333import com .jagrosh .jdautilities .command .SlashCommandEvent ;
34+ import com .mcmoddev .mmdbot .core .TaskScheduler ;
3435import com .mcmoddev .mmdbot .modules .commands .DismissListener ;
3536import com .mcmoddev .mmdbot .utilities .Utils ;
3637import com .mcmoddev .mmdbot .utilities .scripting .ScriptingContext ;
5152import javax .annotation .Nullable ;
5253import java .util .List ;
5354import java .util .Objects ;
55+ import java .util .concurrent .TimeUnit ;
5456
5557public class CmdEvaluate extends SlashCommand {
5658
@@ -66,7 +68,7 @@ public CmdEvaluate() {
6668 protected void execute (final SlashCommandEvent event ) {
6769 event .deferReply ().queue (hook -> {
6870 try {
69- ScriptingUtils . evaluate ( Utils . getOrEmpty ( event , "script" ), createContext (new EvaluationContext () {
71+ final var context = createContext (new EvaluationContext () {
7072 @ Override
7173 public Guild getGuild () {
7274 return event .getGuild ();
@@ -103,7 +105,17 @@ public void replyEmbeds(final MessageEmbed... embeds) {
103105 hook .editOriginal (new MessageBuilder ().setEmbeds (embeds ).setAllowedMentions (ALLOWED_MENTIONS ).build ())
104106 .setActionRow (DismissListener .createDismissButton (getUser ())).queue ();
105107 }
106- }));
108+ });
109+
110+ final var evalThread = new Thread (() -> ScriptingUtils .evaluate (Utils .getOrEmpty (event , "script" ), context ), "ScriptEvaluation" );
111+ evalThread .setDaemon (true );
112+ evalThread .start ();
113+ TaskScheduler .scheduleTask (() -> {
114+ if (evalThread .isAlive ()) {
115+ evalThread .interrupt ();
116+ hook .editOriginal ("Evaluation was timed out!" ).queue ();
117+ }
118+ }, 4 , TimeUnit .SECONDS );
107119 } catch (ScriptingUtils .ScriptingException e ) {
108120 hook .editOriginal ("There was an exception evaluating: " + e .getLocalizedMessage ()).queue ();
109121 }
@@ -186,7 +198,16 @@ public void replyEmbeds(final MessageEmbed... embeds) {
186198 event .getTextChannel ().editMessageEmbedsById (msgId , embeds ).queue ();
187199 });
188200 }
189- ScriptingUtils .evaluate (script , context );
201+ final String finalScript = script ;
202+ final var evalThread = new Thread (() -> ScriptingUtils .evaluate (finalScript , context ), "ScriptEvaluation" );
203+ evalThread .setDaemon (true );
204+ evalThread .start ();
205+ TaskScheduler .scheduleTask (() -> {
206+ if (evalThread .isAlive ()) {
207+ evalThread .interrupt ();
208+ event .getMessage ().reply ("Evaluation was timed out!" ).queue ();
209+ }
210+ }, 4 , TimeUnit .SECONDS );
190211 } catch (ScriptingUtils .ScriptingException e ) {
191212 event .getMessage ().reply ("There was an exception evaluating: " + e .getLocalizedMessage ()).queue ();
192213 }
0 commit comments