Can't find user by id #2569
-
So i tried to code a bot that dms people Here is my code package me.rtx4090;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.guild.GuildReadyEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.entities.User;
public class Main extends ListenerAdapter {
private JDA jda;
public Main() {
String botToken = "(censored)";
jda = JDABuilder.createDefault(botToken).addEventListeners(this).build();
}
public static void main(String[] args) {
new Main();
}
@Override
public void onGuildReady(GuildReadyEvent event) {
System.out.println("All members in the guild " + event.getGuild().getName() + " have been downloaded.");
// Now you can be sure that all server members are loaded
sendPrivateMessage();
}
private void sendPrivateMessage() {
String recipientUserId = "(censored)";
String messageContent = "L";
User recipient = jda.getUserById(recipientUserId);
System.out.println(recipient);
if (recipient != null) {
recipient.openPrivateChannel().queue(privateChannel -> {
privateChannel.sendMessage(messageContent).queue();
System.out.println("Message has been sent to " + recipient.getAsTag() + " successfully");
});
} else {
System.out.println("Can't find the user. Please make sure you've used the correct ID");
}
}
} And here is the console 12:28:54 AM: Executing ':Main.main()'...
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :Main.main()
All members in the guild Private have been downloaded.
null
Can't find the user. Please make sure you've used the correct ID
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".
SLF4J: Defaulting to no-operation MDCAdapter implementation.
SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details.
[main] INFO JDA - Login Successful!
[JDA MainWS-ReadThread] INFO WebSocketClient - Connected to WebSocket
[JDA MainWS-ReadThread] INFO JDA - Finished Loading!
Please help me to solve this 🙏🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The method you are invoking uses the JDA cache for the id which probably has not have been invalidated yet. What I have been using on my project to send private message to a user looks like this //get the user from the event and filter based on your preferences. I used the member.getRoles() to keep only the objects that belong to a role I have defined.
List<Member> members = event.getGuild().loadMembers()
.get()
.stream()
.filter(member -> member.getRoles().contains(ROLE))
.collect(Collectors.toList());
for (Member member : members)
PrivateChannel channel = member.getUser().openPrivateChannel().complete();
channel.sendMessage(message).queue(); Also if you have the guild Id and the user ID you can do something like the following: Member caller = jda.getGuildById(guildId).loadMembers()
.get()
.stream()
.filter(m -> m.getUser().getId().equals(userId))
.collect(Collectors.toList()).get(0); I would recommend to read the Guild.java (findMembers method), Member.java and JDA.java (retrieveUserById method) as they might provide some more insight on how to proceed. Hope this helps :) |
Beta Was this translation helpful? Give feedback.
-
This has already been answered on stackoverflow. The right approach is to use |
Beta Was this translation helpful? Give feedback.
This has already been answered on stackoverflow. The right approach is to use
openPrivateChannelById
instead.