Skip to content

Commit

Permalink
⚡ Update UnbanMember effect to support user ID as string and add vali…
Browse files Browse the repository at this point in the history
…dation
  • Loading branch information
ItsTheSky committed Feb 6, 2025
1 parent 96d7323 commit 203c2cd
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.util.AsyncEffect;
import ch.njol.util.Kleenean;
import net.dv8tion.jda.api.entities.UserSnowflake;
import net.itsthesky.disky.api.skript.EasyElement;
import net.itsthesky.disky.elements.sections.handler.DiSkyRuntimeHandler;
import net.dv8tion.jda.api.entities.Guild;
Expand All @@ -26,32 +27,39 @@ public class UnbanMember extends AsyncEffect {
static {
Skript.registerEffect(
UnbanMember.class,
"[discord] un[-| ]ban [the] [discord] [user] %user% (from|in) [guild] %guild%"
"[discord] un[-| ]ban [the] [discord] [user] %user/string% (from|in) [guild] %guild%"
);
}

private Expression<User> exprUser;
private Expression<Object> exprTarget;
private Expression<Guild> exprGuild;
private Node node;

@Override
public boolean init(Expression[] expr, int i, Kleenean kleenean, ParseResult parseResult) {
exprUser = (Expression<User>) expr[0];
exprTarget = (Expression<Object>) expr[0];
exprGuild = (Expression<Guild>) expr[1];
node = getParser().getNode();
return true;
}

@Override
public void execute(@NotNull Event e) {
final User user = EasyElement.parseSingle(exprUser, e, null);
final Object target = EasyElement.parseSingle(exprTarget, e, null);
final Guild guild = EasyElement.parseSingle(exprGuild, e, null);

if (user == null || guild == null) {
DiSkyRuntimeHandler.error(new NullPointerException("The user or the guild cannot be null!"), node);
if (!DiSkyRuntimeHandler.checkSet(node, target, exprTarget, guild, exprGuild))
return;

if (!(target instanceof UserSnowflake) && !(target instanceof String)) {
DiSkyRuntimeHandler.error(new IllegalArgumentException("The given object is not a discord user or a string (user id)!"), node);
return;
}

final UserSnowflake user = target instanceof UserSnowflake
? (UserSnowflake) target
: UserSnowflake.fromId((String) target);

try {
guild.unban(user).complete();
} catch (Exception ex) {
Expand All @@ -61,6 +69,6 @@ public void execute(@NotNull Event e) {

@Override
public @NotNull String toString(@Nullable Event e, boolean debug) {
return "unbanned " + exprUser.toString(e, debug) + " from guild " + exprGuild.toString(e, debug);
return "unban " + exprTarget.toString(e, debug) + " from guild " + exprGuild.toString(e, debug);
}
}

0 comments on commit 203c2cd

Please sign in to comment.