Reputation: 172
I get a User from User.fromID(long id)
User u = User.fromID(123456);
I then want to get the name of that user. So I use User.getName()
String name = u.getName();
But then I get an error.
[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.UnsupportedOperationException: This User instance only wraps an ID. Other operations are unsupported
at net.dv8tion.jda.internal.entities.UserById.unsupported(UserById.java:78)
at net.dv8tion.jda.internal.entities.UserById.getName(UserById.java:85)
at bot.Profile.<init>(Profile.java:19)
at files.Profiles.handleUser(Profiles.java:17)
at bot.Chat.onMessageReceived(Chat.java:19)
at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:430)
at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:82)
at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:69)
at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:150)
at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:122)
at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:948)
at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:835)
at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:813)
at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:986)
at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)
How can I get the name of a User that only wraps an ID?
Any help is appreciated.
Upvotes: 2
Views: 7514
Reputation: 6134
You need a JDA instance and use retrieveUserById instead.
Example:
jda.retrieveUserById(userId)
.map(User::getName)
.queue(name -> {
// use name here
System.out.println("The user has the name " + name);
});
Upvotes: 4