From d181f7b07c3abb8d55f2b378685cd71d883a8a91 Mon Sep 17 00:00:00 2001 From: Sky Date: Tue, 1 Oct 2024 10:50:29 +0200 Subject: [PATCH] :recycle: Guild/Bot avatar property is now async --- .../disky/elements/properties/AvatarURL.java | 116 ++++++++++-------- 1 file changed, 65 insertions(+), 51 deletions(-) diff --git a/src/main/java/info/itsthesky/disky/elements/properties/AvatarURL.java b/src/main/java/info/itsthesky/disky/elements/properties/AvatarURL.java index fb1b0cf9..85e0460a 100644 --- a/src/main/java/info/itsthesky/disky/elements/properties/AvatarURL.java +++ b/src/main/java/info/itsthesky/disky/elements/properties/AvatarURL.java @@ -1,17 +1,26 @@ package info.itsthesky.disky.elements.properties; import ch.njol.skript.classes.Changer; +import ch.njol.skript.config.Node; import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; import ch.njol.skript.expressions.base.SimplePropertyExpression; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.util.Kleenean; import info.itsthesky.disky.DiSky; import info.itsthesky.disky.api.changers.ChangeableSimplePropertyExpression; import info.itsthesky.disky.api.skript.EasyElement; +import info.itsthesky.disky.api.skript.INodeHolder; import info.itsthesky.disky.core.Bot; +import info.itsthesky.disky.core.SkriptUtils; import info.itsthesky.disky.core.Utils; +import info.itsthesky.disky.elements.changers.IAsyncChangeableExpression; +import info.itsthesky.disky.elements.sections.handler.DiSkyRuntimeHandler; import net.dv8tion.jda.api.entities.*; import net.dv8tion.jda.api.entities.sticker.Sticker; +import net.dv8tion.jda.api.requests.RestAction; import org.bukkit.event.Event; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -23,7 +32,8 @@ @Description({"Return the avatar URL of any user, guild or bot.", "This can be changed for guilds and bots only!"}) @Examples({"avatar of event-guild", "avatar of event-user"}) -public class AvatarURL extends ChangeableSimplePropertyExpression { +public class AvatarURL extends ChangeableSimplePropertyExpression + implements IAsyncChangeableExpression, INodeHolder { static { register( @@ -34,58 +44,18 @@ public class AvatarURL extends ChangeableSimplePropertyExpression[] expressions, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + node = getParser().getNode(); + return super.init(expressions, matchedPattern, isDelayed, parseResult); + } - if (entity instanceof Guild) - Utils.catchAction(((Guild) entity).getManager().setIcon(icon), e); - else - Utils.catchAction(((Bot) entity).getInstance().getSelfUser().getManager().setAvatar(icon), e); - } + @Override + @NotNull + public Node getNode() { + return node; } @Override @@ -121,4 +91,48 @@ public void change(Event e, Object[] delta, Bot bot, Changer.ChangeMode mode) { public @NotNull Class getReturnType() { return String.class; } + + @Override + public void changeAsync(Event e, Object[] delta, Changer.ChangeMode mode) { + change(e, delta, mode, true); + } + + @Override + public void change(Event e, Object[] delta, Bot bot, Changer.ChangeMode mode) { + change(e, delta, mode, false); + } + + public void change(Event e, Object[] delta, Changer.ChangeMode mode, boolean async) { + if (!EasyElement.isValid(delta)) + return; + final String value = (String) delta[0]; + final Object entity = EasyElement.parseSingle(getExpr(), e, null); + if (entity == null || entity instanceof User) + return; + + RestAction action = null; + + if (mode == Changer.ChangeMode.RESET || mode == Changer.ChangeMode.DELETE) { + if (entity instanceof final Guild guild) + action = guild.getManager().setIcon(null); + if (entity instanceof final Bot bot) + action = bot.getInstance().getSelfUser().getManager().setAvatar(null); + } else { + final var parsedIcon = SkriptUtils.parseIcon(value); + if (parsedIcon == null) { + DiSkyRuntimeHandler.error(new IllegalArgumentException("Cannot parse the given icon URL: " + value), node); + return; + } + + if (entity instanceof final Guild guild) + action = guild.getManager().setIcon(parsedIcon); + else if (entity instanceof final Bot bot) + action = bot.getInstance().getSelfUser().getManager().setAvatar(parsedIcon); + } + + if (action != null) { + if (async) action.complete(); + else action.queue(); + } + } }