Converting UserID to Member object [DISCORD4J]

227 Views Asked by At

I currently code a Discord bot in Java with Discord4j. Now, I want to get the mentioned user IDs (already did that) and convert them into Member object, but I don't know how to do it.

That's my code currently:

public static void run(MessageCreateEvent event) {
    if(event.getMessage().getUserMentionIds().toString() == "[]") {
        Utils.sendMessageToChannel("ERROR: No User got mentioned.", event);
    }
    Snowflake userMentions = Snowflake.of(event.getMessage().getUserMentionIds().toString());
    Member member = new Member(userMentions);
}
1

There are 1 best solutions below

0
NovaFox161 On BEST ANSWER

Discord4J does not allow the use of constructors on its entities and most objects. This is because all of the entities are built up from data from the cache and/or a rest request to discord.

To get a Member from a user ID you would do the following:

GatewayDiscordClient#getMemberById(guildId, userId).block();

If you just want the user, as Member requires the person be in the guild:

GatwayDiscordClient#getUserById(userId).block();

You can also do this reactively by (flat)mapping the Mono(s).