From 376abc4aab37b165762c511882d053ca377ca151 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Sun, 25 Dec 2022 16:13:56 -0500 Subject: [PATCH] Add DiscordService#getMemberById --- .../api/v2/services/discord/DiscordService.java | 9 +++++++++ .../essentialsx/discord/JDADiscordService.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/DiscordService.java b/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/DiscordService.java index 30c3a8dc5..1f6a4b2b0 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/DiscordService.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/api/v2/services/discord/DiscordService.java @@ -4,6 +4,8 @@ import net.essentialsx.api.v2.events.discord.DiscordChatMessageEvent; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import java.util.concurrent.CompletableFuture; + /** * A class which provides numerous methods to interact with EssentialsX Discord. */ @@ -47,6 +49,13 @@ public interface DiscordService { */ InteractionController getInteractionController(); + /** + * Gets an {@link InteractionMember} by their Discord ID. + * @param id The ID of the member to look up. + * @return A future which will complete with the member or null if none is reachable. + */ + CompletableFuture getMemberById(final String id); + /** * Gets unstable API that is subject to change at any time. * @return {@link Unsafe the unsafe} instance. diff --git a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java index a1030442c..9f2d94417 100644 --- a/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java +++ b/EssentialsDiscord/src/main/java/net/essentialsx/discord/JDADiscordService.java @@ -26,9 +26,11 @@ import net.essentialsx.api.v2.events.discord.DiscordMessageEvent; import net.essentialsx.api.v2.services.discord.DiscordService; import net.essentialsx.api.v2.services.discord.InteractionController; import net.essentialsx.api.v2.services.discord.InteractionException; +import net.essentialsx.api.v2.services.discord.InteractionMember; import net.essentialsx.api.v2.services.discord.MessageType; import net.essentialsx.api.v2.services.discord.Unsafe; import net.essentialsx.discord.interactions.InteractionControllerImpl; +import net.essentialsx.discord.interactions.InteractionMemberImpl; import net.essentialsx.discord.interactions.commands.ExecuteCommand; import net.essentialsx.discord.interactions.commands.ListCommand; import net.essentialsx.discord.interactions.commands.MessageCommand; @@ -466,6 +468,19 @@ public class JDADiscordService implements DiscordService, IEssentialsModule { } } + @Override + public CompletableFuture getMemberById(final String id) { + final CompletableFuture future = new CompletableFuture<>(); + getGuild().retrieveMemberById(id).queue(member -> { + if (member != null) { + future.complete(new InteractionMemberImpl(member)); + return; + } + future.complete(null); + }, fail -> future.complete(null)); + return future; + } + public JDA getJda() { return jda; }