Skip to content

Commit

Permalink
🚀 Added find effects!
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsTheSky committed Apr 22, 2024
1 parent 5a73d2a commit cd65bc4
Show file tree
Hide file tree
Showing 3 changed files with 437 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package info.itsthesky.disky.elements.effects.find;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.config.Node;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.util.AsyncEffect;
import ch.njol.util.Kleenean;
import info.itsthesky.disky.DiSky;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class FindMembersWithNickname extends AsyncEffect {

static {
Skript.registerEffect(
FindMembersWithNickname.class,
"find [the] [discord] member[s] with [the] nick[( |-)]name %string% [case:ignor(e|ing) [the] case] (from|in) [the] [guild] %guild% and store (them|the member[s]) in %~objects%"
);
}

private Expression<String> exprNickname;
private Expression<Guild> exprGuild;
private Expression<Object> exprResult;
private Node node;
private boolean ignoreCase;

@Override
public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {
getParser().setHasDelayBefore(Kleenean.TRUE);

exprNickname = (Expression<String>) expressions[0];
exprGuild = (Expression<Guild>) expressions[1];
exprResult = (Expression<Object>) expressions[2];

node = getParser().getNode();
ignoreCase = parseResult.hasTag("case");

return Changer.ChangerUtils.acceptsChange(exprResult, Changer.ChangeMode.SET, Member[].class);
}

@Override
protected void execute(@NotNull Event event) {
final String nickname = exprNickname.getSingle(event);
final Guild guild = exprGuild.getSingle(event);
if (nickname == null || guild == null)
return;

final List<Member> members;
try {
members = guild.findMembers(member -> {
if (ignoreCase) {
return member.getEffectiveName().equalsIgnoreCase(nickname);
} else {
return member.getEffectiveName().equals(nickname);
}
}).get();
} catch (Exception e) {
DiSky.getErrorHandler().exception(event, e);
return;
}

exprResult.change(event, members.toArray(new Member[0]), Changer.ChangeMode.SET);
}

@Override
public @NotNull String toString(@Nullable Event event, boolean debug) {
return "find members with nickname " + exprNickname.toString(event, debug)
+ " in guild " + exprGuild.toString(event, debug)
+ " and store them in " + exprResult.toString(event, debug);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package info.itsthesky.disky.elements.effects.find;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer;
import ch.njol.skript.config.Node;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.util.AsyncEffect;
import ch.njol.util.Kleenean;
import info.itsthesky.disky.DiSky;
import info.itsthesky.disky.core.SkriptUtils;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role;
import org.bukkit.event.Event;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class FindMembersWithRoles extends AsyncEffect {

static {
Skript.registerEffect(
FindMembersWithRoles.class,
"find [the] [discord] member[s] with [the] role[s] %roles% and store (them|the member[s]) in %~objects%"
);
}

private Expression<Role> exprRoles;
private Expression<Object> exprResult;
private Node node;

@Override
public boolean init(Expression<?> @NotNull [] expressions, int matchedPattern, @NotNull Kleenean isDelayed, SkriptParser.@NotNull ParseResult parseResult) {
getParser().setHasDelayBefore(Kleenean.TRUE);

exprRoles = (Expression<Role>) expressions[0];
exprResult = (Expression<Object>) expressions[1];

node = getParser().getNode();

return Changer.ChangerUtils.acceptsChange(exprResult, Changer.ChangeMode.SET, Member[].class);
}

@Override
protected void execute(@NotNull Event event) {
final Object[] rolesObjs = exprRoles.getArray(event);
if (rolesObjs.length == 0)
return;

// First, parse all roles
final Role[] roles = new Role[rolesObjs.length];
for (int i = 0; i < rolesObjs.length; i++) {
final Object obj = rolesObjs[i];
if (obj == null) {
SkriptUtils.error(node, "All provided roles must be a role! (one of them is null)");
return;
}

if (!(obj instanceof Role)) {
SkriptUtils.error(node, "All provided roles must be a role! (one of them is not a role)");
return;
}

roles[i] = (Role) obj;
}

// Be sure all roles are in the same guild
@Nullable Guild guild = null;
for (Role role : roles) {
if (guild == null) {
guild = role.getGuild();
continue;
}

if (!role.getGuild().equals(guild)) {
SkriptUtils.error(node, "All provided roles must be in the same guild!");
return;
}
}

final List<Member> members;
try {
members = guild.findMembersWithRoles(roles).get();
} catch (Exception e) {
DiSky.getErrorHandler().exception(event, e);
return;
}

exprResult.change(event, members.toArray(new Member[0]), Changer.ChangeMode.SET);
}

@Override
public @NotNull String toString(@Nullable Event event, boolean debug) {
return "find members with roles " + exprRoles.toString(event, debug) + " and store them in " + exprResult.toString(event, debug);
}

}
Loading

0 comments on commit cd65bc4

Please sign in to comment.