Skip to content

Commit

Permalink
🐛 Fixed & enhanced usage of event-bots in DiSky-related events
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed May 18, 2024
1 parent b91c73d commit 3a195e6
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 15 deletions.
15 changes: 15 additions & 0 deletions src/main/java/info/itsthesky/disky/core/Bot.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Bot> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)};
Expand All @@ -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)};
Expand Down

0 comments on commit 3a195e6

Please sign in to comment.