Skip to content

Commit

Permalink
✨ Forward message effect can now store the forwarded message
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Oct 8, 2024
1 parent 3b5883b commit ab96a11
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package info.itsthesky.disky.elements.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.config.Node;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
Expand All @@ -19,21 +20,25 @@ public class ForwardMessage extends AsyncEffect implements INodeHolder {
static {
Skript.registerEffect(
ForwardMessage.class,
"forward [the] [message] %message% to %channel/textchannel%"
"forward [the] [message] %message% to %channel/textchannel% [and store (it|the message) in %-object%]"
);
}

private Expression<Message> exprMessage;
private Expression<Object> exprChannel;
private Expression<Object> exprVariable;
private Node node;

@Override
public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {
getParser().setHasDelayBefore(Kleenean.TRUE);
node = getParser().getNode();

exprMessage = (Expression<Message>) expressions[0];
exprChannel = (Expression<Object>) expressions[1];
return true;
exprVariable = (Expression<Object>) expressions[2];

return exprVariable == null || Changer.ChangerUtils.acceptsChange(exprVariable, Changer.ChangeMode.SET, Message.class);
}

@Override
Expand All @@ -45,7 +50,10 @@ protected void execute(@NotNull Event event) {

if (rawChannel instanceof final MessageChannel channel) {
try {
message.forwardTo(channel).complete();
final var msg = message.forwardTo(channel).complete();

if (exprVariable != null)
exprVariable.change(event, new Object[] {msg}, Changer.ChangeMode.SET);
} catch (Exception ex) {
DiSkyRuntimeHandler.error(ex, node);
}
Expand All @@ -56,7 +64,8 @@ protected void execute(@NotNull Event event) {

@Override
public @NotNull String toString(@Nullable Event event, boolean debug) {
return "forward message " + exprMessage.toString(event, debug) + " to " + exprChannel.toString(event, debug);
return "forward message " + exprMessage.toString(event, debug) + " to " + exprChannel.toString(event, debug)
+ (exprVariable == null ? "" : " and store it in " + exprVariable.toString(event, debug));
}

@Override
Expand Down

0 comments on commit ab96a11

Please sign in to comment.