From 3a195e6136c9d73a9da209197eaf193703000dfc Mon Sep 17 00:00:00 2001 From: Sky Date: Sat, 18 May 2024 22:11:35 +0200 Subject: [PATCH] :bug: Fixed & enhanced usage of event-bots in DiSky-related events --- src/main/java/info/itsthesky/disky/core/Bot.java | 15 +++++++++++++++ .../disky/elements/effects/CreateThread.java | 2 +- .../elements/effects/retrieve/RetrieveBans.java | 2 +- .../elements/effects/retrieve/RetrieveEmotes.java | 2 +- .../retrieve/RetrieveInterestedMembers.java | 2 +- .../elements/effects/retrieve/RetrieveInvite.java | 2 +- .../effects/retrieve/RetrieveInvites.java | 2 +- .../elements/effects/retrieve/RetrieveLogs.java | 2 +- .../elements/effects/retrieve/RetrieveMember.java | 2 +- .../effects/retrieve/RetrieveMessage.java | 2 +- .../effects/retrieve/RetrieveSticker.java | 2 +- .../effects/retrieve/RetrieveStickers.java | 2 +- .../effects/retrieve/RetrieveThreadMembers.java | 2 +- .../effects/retrieve/RetrieveThreads.java | 2 +- .../elements/getters/BaseGetterExpression.java | 4 ++-- 15 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/main/java/info/itsthesky/disky/core/Bot.java b/src/main/java/info/itsthesky/disky/core/Bot.java index 291e7ceb..9751e268 100644 --- a/src/main/java/info/itsthesky/disky/core/Bot.java +++ b/src/main/java/info/itsthesky/disky/core/Bot.java @@ -1,8 +1,10 @@ package info.itsthesky.disky.core; +import ch.njol.skript.lang.Expression; import ch.njol.skript.util.Timespan; import info.itsthesky.disky.BotApplication; import info.itsthesky.disky.DiSky; +import info.itsthesky.disky.api.events.SimpleDiSkyEvent; import info.itsthesky.disky.elements.structures.slash.SlashManager; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; @@ -16,6 +18,8 @@ import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; import net.dv8tion.jda.api.entities.channel.concrete.ThreadChannel; import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel; +import org.bukkit.event.Event; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.lang.reflect.Method; @@ -113,6 +117,17 @@ public BotOptions getOptions() { return DiSky.getManager().findAny(); } + public static @NotNull Bot fromEvent(Event event) { + if (event instanceof SimpleDiSkyEvent) + return byJDA(((SimpleDiSkyEvent) event).getJDAEvent().getJDA()); + return any(); + } + + public static @NotNull Bot fromContext(@Nullable Expression expr, Event event) { + final Bot first = expr == null ? null : expr.getSingle(event); + return first == null ? fromEvent(event) : first; + } + public static @Nullable Bot byJDA(JDA instance) { return DiSky.getManager().fromJDA(instance); } diff --git a/src/main/java/info/itsthesky/disky/elements/effects/CreateThread.java b/src/main/java/info/itsthesky/disky/elements/effects/CreateThread.java index 4b538569..eb2dac09 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/CreateThread.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/CreateThread.java @@ -65,7 +65,7 @@ public boolean init(Expression[] exprs, int i, Kleenean kleenean, SkriptParse public void execute(@NotNull Event e) { final String name = exprName.getSingle(e); final @Nullable Message message = parseSingle(exprMessage, e, null); - final Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(e); + final Bot bot = Bot.fromContext(exprBot, e); StandardGuildMessageChannel channel = exprChannel.getSingle(e); if (anyNull(this, bot, name, channel)) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveBans.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveBans.java index e4fd3259..3b9d6426 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveBans.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveBans.java @@ -39,7 +39,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene @Override protected void execute(@NotNull Event event) { Guild guild = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event);; if (guild == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveEmotes.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveEmotes.java index aed1668b..0eb3be01 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveEmotes.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveEmotes.java @@ -43,7 +43,7 @@ public boolean init(Expression[] expressions, int i, Kleenean kleenean, Skrip @Override protected void execute(Event event) { Guild guild = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (guild == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInterestedMembers.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInterestedMembers.java index a5b46501..163707e9 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInterestedMembers.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInterestedMembers.java @@ -47,7 +47,7 @@ public boolean init(Expression[] expressions, int i, Kleenean kleenean, Skrip @Override protected void execute(Event event) { ScheduledEvent scheduledEvent = exprEvent.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (scheduledEvent == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInvite.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInvite.java index f0ed87ac..67c16990 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInvite.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInvite.java @@ -43,7 +43,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene protected void execute(@NotNull Event event) { String id = exprID.getSingle(event); Guild guild = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (id == null || guild == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInvites.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInvites.java index 48efa7ff..9ea90cdd 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInvites.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveInvites.java @@ -39,7 +39,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene @Override protected void execute(@NotNull Event event) { Guild guild = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (guild == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveLogs.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveLogs.java index b0838644..99b69a7e 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveLogs.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveLogs.java @@ -48,7 +48,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene @Override protected void execute(@NotNull Event event) { Guild guild = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (guild == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveMember.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveMember.java index a6b6db95..997f7983 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveMember.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveMember.java @@ -42,7 +42,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene protected void execute(@NotNull Event event) { String id = exprID.getSingle(event); Guild guild = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (id == null || guild == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveMessage.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveMessage.java index adcd191a..3f1c48db 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveMessage.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveMessage.java @@ -47,7 +47,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene @Override protected void execute(@NotNull Event event) { String id = exprID.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); Channel rawChannel = exprChannel.getSingle(event); if (id == null || bot == null || rawChannel == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveSticker.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveSticker.java index 649b4525..8b24a39f 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveSticker.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveSticker.java @@ -48,7 +48,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene protected void execute(@NotNull Event event) { String id = exprID.getSingle(event); Guild guild = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (id == null || guild == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveStickers.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveStickers.java index a83a5336..555ad147 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveStickers.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveStickers.java @@ -45,7 +45,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene @Override protected void execute(@NotNull Event event) { Guild guild = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (guild == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveThreadMembers.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveThreadMembers.java index 0042c556..bbcef5d9 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveThreadMembers.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveThreadMembers.java @@ -47,7 +47,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene @Override protected void execute(@NotNull Event event) { ThreadChannel channel = exprChannel.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (channel == null || bot == null) return; diff --git a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveThreads.java b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveThreads.java index 74f7c2ac..583f2d7d 100644 --- a/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveThreads.java +++ b/src/main/java/info/itsthesky/disky/elements/effects/retrieve/RetrieveThreads.java @@ -50,7 +50,7 @@ public boolean init(Expression[] expressions, int i, @NotNull Kleenean kleene @Override protected void execute(@NotNull Event event) { Object entity = exprGuild.getSingle(event); - Bot bot = exprBot == null ? Bot.any() : exprBot.getSingle(event); + Bot bot = Bot.fromContext(exprBot, event); if (bot == null || !(entity instanceof IPostContainer)) return; diff --git a/src/main/java/info/itsthesky/disky/elements/getters/BaseGetterExpression.java b/src/main/java/info/itsthesky/disky/elements/getters/BaseGetterExpression.java index 58fc566a..48a1f3ff 100644 --- a/src/main/java/info/itsthesky/disky/elements/getters/BaseGetterExpression.java +++ b/src/main/java/info/itsthesky/disky/elements/getters/BaseGetterExpression.java @@ -53,7 +53,7 @@ public boolean init(Expression @NotNull [] exprs, int matchedPattern, @NotNul protected @Nullable T @NotNull [] get(@NotNull Event e) { final String id = exprId.getSingle(e); - final Bot bot = EasyElement.parseSingle(exprBot, e, DiSky.getManager().findAny()); + final Bot bot = Bot.fromContext(exprBot, e); if (EasyElement.anyNull(this, id, bot)) return (T[]) new Object[0]; return (T[]) new Object[] {get(id, bot)}; @@ -80,7 +80,7 @@ protected boolean allowBot() { @Override public T[] getAsync(Event e) { final String id = exprId.getSingle(e); - final Bot bot = EasyElement.parseSingle(exprBot, e, DiSky.getManager().findAny()); + final Bot bot = Bot.fromContext(exprBot, e); if (EasyElement.anyNull(this, id, bot)) return (T[]) new Object[0]; return (T[]) new Object[] {getAsync(id, bot)};