From 93dcb2b358121891beecbeec356d30b616db1e19 Mon Sep 17 00:00:00 2001 From: Sky Date: Tue, 14 May 2024 14:42:25 +0200 Subject: [PATCH] :sparkles: Added retrieve start message of thread effect --- .../retrieve/RetrieveStartMessage.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveStartMessage.java diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveStartMessage.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveStartMessage.java new file mode 100644 index 0000000..e46925f --- /dev/null +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveStartMessage.java @@ -0,0 +1,56 @@ +package info.itsthesky.disky.elements.effects.retrieve; + +import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.skript.util.AsyncEffect; +import ch.njol.util.Kleenean; +import info.itsthesky.disky.DiSky; +import net.dv8tion.jda.api.entities.Message; +import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class RetrieveStartMessage extends AsyncEffect { + + static { + Skript.registerEffect( + RetrieveStartMessage.class, + "retrieve start message (from|with|of|in) %threadchannel% and store (it|the message) in %~objects%" + ); + } + + private Expression exprChannel; + private Expression exprResult; + + @Override + public boolean init(Expression[] expressions, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) { + exprChannel = (Expression) expressions[0]; + exprResult = expressions[1]; + return Changer.ChangerUtils.acceptsChange(exprResult, Changer.ChangeMode.SET, Message.class); + } + + @Override + protected void execute(@NotNull Event event) { + ThreadChannel channel = exprChannel.getSingle(event); + if (channel == null) + return; + + final Message message; + try { + message = channel.retrieveStartMessage().complete(); + } catch (Exception ex) { + DiSky.getErrorHandler().exception(event, ex); + return; + } + + exprResult.change(event, new Message[] {message}, Changer.ChangeMode.SET); + } + + @Override + public @NotNull String toString(@Nullable Event event, boolean debug) { + return "retrieve start message from " + exprChannel.toString(event, debug) + " and store it in " + exprResult.toString(event, debug); + } +}