Skip to content

Commit

Permalink
✨ Added retrieve start message of thread effect
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed May 14, 2024
1 parent e1899a7 commit 93dcb2b
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<ThreadChannel> exprChannel;
private Expression<?> exprResult;

@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {
exprChannel = (Expression<ThreadChannel>) 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);
}
}

0 comments on commit 93dcb2b

Please sign in to comment.