Skip to content

Commit 76e7013

Browse files
committed
Add timeout for evaluation commands
1 parent bea34b5 commit 76e7013

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

src/main/java/com/mcmoddev/mmdbot/modules/commands/community/CmdEvaluate.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.jagrosh.jdautilities.command.CommandEvent;
3232
import com.jagrosh.jdautilities.command.SlashCommand;
3333
import com.jagrosh.jdautilities.command.SlashCommandEvent;
34+
import com.mcmoddev.mmdbot.core.TaskScheduler;
3435
import com.mcmoddev.mmdbot.modules.commands.DismissListener;
3536
import com.mcmoddev.mmdbot.utilities.Utils;
3637
import com.mcmoddev.mmdbot.utilities.scripting.ScriptingContext;
@@ -51,6 +52,7 @@
5152
import javax.annotation.Nullable;
5253
import java.util.List;
5354
import java.util.Objects;
55+
import java.util.concurrent.TimeUnit;
5456

5557
public 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

Comments
 (0)