Skip to content

Commit

Permalink
⚡ Made get member, sticker, user & user in guild expression async
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed May 17, 2024
1 parent e72ebcd commit c58f9e0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import info.itsthesky.disky.DiSky;
import info.itsthesky.disky.api.skript.EasyElement;
import info.itsthesky.disky.core.Bot;
import info.itsthesky.disky.elements.changers.IAsyncGettableExpression;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@SuppressWarnings({"unchecked"})
public abstract class BaseGetterExpression<T> extends SimpleExpression<T> {
public abstract class BaseGetterExpression<T> extends SimpleExpression<T> implements IAsyncGettableExpression<T> {

protected static <T> void register(Class clazz,
Class type,
Expand All @@ -33,10 +34,13 @@ protected static <T> void register(Class clazz,
codeName + " (with|from) [the] "+property+" %string% "+ (allowBot ? "[(with|using) [the] [bot] [(named|with name)] %-bot%]" : ""));
}

private Expression<String> exprId;
private Expression<Bot> exprBot;
protected Expression<String> exprId;
protected Expression<Bot> exprBot;

protected abstract T get(String id, Bot bot);
protected T getAsync(String id, Bot bot) {
return get(id, bot);
};

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull SkriptParser.ParseResult parseResult) {
Expand Down Expand Up @@ -72,4 +76,13 @@ protected boolean allowBot() {
exprBot != null ? "using the bot " + exprBot.toString(e, debug) : ""
);
}

@Override
public T[] getAsync(Event e) {
final String id = exprId.getSingle(e);
final Bot bot = EasyElement.parseSingle(exprBot, e, DiSky.getManager().findAny());
if (EasyElement.anyNull(this, id, bot))
return (T[]) new Object[0];
return (T[]) new Object[] {getAsync(id, bot)};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public boolean isSingle() {

@Override
public Member[] getAsync(Event e) {
DiSky.debug("Getting member async");
final String id = exprId.getSingle(e);
final Guild guild = exprGuild.getSingle(e);
if (EasyElement.anyNull(this, id, guild))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,25 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import info.itsthesky.disky.api.skript.EasyElement;
import info.itsthesky.disky.elements.changers.IAsyncGettableExpression;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.sticker.GuildSticker;
import net.dv8tion.jda.api.entities.sticker.Sticker;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.stream.Collectors;

@Name("Get Sticker")
@Description({"Get a cached sticker from its per-guild name",
"This expression is here to get a sticker from its name.",
"If you success to get a sticker's ID, use the retrieve sticker effect instead!",
"This expression cannot be changed"})
@Examples({"sticker with named \"meliodas\" from event-guild"})
public class GetSticker extends SimpleExpression<Sticker> {
public class GetSticker extends SimpleExpression<Sticker> implements IAsyncGettableExpression<Sticker> {

static {
Skript.registerExpression(
Expand Down Expand Up @@ -67,4 +72,21 @@ public boolean isSingle() {
public @NotNull String toString(@Nullable Event e, boolean debug) {
return "sticker with name " + exprId.toString(e, debug) +" in " + exprGuild.toString(e, debug);
}

@Override
public Sticker[] getAsync(Event e) {
final String id = exprId.getSingle(e);
final Guild guild = exprGuild.getSingle(e);
if (EasyElement.anyNull(this, id, guild))
return new Sticker[0];

final Sticker found = guild.getStickersByName(id, false).stream().findFirst().orElse(null);
if (found != null)
return new Sticker[] {found};

final List<GuildSticker> retrieveFound = guild.retrieveStickers().complete()
.stream().filter(sticker -> sticker.getName().equalsIgnoreCase(id)).collect(Collectors.toList());

return retrieveFound.isEmpty() ? new Sticker[0] : new Sticker[] {retrieveFound.get(0)};
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package info.itsthesky.disky.elements.getters;

import info.itsthesky.disky.core.Bot;
import info.itsthesky.disky.elements.changers.IAsyncGettableExpression;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.User;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;

public class GetUser extends BaseGetterExpression<User> {
Expand All @@ -27,4 +29,9 @@ public String getCodeName() {
public @NotNull Class<? extends User> getReturnType() {
return User.class;
}

@Override
protected User getAsync(String id, Bot bot) {
return bot.getInstance().retrieveUserById(id).complete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.util.Kleenean;
import info.itsthesky.disky.api.skript.EasyElement;
import info.itsthesky.disky.elements.changers.IAsyncGettableExpression;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
Expand All @@ -21,7 +22,7 @@
"Users are common to whole Discord, two user cannot have the same instance.",
"Members are common to guilds, but also holding an user as reference.",
"User can have multiple instance according to which guild they are in, therefore they are considered as member."})
public class GetUserInGuild extends SimpleExpression<Member> {
public class GetUserInGuild extends SimpleExpression<Member> implements IAsyncGettableExpression<Member> {

static {
Skript.registerExpression(GetUserInGuild.class,
Expand All @@ -30,6 +31,13 @@ public class GetUserInGuild extends SimpleExpression<Member> {
"%user% in [the] [guild] %guild%");
}

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull SkriptParser.ParseResult parseResult) {
exprUser = (Expression<User>) exprs[0];
exprGuild = (Expression<Guild>) exprs[1];
return true;
}

private Expression<User> exprUser;
private Expression<Guild> exprGuild;

Expand Down Expand Up @@ -58,9 +66,11 @@ public boolean isSingle() {
}

@Override
public boolean init(Expression<?> @NotNull [] exprs, int matchedPattern, @NotNull Kleenean isDelayed, @NotNull SkriptParser.ParseResult parseResult) {
exprUser = (Expression<User>) exprs[0];
exprGuild = (Expression<Guild>) exprs[1];
return true;
public Member[] getAsync(Event e) {
final User user = EasyElement.parseSingle(exprUser, e, null);
final Guild guild = EasyElement.parseSingle(exprGuild, e, null);
if (EasyElement.anyNull(this, user, guild))
return new Member[0];
return new Member[] {guild.retrieveMember(user).complete()};
}
}

0 comments on commit c58f9e0

Please sign in to comment.